SheetViews为null,试图冻结行openXML

时间:2017-09-06 11:41:26

标签: c# openxml

尝试引用此链接:

https://social.msdn.microsoft.com/Forums/office/en-US/e1c08add-b610-44c9-b60e-fa8ef6c24978/openxmlexcelc?forum=oxmlsdk

我在这一行得到了一个sheetview空指针异常:

SheetView sw = wsp.Worksheet.SheetViews.FirstOrDefault()asSheetView;

我在创建电子表格时没有使用任何资料查看,我正在使用内存流传回使用。

使用此代码:

我如何冻结前两行?谢谢!

MemoryStream ms = new MemoryStream();
        SpreadsheetDocument xl = SpreadsheetDocument.Create(ms, 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();

        SheetData sd = new SheetData();

        WorkbookStylesPart stylesPart = xl.WorkbookPart.AddNewPart<WorkbookStylesPart>();
        stylesPart.Stylesheet = GenerateStyleSheet();
        stylesPart.Stylesheet.Save();




        //create header
        uint colHeaderIndex = 1;
        Row r1 = new Row() { RowIndex = colHeaderIndex };
        foreach (System.ComponentModel.PropertyDescriptor descriptor in System.ComponentModel.TypeDescriptor.GetProperties(list[0]))
        {
            Cell headerCell = new Cell();
            headerCell.DataType = CellValues.String;
            headerCell.CellValue = new CellValue(descriptor.DisplayName);
            r1.Append(headerCell);
        }
        //append header row to sheet
        sd.Append(r1);


        //now fill the rest of the rows with data
        uint rowIndex = 1; //Start at 1, since we reserved index 1 for the heading. The indexer will increase its value by 1 before it is used.  
        for (int i = 0; i < list.Count; i++)
        {
            uint colIndex = 1;
            Row row = new Row() { RowIndex = ++rowIndex };
            foreach (System.ComponentModel.PropertyDescriptor descriptor in System.ComponentModel.TypeDescriptor.GetProperties(list[i]))
            {
                Cell cell = new Cell();
                cell.CellReference = rowIndex + " x " + colIndex;
                cell.DataType = CellValues.String;
                cell.StyleIndex = 1;
                var val = descriptor.GetValue(list[i]);
                cell.CellValue = new CellValue(val != null ? val.ToString() : "");
                row.Append(cell);
            }

            //append data row to sheet
            sd.Append(row);
        }

        ws.Append(sd);
        wsp.Worksheet = ws;
        wsp.Worksheet.Save();
        Sheets sheets = new Sheets();
        Sheet sheet = new Sheet();
        sheet.Name = "first sheet";
        sheet.SheetId = 1; //we will only have one sheet for now, unless we are maxing out on sheets then we can create new ones
        sheet.Id = wbp.GetIdOfPart(wsp);
        sheets.Append(sheet);
        wb.Append(fv);
        wb.Append(sheets);
        freezeHeader(wbp, wsp);
        xl.WorkbookPart.Workbook = wb;
        xl.WorkbookPart.Workbook.Save();

        xl.Close();

        Response.Clear();
        byte[] dt = ms.ToArray();

        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", fileName));
        Response.BinaryWrite(dt);
        Response.End();

1 个答案:

答案 0 :(得分:1)

通过使用开放的XML SDK 2.0生产力工具来解决这个问题。显然,附加到工作表的顺序是一个大问题。所以我确实需要创建一个新的SheetViews列表和SheetView,但SheetViews列表需要在数据表之前附加到工作表。

所以添加,

    SheetViews sheetViews1 = new SheetViews();

    SheetView sheetView1 = new SheetView() { TabSelected = true, WorkbookViewId = (UInt32Value)0U };
    Pane pane1 = new Pane() { VerticalSplit = 1D, TopLeftCell = "A2", ActivePane = PaneValues.BottomLeft, State = PaneStateValues.Frozen };
    Selection selection1 = new Selection() { Pane = PaneValues.BottomLeft, ActiveCell = "A2", SequenceOfReferences = new ListValue<StringValue>() { InnerText = "A2:XFD2" } };

    sheetView1.Append(pane1);
    sheetView1.Append(selection1);

    sheetViews1.Append(sheetView1);

并致电

ws.Append(sheetViews1);

之前,

ws.Append(sd);

的工作。如果我在附加了sheetdata(sd)之后附加了,那么由于它抛出的错误,该文件将会损坏;我能够看到使用生产力工具。

使用该工具,我能够首先使用我的工作代码打印我的Excel工作表(在尝试冻结标题行之前)然后我在excel中手动冻结标题行并重新保存文件。我在工具中打开了这个新文件,我能够查看用于创建文档的代码并查看附加的顺序。

希望这有助于某人。我能够看到其他一些问题,没有解决方案(或跟进)。