I want to export data to a excel file in a windows form application but when I try to export dates the format changes. I want to use the format dd-MM-yyyy
Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("Excel is not properly installed!!");
return;
}
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1].NumberFormat = "dd/mm/yyyy";
xlWorkSheet.Cells[1, 1] = "19-02-2015";
xlWorkSheet.Cells[2, 1] = "02-03-2015";
xlWorkSheet.Cells[2, 1].NumberFormat = "dd/mm/yyyy";
xlWorkBook.SaveAs("C:\\Users\\user\\Desktop\\Data.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
the result in the excel file is: 19-2-2015 as text 02/03/2015 as datetime I use excel 2013
so how to export dates where the day is above 12 to excel and give them the format mm/dd/jjjj