Friday, October 28, 2016

How to force HTML table cell as text in Excel

<td> style to force the cell as text: mso-number-format:"\@";
<td> style to force the cell as number: mso-number-format:General;

Code Behind(cs) file
        public PartialViewResult ExcelFile()
        {
            StringBuilder table = new StringBuilder();
            table.AppendLine("<table>");
            table.AppendLine("<thead>");
            table.AppendLine("<tr>");
            table.AppendLine("<th>Date</th>");
            table.AppendLine("<th>Store</th>");
            table.AppendLine("</tr>");
            table.AppendLine("</thead>");

            table.AppendLine("<tbody>");
            table.AppendLine("<tr>");
            table.Append("<td>10/27/2016</td>");
            table.Append("<td style='mso-number-format:\"\\@\"'>0001</td>");
            table.AppendLine("</tr>");
            table.AppendLine("<tr>");
            table.Append("<td>10/27/2016</td>");
            table.Append("<td style='mso-number-format:\"\\@\"'>0002</td>");
            table.AppendLine("</tr>");
            table.AppendLine("</tbody>");
            table.AppendLine("</table>");

            Response.ContentType = "application/force-download";
            Response.AddHeader("content-disposition", "attachment; filename=ExcelFile_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls");
            Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
            Response.Write("<head>");
            Response.Write("<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
            Response.Write("<!--[if gte mso 9]><xml>");
            Response.Write("<x:ExcelWorkbook>");
            Response.Write("<x:ExcelWorksheets>");
            Response.Write("<x:ExcelWorksheet>");
            Response.Write("<x:Name>ExcelFile</x:Name>");
            Response.Write("<x:WorksheetOptions>");
            Response.Write("<x:Print>");
            Response.Write("<x:ValidPrinterInfo/>");
            Response.Write("</x:Print>");
            Response.Write("</x:WorksheetOptions>");
            Response.Write("</x:ExcelWorksheet>");
            Response.Write("</x:ExcelWorksheets>");
            Response.Write("</x:ExcelWorkbook>");
            Response.Write("</xml>");
            Response.Write("<![endif]--> ");
            Response.Write("</head>");
            Response.Write(table.ToString());
            Response.Flush();

            return PartialView();
        }

Wednesday, April 6, 2016

How to launch the Android emulator from the command line?

1. Go to SDK folder
Go to android sdk folder in which "android.bat" and "emulator.exe" are located.
ex) Default folder on Windows: C:\Users\{user}\AppData\Local\Android\sdk\tools

2. Run AVD manager
Type in "android avd" in the command prompt to launch the Android Virtual Devices manager.

3. Create AVD
Create a AVD by clicking "Create..." button.
Or you can just use the one already created from the list.

4. Launch emulator
Run the AVD either by using command "emulator -avd <name>" or by clicking "Start..." button on the AVD manager.

Tuesday, April 5, 2016

How to install app in Android emulator?

1. Launch emulator
Go to here to see how to launch Android Emulator.

2. Copy the apk file to platform-tools folder
ex) Default folder on Windows: C:\Users\{user}\AppData\Local\Android\sdk\platform-tools

3. Go to command prompt
Type "adb install filename.apk" to install
Type "adb install -r filename.apk" to re-install

4. Run emulator then you will see the app is installed.

Friday, March 11, 2016

How to pass javascript array to C# code behind

Used jquery version: jquery-1.8.3.min.js

html(cshtml) file
function updateNotification() {
    var ids = [1, 2, 3, 4, 5];

    if (ids.length > 0) {
        $.ajax({
            traditional: true,             // Should be true
            type: "POST",
            url: "/Notification/Update",   // Url to pass
            data: { ids: ids },
            dataType: "json",
            success: function (result) {
            },
            error: function (result) {
            }
        });
    }
}

Code Behind(cs) file
        [HttpPost]
        public JsonResult Update(int[] ids)
        {
            // ids is int array

            return Json(new { result = true });
        }

Wednesday, December 16, 2015

[C# 초보 강좌] 예제로 배우는 C# 01


예제로 배우는 C# 첫번째 강좌입니다.
책을 봐도 잘 모르시겠다는 분들이나, 개념은 알겠는데 막상 프로그램을 못하시는 분들을 위한 강좌입니다.

비주얼 스튜디오를 깔고 Hello C# 프로그램을 통해서 기본 구조를 익히겠습니다.
전체 설명은 아래 YouTube를 참조하시고 코드 부분만 밑에 넣도록 하겠습니다.




HelloCSharp.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello C#");
            Console.WriteLine("Hello " + args[0]);

            Console.ReadKey();
        }
    }
}



Tuesday, December 8, 2015

[C# 강의 중급] 라이브러리를 만들자 04 - 확장 메소드


C# 라이브러리를 만들자 네번째 강의입니다.
자신의 라이브러리를 구축하는 것은 프로그래머로서 재산을 축적하 것과 같다고 생각합니다.

오늘은 String과 DateTime에 Extension Methods를 추가하는 클래스를 만들어 라이브러리에 넣도록 하겠습니다.
전체 설명은 아래 YouTube를 참조하시고 코드 부분만 밑에 넣도록 하겠습니다.









BabyCarrot.Extensions.StringExtensions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BabyCarrot.Extensions
{
    public static class StringExtensions
    {
        #region Methods
        public static bool IsNumeric(this string s)
        {
            long result;
            return long.TryParse(s, out result);
        }

        public static bool IsDateTime(this string s)
        {
            if (String.IsNullOrEmpty(s))
            {
                return false;
            }
            else
            {
                DateTime result;
                return DateTime.TryParse(s, out result);
            }
        }
        #endregion
    }
}



BabyCarrot.Extensions.DateTimeExtensions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BabyCarrot.Extensions
{
    public static class DateTimeExtensions
    {
        #region Methods
        public static DateTime FirstDateOfMonth(this DateTime date)
        {
            return new DateTime(date.Year, date.Month, 1);
        }

        public static DateTime LastDateOfMonth(this DateTime date)
        {
            return new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
        }
        #endregion
    }
}



Monday, December 7, 2015

[C# 강의 중급] 라이브러리를 만들자 03 - Log Manager 확장


C# 라이브러리를 만들자 세번째 강의입니다.
자신의 라이브러리를 구축하는 것은 프로그래머로서 재산을 축적하 것과 같다고 생각합니다.

이전 강의에서 만들었던 LogManager Class를 몇 개의 Option을 이용해서 확장해 보겠습니다.
전체 설명은 아래 YouTube를 참조하시고 코드 부분만 밑에 넣도록 하겠습니다.








BabyCarrot.Tools.LogManager
using System;
using System.IO;

namespace BabyCarrot.Tools
{
    public enum LogType { Daily, Monthly }


    public class LogManager
    {
        private string _path;


        #region Constructors
        public LogManager(string path, LogType logType, string prefix, string postfix)
        {
            _path = path;
            _SetLogPath(logType, prefix, postfix);
        }

        public LogManager(string prefix, string postfix)
            : this(Path.Combine(Application.Root, "Log"), LogType.Daily, prefix, postfix)
        {

        }

        public LogManager()
            : this(Path.Combine(Application.Root, "Log"), LogType.Daily, null, null)
        {
        }
        #endregion


        #region Methods
        private void _SetLogPath(LogType logType, string prefix, string postfix)
        {
            string path = String.Empty;
            string name = String.Empty;

            switch (logType)
            {
                case LogType.Daily:
                    path = String.Format(@"{0}\{1}\", DateTime.Now.Year, DateTime.Now.ToString("MM"));
                    name = DateTime.Now.ToString("yyyyMMdd");
                    break;
                case LogType.Monthly:
                    path = String.Format(@"{0}\", DateTime.Now.Year);
                    name = DateTime.Now.ToString("yyyyMM");
                    break;
            }

            _path = Path.Combine(_path, path);
            if (!Directory.Exists(_path))
                Directory.CreateDirectory(_path);

            if (!String.IsNullOrEmpty(prefix))
                name = prefix + name;
            if (!String.IsNullOrEmpty(postfix))
                name = name + postfix;
            name += ".txt";

            _path = Path.Combine(_path, name);
        }

        public void Write(string data)
        {
            try
            {
                using (StreamWriter writer = new StreamWriter(_path, true))
                {
                    writer.Write(data);
                }
            }
            catch (Exception ex)
            { }
        }

        public void WriteLine(string data)
        {
            try
            {
                using (StreamWriter writer = new StreamWriter(_path, true))
                {
                    writer.WriteLine(DateTime.Now.ToString("yyyyMMdd HH:mm:ss\t") + data);
                }
            }
            catch (Exception ex)
            { }
        }
        #endregion
    }
}