我正在尝试使用 OpenXML 框架在 Excel 电子表格中设置默认列宽,结果文件损坏。这是代码
private void initSpreadsheetDocument()
{
// Add a WorkbookPart to the spreadsheet document.
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
var sheetData = new SheetData();
var properties = new SheetFormatProperties { DefaultColumnWidth = 25D };
Worksheet worksheet = new Worksheet();
worksheet.AppendChild(sheetData);
// here is line of code that corrupt file
// without it - file is being generated properly
worksheet.AppendChild(properties);
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = worksheet;
// Init sheets
sheetsStorage = new Sheets();
defaultSheet = new Sheet();
defaultSheet.Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart);
defaultSheet.SheetId = 1;
defaultSheet.Name = DEFAULT_SHEET_NAME;
sheetsStorage.AppendChild(defaultSheet);
spreadsheetDocument.WorkbookPart.Workbook.AppendChild(sheetsStorage);
spreadsheetDocument.WorkbookPart.Workbook.Save();
}
我试图使用“Append”与“AppendChild”,但结果是一样的
有没有人有意见如何解决?