2014-10-20 24 views
2

我正在使用open xml sdk創建excel文件。我想用open xml讀取它。當我想在這行
SharedStringTablePart sstpart = workbookPart.GetPartsOfType<SharedStringTablePart>().First();it return null共享字符串表在讀取創建的Excel時返回null(打開xml)

,如果我打開Excel文件並保存again.It創建共享字符串表,並運行它閱讀我收到錯誤。

從Excel

#region OpenFile 
     public void OpenFile(string directory) 
     { 
      try 
      { 
       fs = new FileStream(directory, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
       doc = SpreadsheetDocument.Open(fs, false); 
      } 
      catch (FileNotFoundException ex) 
      { 
       string exception = string.Format("Doya bulunamadı{0}", directory); 
       throw new ApplicationException(exception); 
      } 
     } 
     #endregion 

     #region GetWorkSheet 
     public void AssignWorkSheet(int sheetID) 
     { 
      if (fs == null || doc == null) 
       throw new ApplicationException("Dosya açılamadı"); 

      WorkbookPart workbookPart = doc.WorkbookPart; 
      SharedStringTablePart sstpart = workbookPart.GetPartsOfType<SharedStringTablePart>().First(); 
      sst = sstpart.SharedStringTable; 

      var workbook = workbookPart.Workbook; 
      var sheets = workbook.Descendants<Sheet>(); 
      var sheetINVOICE = sheets.ElementAt(sheetID); 
      var worksheetPartINVOICE = (WorksheetPart)workbookPart.GetPartById(sheetINVOICE.Id); 
      WorkSheet = worksheetPartINVOICE.Worksheet; 
      //return sheetInvoice; 
     } 
     #endregion 

讀它創建了一個寫一些文字,數值與下面的代碼

var fileName = string.Format(@"C:\Sonuc\{0}\{1}.xlsx", systemType.ToString(), systemTypeEnum.ToString() + "_" + fileType.ToString()); 
      SpreadsheetDocument xl = SpreadsheetDocument.Create(fileName, 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(); 

      //int rowCount = 1; 
      DocumentFormat.OpenXml.UInt32Value rowCount = 1; 



      foreach (var item in resultList) 
      { 
       rowCount = rowCount + 1; 
       Row r3 = new Row() { RowIndex = rowCount }; 
       Cell c6 = new Cell(); 
       c6.DataType = CellValues.String; 
       c6.CellValue = new CellValue(item.BusinessPartner); 
       r3.Append(c6); 

       Cell c7 = new Cell(); 
       c7.DataType = CellValues.Number; 
       c7.CellValue = new CellValue(item.VKN); 
       r3.Append(c7); 

       Cell c8 = new Cell(); 
       c8.DataType = CellValues.Number; 
       c8.CellValue = new CellValue(item.InvoiceCount.ToString()); 
       r3.Append(c8); 

       Cell c9 = new Cell(); 
       c9.DataType = CellValues.Number; 
       c9.CellValue = new CellValue(item.TaxTotalAmount.ToString()); 
       r3.Append(c9); 
       sd.Append(r3); 

      } 

      ws.Append(sd); 
      wsp.Worksheet = ws; 
      wsp.Worksheet.Save(); 

      Sheets sheets = new Sheets(); 
      Sheet sheet = new Sheet(); 
      sheet.Name = "Sheet1"; 
      sheet.SheetId = 1; 
      sheet.Id = wbp.GetIdOfPart(wsp); 
      sheets.Append(sheet); 
      wb.Append(fv); 
      wb.Append(sheets); 

      xl.WorkbookPart.Workbook = wb; 
      xl.WorkbookPart.Workbook.Save(); 
      xl.Close(); 

回答

4

默認情況下,MS EXCEL保存使用SharedStringTablePartString值。這就是爲什麼當你使用MS EXCEL打開和保存文件時,你的代碼似乎很有用。

但在這裏,當你創建的文件,您通過閱讀Cell.CellValue屬性定義Cell.DatatypeCellValues.String(而不是CellValues.SharedString

c6.DataType = CellValues.String; 

Cell.DatatypeCellValues.String,你必須閱讀的價值。


保存使用SharedStringPartString值,請參考在線文檔:

+0

感謝克里斯,我已閱讀並通過閱讀解決我的問題你第一個鏈接 – altandogan 2014-10-21 10:16:50

+0

@ user990513歡迎您。很高興我能幫上忙。 – Chris 2014-10-21 18:03:45

相關問題