简体   繁体   English

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

[英]Apache POI shiftRows corrupts file and deletes content

I want to fill table a template excel file. 我要填写表格模板excel文件。 I want to insert rows and fill them. 我想插入行并填充它们。 I used java Apache POI library to access excel files. 我使用Java Apache POI库访问excel文件。 At first, I created a new file and filled column A from row 1 to 10 with 1..10 numbers and saved the file. 首先,我创建了一个新文件,并在第1行至第10列的A列中填充了1..10数字并保存了该文件。 Then I read the file and tried to insert a single empty row with a sheet.shiftRows() method. 然后,我读取了文件,并尝试使用sheet.shiftRows()方法插入单个空行。 I tried below code but output file has a problem in the opening (reading) and rows 5,6,7 are empty and move has not occurred. 我在下面的代码中尝试过,但是输出文件的开头(读取)有问题,第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();

Your shiftRows tries shifting rows between row 5 (index 4) and row 6 (index 5) one row down. 您的shiftRows尝试在第5行(索引4)和第6行(索引5)之间向下移动一行。 But what about row 7, 8, 9 and 10? 但是第7、8、9和10行呢? You needs shifting rows between row 5 and last row one row down if the need is getting a new empty row 5. 如果需要获得新的空行5,则需要在第5行和最后一行之间向下移动一行。

Using apache poi version 3.17 this is as simple as: 使用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);
  } 
 }
}

But apache poi versions greater than 3.17 , also 4.1.0 , have a bug in shiftRows using XSSF . 但是, apache poi版本大于3.17 ,还4.1.0 ,有一个bug shiftRows使用XSSF There, after shifting, the references in the cells remain old instead being adjusted to the new rows. 在那里,移动后,单元格中的引用保持旧状态,而不是被调整为新的行。 For example the references A5 , A6 , ... remain after shifting down instead of getting adjusted to A6 , A7 , ... 例如,参考A5A6 ,...在降档后仍然保留,而不是调整为A6A7 ,...

So this bug must be corrected: 因此,必须更正此错误:

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);
  } 
 }
}

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

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