Home » c# » c# – Winform-Export To Excel – XLWorkbook – Date and Time NumberFormat-Exceptionshub

c# – Winform-Export To Excel – XLWorkbook – Date and Time NumberFormat-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

My Requirement is –

  1. I have to export Datatable to .xls file.
  2. For all Date Columns in Datatable, the excel cell should be of Date Format instead of Text or General.
    This is because, the user can filter data according to date as required.

MY CODE

            Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
            Workbook xlWorkBook = ExcelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            StringBuilder sb = new StringBuilder();
            sb.Append("<html><body>");
            sb.Append("<table style='border: 1px solid black;'>");
            var index = 1;
            sb.Append("<tr>");
            sb.Append("<th colspan='9'>SANJAY MALAKIYA</th>");
            sb.Append("</tr>");
            sb.Append("<tr></tr>");
            sb.Append("<tr>");
            sb.Append("<td colspan='9'><b>SANJAY</td>");
            sb.Append("</tr>");
            sb.Append("<tr>");
            sb.Append("<td style='border: 1px solid black'>SrNO</td>");
            foreach (DataColumn column in dtDataTable1.Columns)
            {
                sb.Append("<td style='border: 1px solid black'>" + column.ColumnName + "</td>");
            }
            sb.Append("</tr>");
            ExcelApp.Windows.Application.ActiveWindow.DisplayGridlines = false;
            StringBuilder objSB = new StringBuilder();

            foreach (DataRow row in dtDataTable1.Rows)
            {
                sb.Append("<tr>");

                sb.Append("<td style='width:100px;border: 1px solid black'>" + index.ToString() + "</td>");
                foreach (DataColumn column in dtDataTable1.Columns)
                {
                    sb.Append("<td style='width:100px;border: 1px solid black'>" + row[column.ColumnName].ToString() + "</td>");
                }
                index++;
                sb.Append("</tr>");
            }
            if (dtDataTable1.Rows.Count > 0)
            {


                ExcelApp.Columns.AutoFit();

            }
            sb.Append("</table>");
            sb.Append("</body></html>");
            String Todaysdate = DateTime.Now.ToString("dd-MM-yyyy");
            if (!Directory.Exists("C:\Users\Krupal\Desktop\" + Todaysdate))
            {
                Directory.CreateDirectory("C:\Users\Krupal\Desktop\" + Todaysdate);
            }
            using (System.IO.StreamWriter file = new System.IO.StreamWriter("C:\Users\Krupal\Desktop\" + Todaysdate + "\Summury_final " + Convert.ToDateTime(cmbmonth.SelectedItem).ToString("dd/MM/yyyy") + ".XLS"))
            {
                file.WriteLine(sb.ToString());
            }
            Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
            Workbook xlWorkBook1 = ExcelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            xlWorkBook1 = Excel.Workbooks.Open("C:\Users\Krupal\Desktop\" + Todaysdate + "\Summury_final " + Convert.ToDateTime(cmbmonth.SelectedItem).ToString("dd/MM/yyyy") + ".XLS", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            Microsoft.Office.Interop.Excel.Worksheet Wt = (Excel.Worksheet)xlWorkBook1.Worksheets.get_Item(1);
            Excel.Visible = true;
How to&Answers:

You can set the date format like below

Range rg = (Excel.Range)worksheetobject.Cells[1,1];
rg.EntireColumn.NumberFormat = "MM/DD/YYYY";