如何使用 open xml C# 在 Excel 中禁用网格线? [英] How to disable gridlines in Excel using open xml C#?

查看:2

问题描述

我想在 excel 中禁用 GridLines,并使用 C# 中的 open xml 将自定义边框放置到 excel 单元格

I want to disable GridLines in excel and put custom borders to excel cells using open xml in C#

我尝试过使用下面的代码,但是当我打开 Excel 时抛出异常,例外情况是已修复部件:/xl/worksheets/sheet.xml 部件存在 XML 错误.加载错误.第 1 行,第 0 列."

I have tried with below code but is throwing exception when i open the excell, the exception is "Repaired Part: /xl/worksheets/sheet.xml part with XML error. Load error. Line 1, column 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 属性是必需的属性/属性.试试这个:

The WorkbookViewId property of the SheetView class is a required attribute/property. Try this:

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

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

That uses the 1st (default) workbook view. Don't worry, you don't have to explicitly create a WorkbookView child of the BookViews class, which is a child of Workbook. Unless you want to, of course. :)

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆