gpt4 book ai didi

c# - 设置 Excel 电子表格列属性问题

转载 作者:太空狗 更新时间:2023-10-29 19:53:53 26 4
gpt4 key购买 nike

我正在尝试使用 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”,但结果是一样的

有没有人有观点如何解决这个问题?

最佳答案

好的。原因是 SheetFormatProperties 对象具有 required 属性 defaultRowHeight。您甚至可以指定 defaultRowHeight = 0,但它必须存在。所以使用

SheetFormatProperties sheetFormatProperties1 = new SheetFormatProperties(){ DefaultColumnWidth = 12.75D, DefaultRowHeight = 0D};

快乐=) Слава Украине!

关于c# - 设置 Excel 电子表格列属性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13120146/

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