2015-03-13 73 views
1

寫入新創建的文件時,我總是得到一個異常。即使提供的表單爲空,我也會得到此異常。無工作表的工作簿可以毫無問題地保存。NPOI xls:計算結束索引(x)超出允許範圍(y..y + 2)

此例外情況顯示空白工作表。

其他信息:計算最終指數(1932年),超出 允許範圍(1908..1910)

我使用NOPI.dll ver.2.1.3.1 http://npoi.codeplex.com/ 文件我嘗試創造是的.xls(Excel中97-2003)

文件路徑代碼bolow具有值@ 「C:\ TB \的report.xls」

我的代碼

public void Generate() 
    { 
     HSSFWorkbook newWorkBook = new HSSFWorkbook(); 
     ISheet newSheet = newWorkBook.CreateSheet("Main"); 
     newWorkBook.Add(newSheet); 

     using (FileStream fileOut = new FileStream(filePath, FileMode.Create)) 
     { 
      newWorkBook.Write(fileOut); 
     } 
    } 

文件被創建,但它是空的。

創建帶有行和列的工作表的xls時,結果是相同的,只是異常中的數字較高。

回答

1

問題是使用ISheet而不是HSSFSheet。示例中顯示的示例使用了ISheet,但它不正確。

正確的代碼:

public void Generate() 
{ 
    HSSFWorkbook newWorkBook = new HSSFWorkbook(); 
    HSSFSheet newSheet = (HSSFSheet)newWorkBook.CreateSheet("Main"); 

    var headerRow = newSheet.CreateRow(0); 
    var headerCell = headerRow.CreateCell(0); 
    headerCell.SetCellValue("Something"); 

    using (FileStream fileOut = new FileStream(filePath, FileMode.Create)) 
    { 
     newWorkBook.Write(fileOut); 
    } 
}