我想从 Excel 中删除整行,
我尝试过removeRow
:
XSSFRow rerow = sheet1.getRow(1);
sheet1.removeRow(rerow);
和shiftRows
:
int rowIndex = 1;
int lastIntext = sheet1.getLastRowNum();
sheet1.shiftRows(rowIndex+1, lastIntext, -1);
但它只删除行中的值而不是整行。
最佳答案
即使我遇到了同样的问题,但经过几次研究后发现
存在错误/或者他们可能更改了 <version>4.0.0</version> and <version>4.0.1</version>
中的行为 Apache 波伊
对于
sheet1.shiftRows(rowIndex+1, lastIntext, -1);
请使用
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
并使用您的代码更好的方式来删除您的行,如下所示
public static void removeRow(Sheet sheet, int rowIndex) {
int lastRowNum = sheet.getLastRowNum();
if (rowIndex >= 0 && rowIndex < lastRowNum) {
sheet.shiftRows(rowIndex + 1, lastRowNum, -1);
}
if (rowIndex == lastRowNum) {
Row removingRow = sheet.getRow(rowIndex);
if (removingRow != null) {
sheet.removeRow(removingRow);
}
}
}
这对我有用。 也许他们可能会在下一个 api 版本中更新它
如果对你有帮助的话谢谢
关于java - 如何使用 Java Apache POI 从 Excel 中删除整行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54252684/