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

后端 未结 2 363
不思量自难忘°
不思量自难忘° 2021-01-14 18:41

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 th

相关标签:
2条回答
  • 2021-01-14 19:04

    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.

    0 讨论(0)
  • 2021-01-14 19:18

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

    0 讨论(0)
提交回复
热议问题