How to disable gridlines in Excel using open xml C#?

老子叫甜甜 提交于 2019-12-01 07:52:58

问题


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

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

回答1:


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

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. :)




回答2:


I tried this way but it was failing but able to identify what is missing. By default when creating a spreadsheet from scratch, there is no default workbook view. So need to create new workbook view.

spreadSheet.WorkbookPart.Workbook = new Workbook(new BookViews(new WorkbookView()));

Then create sheet view.

worksheetPart.Worksheet = new Worksheet(new SheetViews(new SheetView(){WorkbookViewId=0,ShowGridLines=new BooleanValue(false)}), new SheetData());

NOTE: When create columns in the excel workbook, the elements should be created in a sequence. The sequence is

  • Sheet Views
  • Sheet View Formatting
  • Columns
  • Sheet Data

Enjoy Open XML SDK but Microsoft does not provide very powerfull documentation.



来源:https://stackoverflow.com/questions/14230538/how-to-disable-gridlines-in-excel-using-open-xml-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!