java Apache POI:如何在 Excel 文件中插入列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4199096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:16:30  来源:igfitidea点击:

Apache POI: How to insert column in Excel file

javaexcel

提问by user510058

I'm using POI to manipulate data in Excel files for a university project. I'm having trouble inserting a new column in an existing Excel. I tried to use

我正在使用 POI 为大学项目操作 Excel 文件中的数据。我在现有 Excel 中插入新列时遇到问题。我试着用

Cell c = createCell(int column);
c.setCellValue("someValue");

but it seems that if the column already exists, it replaces the existing data. What I need is to shift all the other columns one column to the right when I insert the new one.

但似乎如果该列已经存在,它会替换现有数据。我需要的是在插入新列时将所有其他列向右移动一列。

I searched over the internet but I couldn't find a solution to that. Is there a way to do it, without iterating all the cells in the row and moving them one-by-one ?

我在互联网上搜索,但我找不到解决方案。有没有办法做到这一点,而无需迭代行中的所有单元格并逐一移动它们?

采纳答案by Brad Gardner

To my knowledge, POI doesn't support this directly. You could create a copy of the worksheet and copy the data over, leaving room for your new column. You also need to take into consideration the need to re-write any formulas in the columns beyond your insertion point and any formulas that use those cells.

据我所知,POI 不直接支持这一点。您可以创建工作表的副本并复制数据,为新列留出空间。您还需要考虑在插入点之外的列中重写任何公式以及使用这些单元格的任何公式的需要。

回答by Jim Garrison

According to this postfrom 2008, which I got from Google "poi insert column", POI does not presently have an API to do this. You will need to iterate over the rows and adjust all references yourself.

根据我从 2008 年从 Google“poi insert column”获得的这篇文章,POI 目前没有 API 来执行此操作。您需要遍历行并自己调整所有引用。