[英]Office Open XML Date not working
I'm currently developing an component for our application that allows us to generate an Excel sheet without the clients having to own Excel. 我目前正在为我们的应用程序开发一个组件,该组件使我们能够生成Excel工作表,而客户不必拥有Excel。 So Open XML came to mind. 因此想到了Open XML。
What is not working at the moment is parsing of dates. 目前不起作用的是解析日期。
This is my worksheet: 这是我的工作表:
<?xml version="1.0" encoding="UTF-8"?>
<x:worksheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<x:cols>
<x:col min="1" max="1" width="42.42" customWidth="1" />
<x:col min="2" max="2" width="42.56" customWidth="1" />
<x:col min="3" max="3" width="16.27" customWidth="1" />
</x:cols>
<x:sheetData>
<x:row r="1">
<x:c r="A1">
<x:v>FF kijken hoe dit werkt snap er geen fu** van</x:v>
</x:c>
<x:c r="B1">
<x:v>This is some really, really long text to display.</x:v>
</x:c>
<x:c r="C1" s="0">
<x:v>40651.6777777778</x:v>
</x:c>
</x:row>
</x:sheetData>
</x:worksheet>
This is my stylesheet: 这是我的样式表:
<?xml version="1.0" encoding="UTF-8"?>
<x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<x:numFmts count="1">
<x:numFmt numFmtId="164" formatCode="dd-mm-yy hh:mm" />
</x:numFmts>
<x:fonts count="1">
<x:font>
<x:sz val="11" />
<x:name val="Arial" />
</x:font>
</x:fonts>
<x:cellXfs count="1">
<x:xf numFmtId="164" fontId="0" fillId="0" borderId="0" xfId="0"` applyNumberFormat="1" />
</x:cellXfs>
</x:styleSheet>
This is the code I use to get my dates. 这是我用来获取日期的代码。
cell.StyleIndex = 0; .
string columnValue = date.ToOADate().ToString().Replace(",", ".");
//string columnValue = date.ToOADate().ToString().
Replace(CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator, ".");
cell.CellValue = new CellValue(columnValue);
The result is not what I want I'll get the double value from the date not the date in the stylesheet I created. 结果不是我想要的,我将从日期而不是我创建的样式表中的日期获取double值。
I'll get this 40651,67778 instead of 18-4-2011 16:16 我将获得此40651,67778而不是18-4-2011 16:16
It looks like your styles.xml stylesheet is missing the cellStyles
element. 您的styles.xml样式表似乎缺少cellStyles
元素。 I wrote some code to create a file similar to yours and without that element the style was not applied. 我写了一些代码来创建一个与您的文件相似的文件,没有该元素,样式就没有应用。 The whole element on mine looks like this: 我的整个元素看起来像这样:
<x:cellStyles count="1"><x:cellStyle xfId="0" builtinId="0" /></x:cellStyles>
The code I used to generate the file is below. 我用来生成文件的代码如下。 Note I only output a single date cell but adding the other cells is trivial. 注意我只输出一个日期单元格,但添加其他单元格是微不足道的。
string fileName = @"D:\Test\formatting.xlsx";
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook);
// Add a WorkbookPart to the document.
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
Stylesheet styleSheet = new Stylesheet();
uint iExcelIndex = 164;
CellFormats cfs = new CellFormats();
NumberingFormats nfs = new NumberingFormats();
NumberingFormat nf;
nf = new NumberingFormat();
nf.NumberFormatId = iExcelIndex++;
nf.FormatCode = "dd-mm-yyyy hh:mm:ss";
nfs.Append(nf);
CellFormat cf = new CellFormat();
cf.NumberFormatId = nf.NumberFormatId;
cf.FontId = 0;
cf.FillId = 0;
cf.BorderId = 0;
cf.FormatId = 0;
cf.ApplyNumberFormat = true;
cfs.Append(cf);
styleSheet.CellFormats = cfs;
styleSheet.NumberingFormats = nfs;
styleSheet.Borders = new Borders();
Border border = new Border();
styleSheet.Borders.Append(border);
styleSheet.Fills = new Fills();
Fill fill = new Fill();
styleSheet.Fills.Append(fill);
styleSheet.Fonts = new Fonts();
Font font = new Font();
styleSheet.Fonts.Append(font);
// **** This code is the code I think you are missing: ****
CellStyles css = new CellStyles();
CellStyle cs = new CellStyle();
cs.FormatId = 0;
cs.BuiltinId = 0;
css.Append(cs);
css.Count = UInt32Value.FromUInt32((uint)css.ChildElements.Count);
styleSheet.Append(css);
//**** end of your suspected missing code ****
workbookpart.Workbook = new Workbook();
workbookpart.AddNewPart<WorkbookStylesPart>();
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// Add Sheets to the Workbook.
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
// Append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet()
{
Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
SheetId = 1,
Name = "mySheet"
};
sheets.Append(sheet);
Worksheet worksheet = new Worksheet();
SheetData sheetData = new SheetData();
Row row = new Row();
Cell cell = new Cell();
cell.StyleIndex = 0;
cell.DataType = CellValues.Date;
string columnValue = DateTime.FromOADate(40651.67778).ToOADate().ToString().Replace(",", ".");
cell.CellValue = new CellValue(columnValue);
row.Append(cell);
sheetData.Append(row);
worksheet.Append(sheetData);
worksheetPart.Worksheet = worksheet;
workbookpart.WorkbookStylesPart.Stylesheet = styleSheet;
// Close the document.
spreadsheetDocument.Close();
The XML generated from this for the worksheet was: 由此为工作表生成的XML为:
<?xml version="1.0" encoding="utf-8"?>
<x:worksheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<x:sheetData>
<x:row>
<x:c s="0" t="d">
<x:v>40651.67778</x:v>
</x:c>
</x:row>
</x:sheetData>
</x:worksheet>
And the XML generated from this for the stylesheet was: 从中为样式表生成的XML为:
<?xml version="1.0" encoding="utf-8"?>
<x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<x:numFmts>
<x:numFmt numFmtId="164" formatCode="dd-mm-yyyy hh:mm:ss" />
</x:numFmts>
<x:fonts>
<x:font />
</x:fonts>
<x:fills>
<x:fill />
</x:fills>
<x:borders>
<x:border />
</x:borders>
<x:cellXfs>
<x:xf numFmtId="164" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1" />
</x:cellXfs>
<x:cellStyles count="1">
<x:cellStyle xfId="0" builtinId="0" />
</x:cellStyles>
</x:styleSheet>
I output t="d"
on my cell which denotes that the cell is a date but I don't think this is required. 我在单元格上输出t="d"
,这表示该单元格是日期,但我认为这不是必需的。
A great resource for stylesheets (among other OpenXML elements) can be found at http://polymathprogrammer.com/2009/11/09/how-to-create-stylesheet-in-excel-open-xml/ 可以在http://polymathprogrammer.com/2009/11/09/how-to-create-stylesheet-in-excel-open-xml/中找到有关样式表(以及其他OpenXML元素)的重要资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.