Excel中的单元格的date格式OpenXml

我通过OpenXML SDK 2.5创buildexcel文件。 我有IDIsposable类,其中包括一个variables

private SpreadsheetDocument ProcessDocument { get; set; } 

我可以插入一个单元格的date值:

 var cell = GetCell(sheet, columnName, rowIndex); //method return a cell cell.DataType = new EnumValue<CellValues>(CellValues.Number); cell.CellValue = new CellValue(dateResult.ToOADate().ToString(CultureInfo.InvariantCulture)); cell.StyleIndex = 0; 

在这种状态下,当我打开一个创build的单元格中的Excel文件,我看到一个数字。 这是好的,我的意思是,但现在我需要设置单元格格式…我有一个方法:

 public virtual UInt32 CreateCellFormat(String format) { NumberingFormat nf = new NumberingFormat(); nf.FormatCode = format; if (this.WorkbookStylesPart.Stylesheet.NumberingFormats == null) { this.WorkbookStylesPart.Stylesheet.NumberingFormats = new NumberingFormats(); this.WorkbookStylesPart.Stylesheet.NumberingFormats.AppendChild(new NumberingFormats(new NumberingFormat() { NumberFormatId = 0 })); this.WorkbookStylesPart.Stylesheet.NumberingFormats.Count = 1; } var numberFormatId = this.WorkbookStylesPart.Stylesheet.NumberingFormats.Elements<NumberingFormat>().Where(a => a.NumberFormatId.HasValue).Select(a => a.NumberFormatId).Max(); nf.NumberFormatId = numberFormatId == null || numberFormatId < 199 ? 200 : numberFormatId + 1; this.WorkbookStylesPart.Stylesheet.NumberingFormats.InsertAt(nf, (int)this.WorkbookStylesPart.Stylesheet.NumberingFormats.Count.Value); this.WorkbookStylesPart.Stylesheet.NumberingFormats.Count++; return nf.NumberFormatId; } public virtual void SetCellStyle(Sheet sheet, UInt32 rowIndex, String columnName, UInt32? fontIndex, UInt32? fillIndex, UInt32? formatIndex) { var cell1 = this.GetCell(sheet, columnName, rowIndex); cell1.StyleIndex = this.CreateCellFormat(sheet, fontIndex, fillIndex, formatIndex); } internal virtual UInt32 CreateCellFormat(Sheet sheet, UInt32? fontIndex, UInt32? fillIndex, UInt32? formatIndex) { CellFormat cellFormat = new CellFormat(); WorksheetPart workSheetPart = this.GetWorkSheetPart(sheet.Name); if (fontIndex.HasValue) { cellFormat.FontId = fontIndex; cellFormat.ApplyFont = true; } if (fillIndex.HasValue) { cellFormat.FillId = fillIndex; cellFormat.ApplyFill = true; } if (formatIndex.HasValue) { cellFormat.NumberFormatId = formatIndex; cellFormat.ApplyNumberFormat = BooleanValue.FromBoolean(true); } if (this.ProcessDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats == null) { this.ProcessDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats = new CellFormats(); this.ProcessDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.Append(new CellFormat()); this.ProcessDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.Count = 1; } this.ProcessDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.InsertAt(cellFormat, (int)this.ProcessDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.Count.Value); UInt32 result = this.ProcessDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.Count; this.ProcessDocument.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.Count++; return result; } 

在创build过程中,我使用它:

 var formatIndex = excel.CreateCellFormat("dd.mm.yyyy"); excel.AddCellValue(workSheet, row, 6, DateTime.Now); excel.SetCellStyle(workSheet, row, 6, null, null, formatIndex); 

此时,当我打开生成的excel时,我收到以下消息: “Excel在'test.xlsx'中发现了不可读的内容。 你想恢复这个工作簿的内容? 如果您信任此工作簿的来源,请单击“是”。

修复我的excel报告文件“/xl/styles.xml”中的错误。

有没有人知道我在创build单元格格式时犯了什么错误? 非常感谢