2017-06-15 45 views
0

我想使用Open XML來創建文件,但試圖添加只是第一行的頭文件被損壞,我無法打開,任何人都可以告訴我什麼我在這裏做錯了嗎?使用開放式XML創建excel文件

using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create("C:\\testpdfs\\mytest.xlsx", SpreadsheetDocumentType.Workbook)) 
      { 
       // Add a WorkbookPart to the document. 
       WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart(); 
       workbookpart.Workbook = new Workbook(); 

       // Add a WorksheetPart to the WorkbookPart. 
       WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>(); 
       worksheetPart.Worksheet = new Worksheet(new SheetData()); 

       // Add Sheets to the Workbook. 
       Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook. 
        AppendChild<Sheets>(new Sheets()); 

       // Append a new worksheet and associate it with the workbook. 
       Sheet sheet = new Sheet() 
       { 
        Id = spreadsheetDocument.WorkbookPart. 
        GetIdOfPart(worksheetPart), 
        SheetId = 1, 
        Name = ViewBag.Title 
       }; 

       Row row = new Row() { RowIndex = 1 }; 
       Cell header1 = new Cell() { CellReference = "A1", CellValue = new CellValue("Interval Period Timestamp") }; 
       row.Append(header1); 
       Cell header2 = new Cell() { CellReference = "A2", CellValue = new CellValue("Settlement Interval") }; 
       row.Append(header2); 
       Cell header3 = new Cell() { CellReference = "A3", CellValue = new CellValue("Aggregated Consumption Factor") }; 
       row.Append(header3); 
       Cell header4 = new Cell() { CellReference = "A4", CellValue = new CellValue("Loss Adjusted Aggregated Consumption") }; 
       row.Append(header4); 


       sheet.Append(row); 

       sheets.Append(sheet); 


       //sheet.Append(row); 


       workbookpart.Workbook.Save(); 

       // Close the document. 
       spreadsheetDocument.Close(); 
       return View(); 

      } 

回答

2

這裏有幾個問題。

首先,您將row添加到Sheet,但它需要添加到SheetData。要做到這一點,最簡單的方法就是參考保持到SheetData對象,所以我們可以在以後使用它:

SheetData sheetData = new SheetData(); 
worksheetPart.Worksheet = new Worksheet(sheetData); 
... 
sheetData.Append(row); 

其次,你需要明確地給出一個數據類型到每個細胞,因爲默認情況下,如果沒有數據類型給定是數字:

Cell header1 = new Cell() { CellReference = "A1", CellValue = new CellValue("Interval Period Timestamp"), DataType = CellValues.String }; 

最後,您的單元格引用不太正確。您將所有內容添加到一行,但添加了第1到第4行(A1-A4)的引用。鑑於單元在您的代碼中被稱爲「標題」,我猜你實際上需要單元格A1-D1中的值,在這種情況下,您只需更新CellReference值。如果你真的需要A1-A4的值,那麼你需要將每個單元格添加到新行。

完整的代碼清單(假設您想要單元格A1-D1)是:

using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create("C:\\testpdfs\\mytest.xlsx", SpreadsheetDocumentType.Workbook)) 
{ 
    // Add a WorkbookPart to the document. 
    WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart(); 
    workbookpart.Workbook = new Workbook(); 

    // Add a WorksheetPart to the WorkbookPart. 
    WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>(); 
    SheetData sheetData = new SheetData(); 
    worksheetPart.Worksheet = new Worksheet(sheetData); 

    // Add Sheets to the Workbook. 
    Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook. 
     AppendChild<Sheets>(new Sheets()); 

    // Append a new worksheet and associate it with the workbook. 
    Sheet sheet = new Sheet() 
    { 
     Id = spreadsheetDocument.WorkbookPart. 
     GetIdOfPart(worksheetPart), 
     SheetId = 1, 
     Name = ViewBag.Title 
    }; 

    Row row = new Row() { RowIndex = 1 }; 
    Cell header1 = new Cell() { CellReference = "A1", CellValue = new CellValue("Interval Period Timestamp"), DataType = CellValues.String }; 
    row.Append(header1); 
    Cell header2 = new Cell() { CellReference = "B1", CellValue = new CellValue("Settlement Interval"), DataType = CellValues.String }; 
    row.Append(header2); 
    Cell header3 = new Cell() { CellReference = "C1", CellValue = new CellValue("Aggregated Consumption Factor"), DataType = CellValues.String }; 
    row.Append(header3); 
    Cell header4 = new Cell() { CellReference = "D1", CellValue = new CellValue("Loss Adjusted Aggregated Consumption"), DataType = CellValues.String }; 
    row.Append(header4); 

    sheetData.Append(row); 

    sheets.Append(sheet); 

    workbookpart.Workbook.Save(); 

    // Close the document. 
    spreadsheetDocument.Close(); 
    return View(); 

}