Apache poi - 删除excel中的第一行并将其保存到同一文件中

时间:2021-07-28 20:22:55

Hello i need help with this, i tried about 30 tutorials last few hours and i dont know how to solve it:

您好我需要帮助,我在最近几个小时尝试了大约30个教程,我不知道如何解决它:

  1. Open Excel File
  2. 打开Excel文件

  3. Delete and remove row "A" ( to be replaced in for later by excel row B,C,D,...)
  4. 删除并删除行“A”(稍后将替换为excel行B,C,D,...)

  5. Rewrite opened Excel File ( because if program crash for high usage i need to have stored last value, and start program again without searching and deleting which was used ... )
  6. 重写打开的Excel文件(因为如果程序崩溃高用量我需要存储最后一个值,并重新启动程序而不搜索和删除使用的...)


OPCPackage fileInputStream = OPCPackage.open(new File("input.xlsx"));
        XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
        XSSFSheet worksheet = workbook.getSheetAt(0);
        worksheet.shiftRows(0, 0, 1);
        workbook.write(new FileOutputStream("input.xlsx")); 

This code dont remove row a and dont save file to the same location ...

此代码不删除行a并且不将文件保存到同一位置...

Could anybody help me please?

请问有人帮帮我吗?

Thank you FJ

谢谢FJ

1 个解决方案

#1


1  

First problem in your code:

代码中的第一个问题:

The worksheet.shiftRows(0, 0, 1); shifts first row one row downwards. If the need is removing first row, then second row up to last row should be shifted one row upwards. This would be worksheet.shiftRows(1, worksheet.getLastRowNum(), -1);.

worksheet.shiftRows(0,0,1);将第一行向下移动一行。如果需要移除第一行,则第二行直到最后一行应该向上移动一行。这将是worksheet.shiftRows(1,worksheet.getLastRowNum(), - 1);.

Second problem in your code:

代码中的第二个问题:

If a File is used for creating a Workbook then the workbook cannot be written into the same file. This is because the File used stays opened until the workbook will be closed. So we should not using a File here for opening the workbook but a FileInputStream instead.

如果使用文件创建工作簿,则无法将工作簿写入同一文件。这是因为在关闭工作簿之前,所使用的文件将保持打开状态。所以我们不应该在这里使用File来打开工作簿而是使用FileInputStream。

Working example:

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

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

class ReadExcelRemoveRowAndWrite {

 public static void main(String[] args) throws Exception {

  Workbook workbook = WorkbookFactory.create(new FileInputStream("input.xlsx"));

  Sheet worksheet = workbook.getSheetAt(0);

  worksheet.shiftRows(1, worksheet.getLastRowNum(), -1);

  workbook.write(new FileOutputStream("input.xlsx"));
  workbook.close();

 }
}

#1


1  

First problem in your code:

代码中的第一个问题:

The worksheet.shiftRows(0, 0, 1); shifts first row one row downwards. If the need is removing first row, then second row up to last row should be shifted one row upwards. This would be worksheet.shiftRows(1, worksheet.getLastRowNum(), -1);.

worksheet.shiftRows(0,0,1);将第一行向下移动一行。如果需要移除第一行,则第二行直到最后一行应该向上移动一行。这将是worksheet.shiftRows(1,worksheet.getLastRowNum(), - 1);.

Second problem in your code:

代码中的第二个问题:

If a File is used for creating a Workbook then the workbook cannot be written into the same file. This is because the File used stays opened until the workbook will be closed. So we should not using a File here for opening the workbook but a FileInputStream instead.

如果使用文件创建工作簿,则无法将工作簿写入同一文件。这是因为在关闭工作簿之前,所使用的文件将保持打开状态。所以我们不应该在这里使用File来打开工作簿而是使用FileInputStream。

Working example:

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

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

class ReadExcelRemoveRowAndWrite {

 public static void main(String[] args) throws Exception {

  Workbook workbook = WorkbookFactory.create(new FileInputStream("input.xlsx"));

  Sheet worksheet = workbook.getSheetAt(0);

  worksheet.shiftRows(1, worksheet.getLastRowNum(), -1);

  workbook.write(new FileOutputStream("input.xlsx"));
  workbook.close();

 }
}