简体   繁体   English

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

[英]Apache POI: How to insert column in Excel file

I'm using POI to manipulate data in Excel files for a university project. 我正在使用POI来处理大学项目的Excel文件中的数据。 I'm having trouble inserting a new column in an existing Excel. 我在现有Excel中插入新列时遇到问题。 I tried to use 我试着用

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 ? 有没有办法做到这一点,没有迭代行中的所有单元格并逐个移动它们?

To my knowledge, POI doesn't support this directly. 据我所知,POI不直接支持这一点。 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. 您还需要考虑在插入点之外的列中重写任何公式的需要以及使用这些单元格的任何公式。

According to this post from 2008, which I got from Google "poi insert column", POI does not presently have an API to do this. 根据我从谷歌“poi insert专栏”获得的2008年的这篇文章 ,POI目前没有API来做到这一点。 You will need to iterate over the rows and adjust all references yourself. 您需要遍历行并自行调整所有引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM