2015-03-13 75 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); 
    } 
}