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

时间:2022-08-14 20:26:21

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 ?

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

2 个解决方案

#1


6  

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

#2


1  

According to this post from 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年获得的这篇文章,我从谷歌“poi insert专栏”,POI目前没有API来做到这一点。您需要遍历行并自行调整所有引用。

#1


6  

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

#2


1  

According to this post from 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年获得的这篇文章,我从谷歌“poi insert专栏”,POI目前没有API来做到这一点。您需要遍历行并自行调整所有引用。