我目前正在为我们的应用程序开发一个组件,它允许我们生成Excel工作表,而客户端不必拥有Excel。因此开始考虑Open XML。
目前无效的是解析日期。
这是我的工作表:
<?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>
这是我的样式表:
<?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>
这是我用来获取日期的代码。
cell.StyleIndex = 0; .
string columnValue = date.ToOADate().ToString().Replace(",", ".");
//string columnValue = date.ToOADate().ToString().
Replace(CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator, ".");
cell.CellValue = new CellValue(columnValue);
结果不是我想要的,我将从我创建的样式表中的日期而不是日期中获取双精度值。
我得到这个40651,67778而不是18-4-2011 16:16
答案 0 :(得分:0)
看起来你的styles.xml样式表缺少cellStyles
元素。我写了一些代码来创建一个类似于你的文件,没有那个元素就没有应用样式。我的整个元素看起来像这样:
<x:cellStyles count="1"><x:cellStyle xfId="0" builtinId="0" /></x:cellStyles>
我用来生成文件的代码如下。注意我只输出一个日期单元格,但添加其他单元格是微不足道的。
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();
为此工作表生成的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>
由此为样式表生成的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>
我在我的单元格上输出t="d"
,表示该单元格是日期,但我不认为这是必需的。
可以在http://polymathprogrammer.com/2009/11/09/how-to-create-stylesheet-in-excel-open-xml/找到样式表(以及其他OpenXML元素)的优秀资源