2017-09-06 89 views
1

尝试引用此链接:SheetViews为null试图冻结行的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;

创建我的电子表格时,我没有使用任何sheetviews,和我使用的MemoryStream传回使用。

使用此代码:

我怎么会冻顶两行?谢谢!

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

通过使用开放的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工作表。我打开这个新文件了里面的工具,并且我能够查看用于创建文档的代码,看到了追加的订单。

希望这可以帮助别人。我能够看到像这些没有解决方案(或后续)的其他一些问题。