gpt4 book ai didi

excel - 如何使用 open xml C# 禁用 Excel 中的网格线?

转载 作者:行者123 更新时间:2023-12-02 15:35:35 25 4
gpt4 key购买 nike

我想在 Excel 中禁用网格线,并使用 C# 中的 open xml 将自定义边框添加到 Excel 单元格

我尝试过下面的代码,但当我打开 Excel 时抛出异常,异常(exception)情况是“已修复部分:/xl/worksheets/sheet.xml 部分存在 XML 错误。加载错误。第 1 行,第 0 列。”

                using (SpreadsheetDocument xl = SpreadsheetDocument.Create(sFile, SpreadsheetDocumentType.Workbook))
{
WorkbookPart wbp = xl.AddWorkbookPart();
WorksheetPart wsp = wbp.AddNewPart<WorksheetPart>();
Workbook wb = new Workbook();
FileVersion fv = new FileVersion();
fv.ApplicationName = "Microsoft Office Excel";
Worksheet ws = new Worksheet();
SheetViews sheetViews = new SheetViews();

SheetView sheetView = new SheetView();
sheetView.ShowGridLines = new BooleanValue(false);
sheetViews.Append(sheetView);
ws.Append(sheetViews);

WorkbookStylesPart wbsp = wbp.AddNewPart<WorkbookStylesPart>();
//// add styles to sheet
wbsp.Stylesheet = CreateStylesheet();
wbsp.Stylesheet.Save();
//// add styles to sheet
////wbsp.Stylesheet = GenerateStyleSheet();


//wbsp.Stylesheet.Save();
Columns columns = new Columns();
columns.Append(CreateColumnData(1, 1, 25));
ws.Append(columns);

//// generate rows
SheetData sd = CreateSheetData(products);
ws.Append(sd);
wsp.Worksheet = ws;
wsp.Worksheet.Save();

MERGEiNITIALcELLS(wsp);

wb.Append(fv);
CreateSheet(wbp, wsp, wb);
xl.WorkbookPart.Workbook = wb;
xl.WorkbookPart.Workbook.Save();


xl.Close();

最佳答案

SheetView 类的 WorkbookViewId 属性是必需的特性/属性。试试这个:

SheetView sheetView = new SheetView();
sheetView.ShowGridLines = new BooleanValue(false);
sheetView.WorkbookViewId = 0;
sheetViews.Append(sheetView);
ws.Append(sheetViews);

它使用第一个(默认)工作簿 View 。不用担心,您不必显式创建 BookViews 类的 WorkbookView 子级,该类是 Workbook 的子级。当然,除非你愿意。 :)

关于excel - 如何使用 open xml C# 禁用 Excel 中的网格线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14230538/

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