如何将CellFormat应用于单元格

这是我的代码:

public MemoryStream ExportDataTableToExcel(DataTable exportData, string sheetTitle = "Exported Data", bool includeColumnHeaders = true) { uint excelRow = 1; MemoryStream ms = new MemoryStream(); SpreadsheetDocument objSpreadsheet = SpreadsheetDocument.Create(ms, SpreadsheetDocumentType.Workbook); WorkbookPart objWorkbookPart = objSpreadsheet.AddWorkbookPart(); var wsp = objSpreadsheet.WorkbookPart.WorkbookStylesPart; if (wsp == null) { //objSpreadsheet.WorkbookPart.DeletePart(wsp); var stylesPart = objSpreadsheet.WorkbookPart.AddNewPart<WorkbookStylesPart>(); stylesPart.Stylesheet = new Stylesheet(); CellFormat cf = new CellFormat { NumberFormatId = 14, FontId = 0, FillId = 0, BorderId = 0, FormatId = 0, ApplyNumberFormat = BooleanValue.FromBoolean(true) }; stylesPart.Stylesheet.CellFormats = new CellFormats(); stylesPart.Stylesheet.CellFormats.AppendChild<CellFormat>(cf); stylesPart.Stylesheet.CellFormats.Count = UInt32Value.FromUInt32((uint)stylesPart.Stylesheet.CellFormats.ChildElements.Count); ////ss.Save(stylesPart); stylesPart.Stylesheet.Save(); } objWorkbookPart.Workbook = new Workbook(); WorksheetPart objSheetPart = objWorkbookPart.AddNewPart<WorksheetPart>(); objSheetPart.Worksheet = new Worksheet(new SheetData()); Sheets objSheets = objSpreadsheet.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets()); Sheet objSheet = new Sheet(); objSheet.Id = objSpreadsheet.WorkbookPart.GetIdOfPart(objSheetPart); objSheet.SheetId = 1; objSheet.Name = sheetTitle; objSheets.Append(objSheet); if (includeColumnHeaders) { for (int intCol = 0; intCol < exportData.Columns.Count; intCol++) { string excelColumn = this.ConvertIndexToColumnValue(intCol + 1); string headerText = Common.TextTransformations.CapitalizeWords(exportData.Columns[intCol].ColumnName.Replace("_", " ")); Cell objCell = InsertCellInWorksheet(excelColumn, excelRow, objSheetPart); objCell.CellValue = new CellValue(headerText); objCell.DataType = new EnumValue<CellValues>(CellValues.String); } excelRow++; } //Iterate through the row and column and add it to the cell foreach(DataRow row in exportData.Rows) { for (int intCol = 0; intCol < exportData.Columns.Count; intCol++) { string excelColumn = this.ConvertIndexToColumnValue(intCol + 1); Cell objCell = InsertCellInWorksheet(excelColumn, excelRow, objSheetPart); if (exportData.Columns[intCol].DataType.ToString().Trim().ToLower() == "system.datetime") { objCell.DataType = new EnumValue<CellValues>(CellValues.Number); objCell.CellValue = new CellValue(((DateTime)row[intCol]).ToOADate().ToString()); objCell.StyleIndex = 0; // //objCell.StyleIndex = 5; //objCell.CellReference = "A1"; }else{ objCell.CellValue = new CellValue(row[intCol].ToString()); objCell.DataType = new EnumValue<CellValues>(this.GetCellValueFromColumnType(exportData.Columns[intCol])); } } excelRow++; } objSheetPart.Worksheet.Save(); objSpreadsheet.WorkbookPart.Workbook.Save(); objSpreadsheet.Close(); return ms; } 

问题是,生成了xlsx之后,当我尝试打开它时,它说“Microsoft Excel已停止工作”。 我相信我的代码的cellformat部分是错误的,但我不知道如何纠正它。

问题是你没有正确创build样式表。

要创build最简单的样式表,您需要包括字体,填充和边框。 即使你不使用它们,它们也是强制性的。 而在你的代码中,虽然你已经使用了FontId,FillId和BorderId的索引,但是你并没有真正定义它们。

  // Where are these coming from ??? FontId = 0, FillId = 0, BorderId = 0, 

说我提供了下面的代码片断,这将创build最简单的样式表,

 WorkbookStylesPart stylesheet = spreadsheet.WorkbookPart.AddNewPart<WorkbookStylesPart>(); Stylesheet workbookstylesheet = new Stylesheet(); // <Fonts> Font font0 = new Font(); // Default font : Id =0 /* An example for another font Font font1 = new Font(); // Bold font Bold bold = new Bold(); font1.Append(bold); */ Fonts fonts = new Fonts(); // <APENDING Fonts> fonts.Append(font0); //fonts.Append(font1); // <= Bold font // <Fills> Fill fill0 = new Fill(); // Default fill : Id = 0 Fills fills = new Fills(); // <APENDING Fills> fills.Append(fill0); // <Borders> Border border0 = new Border(); // Defualt border : Id = 0 Borders borders = new Borders(); // <APENDING Borders> borders.Append(border0); // <CellFormats> : Now you can simple use above defined ID's CellFormat cellformat0 = new CellFormat() { FormatId = 0, FillId = 0, BorderId = 0 }; // <APENDING CellFormats> CellFormats cellformats = new CellFormats(); cellformats.Append(cellformat0); // Append FONTS, FILLS , BORDERS & CellFormats to stylesheet <Preserve the ORDER> workbookstylesheet.Append(fonts); workbookstylesheet.Append(fills); workbookstylesheet.Append(borders); workbookstylesheet.Append(cellformats); stylesheet.Stylesheet = workbookstylesheet; stylesheet.Stylesheet.Save(); 

是的,它有点冗长,但只有一个时间代码,你将永远使用..