C#使用NPOI操作导出Excel,设置打印标题行

一、问题来源:C#使用NPOI操作导出Excel,设置打印标题行,使用了如下代码

    ISheet sheet = workbook.GetSheetAt(sheetIndex);
    sheet.RepeatingRows = new NPOI.SS.Util.CellRangeAddress(0, 1, 0, 5);

二、报错情况,运行及错误如下

img

img

报错位置是sheet.RepeatingRows ,提示
“((NPOI.XSSF.UserModel.XSSFSheet)sheet).RepeatingRows ”引发了类型“System.ArgumentException”的异常

img

错误详细信息

System.ArgumentException
  HResult=0x80070057
  Message=Cell reference invalid: 班级!#REF!
  Source=NPOI
  StackTrace:
   在 NPOI.SS.Util.CellReference..ctor(String cellRef)
   在 NPOI.SS.Util.CellRangeAddress.ValueOf(String reference)
   在 NPOI.XSSF.UserModel.XSSFSheet.GetRepeatingRowsOrColums(Boolean rows)
   在 NPOI.XSSF.UserModel.XSSFSheet.set_RepeatingRows(CellRangeAddress value)
   在 Soft.Service.Common.ExcelNPOIService.PrePareExportData(IWorkbook workbook, List`1 listValues, Int32 sheetIndex, List`1 rowHeightInPoints, ExcelCellRangeAddress excelCellRangeAddress) 在 D:\Code\Soft.Service\Common\NPOIService.cs 中: 第 317 行
   在 Soft.Service.Common.ExcelNPOIService.ExportWithPanel(IWorkbook workbook, String ExportFileName, List`1 sheetList) 在 D:\Code\Soft.Service\Common\NPOIService.cs 中: 第 222 行
   在 NewWeb.Areas.ChuLiangGuanLi.Controllers.ShiChuBeiLiangYueBaoTongJiController.DataExportWithPanel(String TongJiNianYue) 在 D:\Code\NewWeb\Areas\ChuLiangGuanLi\Controllers\ShiChuBeiLiangYueBaoTongJiController.cs 中: 第 337 行
   在 System.Web.Mvc.ActionMethodDispatcher.<>c__DisplayClass1.<WrapVoidAction>b__0(ControllerBase controller, Object[] parameters)
   在 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   在 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   在 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   在 System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
   在 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   在 System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
   在 System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()

引用 皆我百晓生 小程序回复内容作答:
根据错误信息,问题似乎是因为在设置重复行的范围时出现了无效的单元格引用。可能是不正确的单元格引用导致了该错误。

请确保在设置重复行的范围时使用正确的单元格引用格式。示例中的代码使用了A1 样式的单元格引用,即像“班级!#REF!”这样的引用是无效的。

正确的单元格引用格式通常类似于第一行和最后一行的单元格位置,例如(0, 5)表示第一行到第五行之间的单元格。

你可以尝试修改代码如下:

ISheet sheet = workbook.GetSheetAt(sheetIndex);
sheet.RepeatingRows = new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 5);

这将设置第一行作为重复行,从第一列到第六列的范围。

如果问题仍然存在,建议仔细检查单元格引用以确保其正确性,确保其引用的范围在有效的行和列范围内。如果问题仍然存在,请提供更多相关代码和错误信息,以便更好地理解问题并提供帮助。

【相关推荐】




如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^