Office打开XML日期不工作

本文关键字:工作 日期 XML 打开 Office | 更新日期: 2023-09-27 18:13:17

我目前正在为我们的应用程序开发一个组件,使我们能够生成一个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

Office打开XML日期不工作

看起来您的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元素)