I am using the following code to add a DateTime
to a column in my spreadsheet:
var dt = DateTime.Now;
r.AppendChild<Cell>(new Cell()
{
CellValue = new CellValue(dt.ToOADate().ToString()),
DataType = new EnumValue<CellValues>(CellValues.Date),
StyleIndex = 1,
CellReference = header[6] + index
});
When I try to open the file in Excel 2010, I get the error
Excel found unreadable content in file.xlsx
All is fine if I comment out the line.
I have referred to similar questions on StackOverflow, but they basically have the same code as I do.
You can use the GetCellValue method to retrieve the value of a cell in a workbook. The method requires the following three parameters: A string that contains the name of the document to examine.
Late to the party as usual but I have to post an answer because ALL of the previous ones are completely wrong except for Oleh's down voted answer which was sadly incomplete.
As the question is related to Excel, the easiest thing to do is create an Excel spread sheet with the data and style you want, then open it as parts and look at the raw XML.
Adding the date 01/01/2015 into cell A1 results in the following:
<row r="1">
<c r="A1" s="0">
<v>42005</v>
</c>
</row>
Note that the type attribute is not there. However there is a style attribute referencing the following style:
<xf numFmtId="14" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1" />
That is the most basic style that you must add.
So code to generate the above:
var CellFormats = new CellFormats();
CellFormats.Append(new CellFormat()
{
BorderId = 0,
FillId = 0,
FontId = 0,
NumberFormatId = 14,
FormatId = 0,
ApplyNumberFormat = true
});
CellFormats.Count = (uint)CellFormats.ChildElements.Count;
var StyleSheet = new Stylesheet();
StyleSheet.Append(CellFormats);
The NumberFormatId = 14
refers to the built-in format mm-dd-yy
, here's a list of some other formats.
Unfortunately it seems that adding just the above style is not quite enough and if you do it actually causes Excel to crash. Note that BorderId
, FillId
, FontId
need to correspond to an item in the style sheet which means you need to provide them. The GetStyleSheet()
method in the complete code listing provides the minimum default stylesheet required for Excel to work without errors.
SheetData.AppendChild(new Row(
new Cell()
{
// CellValue is set to OADate because that's what Excel expects.
CellValue = new CellValue(date.ToOADate().ToString(CultureInfo.InvariantCulture)),
// Style index set to style (0 based).
StyleIndex = 0
}));
Note: Office 2010 and 2013 can handle dates differently but by default it seems they do not.
They provide support for dates in ISO 8601 format i.e. yyyy-MM-ddTHH:mm:ss
just so happens that this is also standard format sortable ("s") so you can do:
SheetData.AppendChild(new Row(
new Cell()
{
CellValue = new CellValue(date.ToString("s")),
// This time we do add the DataType attribute but ONLY for Office 2010+.
DataType = CellValues.Date
StyleIndex = 1
}));
The result:
<row>
<c s="0" t="d">
<v>2015-08-05T11:13:57</v>
</c>
</row>
Below is an example of the minimum code required to add a cell with date format.
private static void TestExcel()
{
using (var Spreadsheet = SpreadsheetDocument.Create("C:\\Example.xlsx", SpreadsheetDocumentType.Workbook))
{
// Create workbook.
var WorkbookPart = Spreadsheet.AddWorkbookPart();
var Workbook = WorkbookPart.Workbook = new Workbook();
// Add Stylesheet.
var WorkbookStylesPart = WorkbookPart.AddNewPart<WorkbookStylesPart>();
WorkbookStylesPart.Stylesheet = GetStylesheet();
WorkbookStylesPart.Stylesheet.Save();
// Create worksheet.
var WorksheetPart = Spreadsheet.WorkbookPart.AddNewPart<WorksheetPart>();
var Worksheet = WorksheetPart.Worksheet = new Worksheet();
// Add data to worksheet.
var SheetData = Worksheet.AppendChild(new SheetData());
SheetData.AppendChild(new Row(
new Cell() { CellValue = new CellValue(DateTime.Today.ToOADate().ToString(CultureInfo.InvariantCulture)), StyleIndex = 1 },
// Only works for Office 2010+.
new Cell() { CellValue = new CellValue(DateTime.Today.ToString("s")), DataType = CellValues.Date, StyleIndex = 1 }));
// Link worksheet to workbook.
var Sheets = Workbook.AppendChild(new Sheets());
Sheets.AppendChild(new Sheet()
{
Id = WorkbookPart.GetIdOfPart(WorksheetPart),
SheetId = (uint)(Sheets.Count() + 1),
Name = "Example"
});
Workbook.Save();
}
}
private static Stylesheet GetStylesheet()
{
var StyleSheet = new Stylesheet();
// Create "fonts" node.
var Fonts = new Fonts();
Fonts.Append(new Font()
{
FontName = new FontName() { Val = "Calibri" },
FontSize = new FontSize() { Val = 11 },
FontFamilyNumbering = new FontFamilyNumbering() { Val = 2 },
});
Fonts.Count = (uint)Fonts.ChildElements.Count;
// Create "fills" node.
var Fills = new Fills();
Fills.Append(new Fill()
{
PatternFill = new PatternFill() { PatternType = PatternValues.None }
});
Fills.Append(new Fill()
{
PatternFill = new PatternFill() { PatternType = PatternValues.Gray125 }
});
Fills.Count = (uint)Fills.ChildElements.Count;
// Create "borders" node.
var Borders = new Borders();
Borders.Append(new Border()
{
LeftBorder = new LeftBorder(),
RightBorder = new RightBorder(),
TopBorder = new TopBorder(),
BottomBorder = new BottomBorder(),
DiagonalBorder = new DiagonalBorder()
});
Borders.Count = (uint)Borders.ChildElements.Count;
// Create "cellStyleXfs" node.
var CellStyleFormats = new CellStyleFormats();
CellStyleFormats.Append(new CellFormat()
{
NumberFormatId = 0,
FontId = 0,
FillId = 0,
BorderId = 0
});
CellStyleFormats.Count = (uint)CellStyleFormats.ChildElements.Count;
// Create "cellXfs" node.
var CellFormats = new CellFormats();
// A default style that works for everything but DateTime
CellFormats.Append(new CellFormat()
{
BorderId = 0,
FillId = 0,
FontId = 0,
NumberFormatId = 0,
FormatId = 0,
ApplyNumberFormat = true
});
// A style that works for DateTime (just the date)
CellFormats.Append(new CellFormat()
{
BorderId = 0,
FillId = 0,
FontId = 0,
NumberFormatId = 14, // or 22 to include the time
FormatId = 0,
ApplyNumberFormat = true
});
CellFormats.Count = (uint)CellFormats.ChildElements.Count;
// Create "cellStyles" node.
var CellStyles = new CellStyles();
CellStyles.Append(new CellStyle()
{
Name = "Normal",
FormatId = 0,
BuiltinId = 0
});
CellStyles.Count = (uint)CellStyles.ChildElements.Count;
// Append all nodes in order.
StyleSheet.Append(Fonts);
StyleSheet.Append(Fills);
StyleSheet.Append(Borders);
StyleSheet.Append(CellStyleFormats);
StyleSheet.Append(CellFormats);
StyleSheet.Append(CellStyles);
return StyleSheet;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With