如何使用 HSSF (Apache POI) 在现有 Excel 中的两行之间插入一行
不知何故,我设法在现有 Excel 文件中的两行之间创建新行。问题是,一些格式没有包含在行的移动中。
其中之一是隐藏的行在轮班期间没有相对地进行。我的意思是(例如),第 20 到 30 行被隐藏,但是当创建新行时,格式仍然存在。在插入/创建新行期间,隐藏行也必须移动,它应该是 21 到 31。
另一件事是,工作表中不在单元格中的其他对象。就像创建新行后文本框不会移动一样。就像这些物体的位置是固定的一样。但我希望它移动,就像我在 Excel 中插入新行或粘贴行一样。如果有插入新行的功能,请告诉我。
这就是我现在所拥有的,只是我的代码中的一个片段。
HSSFWorkbook wb = new HSSFWorkbook(template); //template is the source of file
HSSFSheet sheet = wb.getSheet("SAMPLE");
HSSFRow newRow;
HSSFCell cellData;
int createNewRowAt = 9; //Add the new row between row 9 and 10
sheet.shiftRows(createNewRowAt, sheet.getLastRowNum(), 1, true, false);
newRow = sheet.createRow(createNewRowAt);
newRow = sheet.getRow(createNewRowAt);
如果可以复制和粘贴行,那将有很大帮助。但我已经在这里问了,找不到解决方案。所以我决定创建一行作为临时解决方案。我已经完成了,但遇到了这样的问题。
任何帮助将不胜感激。谢谢!
Somehow I manage to create new rows between two rows in an existing excel file. The problem is, some of the formatting were not include along the shifting of the rows.
One of this, is the row that are hide are not relatively go along during the shift. What I mean is(ex.), rows from 20 to 30 is hidden, but when a create new rows the formating still there. The hidden rows must also move during the insertion/creation of new rows, it should be 21 to 31.
Another thing is, the other object in the sheet that are not in the cell. Like the text box are not move along after the new row is created. Its like the position of these object are fixed. But I want it to move, the same thing as I insert a new row or paste row in excel. If there is a function of inserting a new row, please let me know.
This what I have right now, just a snippet from my code.
HSSFWorkbook wb = new HSSFWorkbook(template); //template is the source of file
HSSFSheet sheet = wb.getSheet("SAMPLE");
HSSFRow newRow;
HSSFCell cellData;
int createNewRowAt = 9; //Add the new row between row 9 and 10
sheet.shiftRows(createNewRowAt, sheet.getLastRowNum(), 1, true, false);
newRow = sheet.createRow(createNewRowAt);
newRow = sheet.getRow(createNewRowAt);
If copy and paste of rows is possible that would be big help. But I already ask it here and can't find a solution. So I decided to create a row as an interim solution. I'm done with it but having a problem like this.
Any help will be much appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
用于复制行的辅助函数无耻地改编自此处
Helper function to copy rows shamelessly adapted from here
对于希望使用 XSSF (Apache POI) 在现有 excel 的两行之间插入一行的人,XSSFSheet 中已经实现了一种方法“copyRows”。
For people who are looking to insert a row between two rows in an existing excel with XSSF (Apache POI), there is already a method "copyRows" implemented in the XSSFSheet.
参考Qwerty的答案,您可以通过重新使用
cellStyle
来避免夸大XL尺寸。当类型为
CELL_TYPE_BLANK
时,getStringCellValue
返回""
而不是null
。Referencing Qwerty's answer, you can avoid to inflate XL size by re-using
cellStyle
.And when the type is
CELL_TYPE_BLANK
,getStringCellValue
returns""
instead ofnull
.引用Qwerty的回答,如果destRow不为null,sheet.shiftRows()会将destRow的引用更改为下一行;所以我们应该总是创建一个新行:
Referencing Qwerty's answer, if the destRow isnot null, sheet.shiftRows() will change the destRow's reference to the next row; so we should always create a new row:
我在以下实现中合并了一些其他答案和评论,并使用 Apache POI v3.9 进行了测试。
我只有一个
rownum
参数,因为我向下移动目标行并将其复制到新的空行中。公式按预期处理,不会逐字复制,但有一个例外:对复制行上方的单元格的引用不会更新;解决方法是将这些显式引用(如果有)替换为使用INDIRECT()
计算的引用,如 this 所建议发布。我在生产代码中使用这个实现。
I merged some of the other answers and comments in the following implementation, tested with Apache POI v3.9.
I have only one
rownum
parameter because I shift down the target row and copy it in the new empty row. Formulas are handled as expected, they are not copied verbatim, with one exception: references to cells that are above the copied line are not updated; the workaround is to replace these explicit references (if any) with references calculated usingINDIRECT()
as suggested by this post.I'm using this implementation in production code.
我在 Kotlin 中实现了这样的功能:
它不复制单元格值,只复制格式。
应该也适用于 Java。
I've implemented this in Kotlin like this:
It doesn't copy the cell values, just the format.
Should be applicable to Java as well.
至于在新行中“更新”的公式,由于所有复制都发生在移位之后,旧行(现在是新行上一个索引)的公式已经移位,因此将其复制到新行将使新行引用旧行单元格。一个解决方案是在转换之前解析出公式,然后应用它们(一个简单的字符串数组就可以完成这项工作。我相信您可以在几行中编写代码)。
在函数开始时:
然后将公式应用于单元格时:
As to formulas being "updated" in the new row, since all the copying occurs after the shift, the old row (now one index up from the new row) has already had its formula shifted, so copying it to the new row will make the new row reference the old rows cells. A solution would be to parse out the formulas BEFORE the shift, then apply those (a simple String array would do the job. I'm sure you can code that in a few lines).
At start of function:
Then when applying the formula to a cell:
我最近遇到了同样的问题。我必须在包含隐藏行的文档中插入新行,并且遇到了与您相同的问题。经过一些搜索和 apache poi 列表中的一些电子邮件后,当文档包含隐藏行时,这似乎是 shiftrows() 中的错误。
I came across the same issue recently. I had to insert new rows in a document with hidden rows and faced the same issues with you. After some search and some emails in apache poi list, it seems like a bug in shiftrows() when a document has hidden rows.