Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
994 views
in Technique[技术] by (71.8m points)

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

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...