I think i found a problem with DateTime Cells and Excel2007 workbook format. This problem occur only if you reWrite data on an existing worksheet. In this case the date format is lost and you have to select all datetime cells on Excel to set their format to Date and get the right date format.
Writing same data in a new worksheet doesn't lost the date format.
if the worsheet exist :
workBook.Worksheets.Remove(workBook.Worksheets[worksheetName]);worksheet = workBook.Worksheets.Add(worksheetName);
Initialize all cells from a database, the datetime format is lost
If the worksheet doesn't existworksheet = workBook.Worksheets.Add(worksheetName);
The datetime format is ok
This problem doesn't occur for Excel2003 format.
Best regards
Florent, French
Hi Mike,
Please find a sample exemple code which try to explain you which kind of problem i found.This problem occur only for 2007 Excel format, never for Excel 2003 format. Also it occur only if you export in an existing excel file.
DataTable data = new DataTable();// data is filled from a database. First column is DateTime format// first time , excel file and worksheetName doesn't existworkBook = new Workbook();worksheet = workBook.Worksheets.Add(worksheetName);foreach (DataRow datarow in data.Rows) { for (int columnIndex = 0; columnIndex < datarow.ItemArray.Length; columnIndex++) { row.Cells[columnIndex].Value = datarow[columnIndex]; } }// Save xlsx excel file// --------------------- // Export DateTime column is ok, it has the right format like dd/mm/yyyy (French culture)// ---------------------workBook.Save(xlsxFile);// Second time excel file and worksheetName existworkBook = Workbook.Load(xlsFile);
// Remove the worksheetNameworkBook.Worksheets.Remove(workBook.Worksheets[worksheetName]);
worksheet = workBook.Worksheets.Add(worksheetName);foreach (DataRow datarow in data.Rows) { for (int columnIndex = 0; columnIndex < datarow.ItemArray.Length; columnIndex++) { row.Cells[columnIndex].Value = datarow[columnIndex]; } }// Save xlsx excel file// --------------------- // Now, export DateTime column is ok but it has a wrong format like 39976// ---------------------workBook.Save(xlsxFile);
Hi Florent,
I'm not sure I understand exactly what you are describing. Can you post a small sample project and give me some steps I can follow in order to duplicate the problem?
Hello,
Is there anybody who can reproduce this problem. Maybe my description is not very clear as i am a French Guy...
Regards
Florent.