gpt4 book ai didi

java - Apache POI shiftRows 损坏文件并删除内容

转载 作者:行者123 更新时间:2023-12-01 07:44:33 36 4
gpt4 key购买 nike

我想将表填充到一个 Excel 模板文件中。我想插入行并填充它们。我使用 java Apache POI 库来访问 excel 文件。首先,我创建了一个新文件,并在 A 列的第 1 行到第 10 行中填充了 1..10 个数字,然后保存了该文件。然后我读取该文件并尝试使用 sheet.shiftRows() 方法插入一个空行。我尝试了下面的代码,但输出文件在打开(读取)时出现问题,第 5、6、7 行为空,并且未发生移动。

InputStream inputStream = new FileInputStream("TestIn-1.xlsx");
Workbook workbookIn = new XSSFWorkbook(inputStream);
Sheet sheetIn = workbookIn.getSheet("Sheet1");

sheetIn.shiftRows(4,5,1);

OutputStream outputStream = new FileOutputStream("TestOut.xlsx");
workbookIn.write(outputStream);
outputStream.close();

最佳答案

您的 shiftRows 尝试将第 5 行(索引 4)和第 6 行(索引 5)之间的行向下移动一行。但是第 7、8、9 和 10 行呢?如果需要获取新的空第 5 行,则需要在第 5 行和最后一行之间向下移动一行。

使用apache poi版本3.17这很简单:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;

class ExcelReadShiftRowsAndWrite {

public static void main(String[] args) throws Exception {
//String fileIn= "TestIn.xls";
//String fileOut= "TestOut.xls";
String fileIn= "TestIn.xlsx";
String fileOut= "TestOut.xlsx";

try (Workbook workbook = WorkbookFactory.create(new FileInputStream(fileIn));
FileOutputStream out = new FileOutputStream(fileOut)) {

Sheet sheet = workbook.getSheet("Sheet1");

sheet.shiftRows(4, sheet.getLastRowNum(), 1); //shifts rows between row 5 (index 4) and last row one row down

workbook.write(out);
}
}
}

但是高于 3.17apache poi 版本,以及 4.1.0,在使用 shiftRows 时存在错误XSSF。在那里,移位后,单元格中的引用保持旧状态,而不是调整为新行。例如,引用 A5A6、... 下移后仍保留,而不是调整为 A6A7, ...

所以必须纠正这个错误:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;

class ExcelReadShiftRowsAndWrite {

public static void main(String[] args) throws Exception {
//String fileIn= "TestIn.xls";
//String fileOut= "TestOut.xls";
String fileIn= "TestIn.xlsx";
String fileOut= "TestOut.xlsx";

try (Workbook workbook = WorkbookFactory.create(new FileInputStream(fileIn));
FileOutputStream out = new FileOutputStream(fileOut)) {

Sheet sheet = workbook.getSheet("Sheet1");

sheet.shiftRows(4, sheet.getLastRowNum(), 1); //shifts rows between row 5 (index 4) and last row one row down

if (sheet instanceof XSSFSheet) {
XSSFSheet xSSFSheet = (XSSFSheet)sheet;
// correcting bug that shiftRows does not adjusting references of the cells
// if row 3 is shifted down, then reference in the cells remain r="A3", r="B3", ...
// they must be adjusted to the new row thoug: r="A4", r="B4", ...
// apache poi 3.17 has done this properly but had have other bugs in shiftRows.
for (int r = xSSFSheet.getFirstRowNum(); r < sheet.getLastRowNum() + 1; r++) {
XSSFRow row = xSSFSheet.getRow(r);
if (row != null) {
long rRef = row.getCTRow().getR();
for (Cell cell : row) {
String cRef = ((XSSFCell)cell).getCTCell().getR();
((XSSFCell)cell).getCTCell().setR(cRef.replaceAll("[0-9]", "") + rRef);
}
}
}
// end correcting bug
}

workbook.write(out);
}
}
}

关于java - Apache POI shiftRows 损坏文件并删除内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55980407/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com