Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
998 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...