gpt4 book ai didi

c# - 如何将 CellFormat 应用于单元格

转载 作者:太空宇宙 更新时间:2023-11-03 23:41:42 24 4
gpt4 key购买 nike

这是我的代码:

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 部分是错误的,但我只是不知道如何更正它。

最佳答案

问题是您没有正确创建样式表。

要创建最简单的样式表,您需要包括字体、填充和边框。即使您不使用它们,它们也是强制性的。在您的代码中,尽管您使用了 FontId、FillId 和 BorderId 的索引,但您实际上并未定义它们。

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

说我提供了下面的代码片段,可以创建最简单的样式表,

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();

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

关于c# - 如何将 CellFormat 应用于单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28838802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com