从 Java 导出到 Excel - InitationTargetException

发布于 2024-09-30 23:40:50 字数 718 浏览 0 评论 0原文

我在尝试将一些数据导出到 Excel 时陷入困境。

这是我的代码

List<String> headers = new ArrayList<String>();
//////////////////////////////////////////////
/// Added some headers in between to the list               
///////////////////////////////////////////////

HSSFCell[] cell = new HSSFCell[headers.size()];

for (int i = 0; i < headersHSSF.length; i++) {  
  cell[i] = excelRow.createCell(i);    
  cell[i].setCellValue(new HSSFRichTextString(headers.get(i)));
}

此代码为行抛出 InvokingTargetException

cell[i].setCellValue(new HSSFRichTextString(headers.get(i)));

任何人都可以告诉我发生这种情况的原因吗?

(PS:我是从 Flex UI 调用代码。这不是从 java 代码调用的)

I am stuck at a point while trying to export some data into Excel.

Here is my code

List<String> headers = new ArrayList<String>();
//////////////////////////////////////////////
/// Added some headers in between to the list               
///////////////////////////////////////////////

HSSFCell[] cell = new HSSFCell[headers.size()];

for (int i = 0; i < headersHSSF.length; i++) {  
  cell[i] = excelRow.createCell(i);    
  cell[i].setCellValue(new HSSFRichTextString(headers.get(i)));
}

This code is throwing InvocationTargetException for line

cell[i].setCellValue(new HSSFRichTextString(headers.get(i)));

Can anyone please tell me the reason why is this happening?

(PS: I am calling the code from Flex UI. This is not called from java code)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戒ㄋ 2024-10-07 23:40:50

IncationTargetException 是一个检查异常,它包装了由调用的方法或构造函数抛出的异常。

可能是在创建 HSSFRichTextString 类时引发异常。在这种情况下,我会首先查看(调试)headers.get(i) 的值。

您实际上很有可能有一个嵌套的 IndexOutOfBoundsException,因为 headersHSSF 数组的大小可能大于 headers 的大小> 列表。

将循环签名更改为:

for (int i = 0; i < headers.size(); i++) {

并检查错误是否相同、消失或不同。

InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor.

Could be, that the exception is thrown while creating a HSSFRichTextString class. In this case, I'd look (debug) at the value of headers.get(i) first.

There is a good chance, that you actually have a nested IndexOutOfBoundsException, because the size of the headersHSSF array may be bigger than the size of the headers list.

Change the loop signature to:

for (int i = 0; i < headers.size(); i++) {

and check, if the error is the same, gone or different.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文