C#在Excel中禁用网格线

C#在Excel中禁用网格线

本文介绍了如何使用open xml C#在Excel中禁用网格线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我尝试使用下面的代码,但是当我打开时抛出异常excel,
的异常是修复的部分:/xl/worksheets/sheet.xml部分与XML错误。加载错误。第1行,列0。

  using(SpreadsheetDocument xl = SpreadsheetDocument.Create(sFile,SpreadsheetDocumentType.Workbook))
{
WorkbookPart wbp = xl.AddWorkbookPart();
WorksheetPart wsp = wbp.AddNewPart< WorksheetPart>();
工作簿wb = new Workbook();
FileVersion fv = new FileVersion();
fv.ApplicationName =Microsoft Office Excel;
工作表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>();
////添加样式到
wbsp.Stylesheet = CreateStylesheet();
wbsp.Stylesheet.Save();
////添加样式到表
////wbsp.Stylesheet = GenerateStyleSheet();


//wbsp.Stylesheet.Save();
列列= new Columns();
columns.Append(CreateColumnData(1,1,25));
ws.Append(columns);

////生成行
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属性是必需属性/属性。尝试这样:

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

使用第一(默认)工作簿视图。不要担心,您不必显式创建BookViews类的WorkbookView子项,该类是Workbook的子代。当然,除非你想要。 :)


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();
解决方案

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

这篇关于如何使用open xml C#在Excel中禁用网格线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:31