Quantcast
Channel: ClosedXML - The easy way to OpenXML
Viewing all articles
Browse latest Browse all 1877

Commented Unassigned: cell.DataType = cellValues.Date does not work!! Using built-in NumberFormatId=15 displays only the day part of entire date. [9339]

$
0
0
i am trying to export a datatable to excel ..for columns with int datatype changing cell.DataType= cellvalues.Numbers works . but cell.DataType= cellvalues.Date does not work.

Plus using custom formatting and setting NumberFormatId=15 (built in format) displays only the date part of entire date i.e 14-Mar-2014 is displayed as 14.

Comments: //Add content from Datatable to SpreadSheet Document foreach (DataRow dr in dt.Rows) { Row row1 = new Row() { RowIndex = ++rowindex }; foreach (string col in columnHeaders) { string rowtext = dr[col].ToString(); Cell cell = new Cell(); cell.CellValue = new CellValue(rowtext); if (col.Value == "DateTime") { cell.StyleIndex = 1; } row1.Append(cell); } sheetdata1.Append(row1); } This is my code. For column with Dates i have set styleIndex of 1. Below is piece of code where cell format is specified. NumberingFormats nfs = new NumberingFormats(); CellFormats cfs = new CellFormats(); CellFormat cf; cf = new CellFormat(); cf.NumberFormatId = 0; cf.FontId = 0; cf.FillId = 0; cf.BorderId = 0; cf.FormatId = 0; cfs.Append(cf); NumberingFormat nfDateTime = new NumberingFormat(); nfDateTime.NumberFormatId = 15; nfs.Append(nfDateTime); // index 1 cf = new CellFormat(); cf.ApplyNumberFormat = true; cf.NumberFormatId = nfDateTime.NumberFormatId; cfs.Append(cf); nfs.Count = UInt32Value.FromUInt32((uint)nfs.ChildElements.Count); cfs.Count = UInt32Value.FromUInt32((uint)cfs.ChildElements.Count); ss.Append(nfs); ss.Append(cfs); Now when i run this and open the generated excel file it gives a msg of unreadable content. When i click on Yes for recovery, the date column in excel file contains only the day part. e.g 14-Apr-2014 is displayed as 14.

Viewing all articles
Browse latest Browse all 1877

Trending Articles