java - 如何使用apache poi将excel拆分为多个excel?

标签 java excel apache-poi

我的 Excel 电子表格中有以下表格

manager    salary
puk          2
puk          3
puk          4
puk          5
ser          3
ser          4
ser          5
sos          23
sos          24
sos          25
sos          26
sos          27

我需要使用 FileOutputStream 将此电子表格拆分为三个不同的 shpreadsheet。第一个应包含 manager puk 的所有条目,第二个应包含 ser 的所有条目。 。 .

我想不出一个好的逻辑来分割它。我应该创建原始电子表格的临时副本,然后删除所有多余的行并保存它吗?那么我怎样才能删除行呢?

(这可以使用 CSVReader 轻松完成,但我需要保留格式)

最佳答案

这是关于如何执行您所要求的操作的整个程序,我已经对其进行了测试并且运行良好:) 告诉我你的想法:

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

import java.util.*;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import java.io.*;
public class StackOverflow {

    public static Workbook readWorkbook(){

        Workbook wb=null;
        try {
            wb = WorkbookFactory.create(new File("stackOverflow.xlsx"));
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }

    public static void writeToSheet(List<String> name, List<Double> salary, int sheetCounter, List<Sheet> outputSheets){


        for(int i=0;i<salary.size();i++){

            Row row=outputSheets.get(sheetCounter).createRow(i);

            Cell nameCell=row.createCell(0);
            nameCell.setCellValue(name.get(i));

            Cell salaryCell=row.createCell(1);
            salaryCell.setCellValue(salary.get(i));
        }
    }

    public static void writeToWorkbook(Workbook wb){

        try{
            FileOutputStream out=new FileOutputStream("new StackOverflow.xlsx");
            wb.write(out);
            out.close();
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args){

        Workbook inputWb=readWorkbook();
        Sheet inputWs=inputWb.getSheet("sheet1");

        List<String> name=new ArrayList<>();
        List<Double> salary=new ArrayList<>();

        Workbook outputWb=new XSSFWorkbook();
        List<Sheet> outputSheets=new ArrayList<>();

        int rowIndex=inputWs.getLastRowNum()+1;
        int sheetCounter=0;

        for(int i=1; i<rowIndex-1; i++){
            Row outerRow=inputWs.getRow(i);
            Row innerRow=null; 
            Cell outerCell=outerRow.getCell(0);
            Cell innerCell=null;

            int j=0;
            for(j=i+1;j<rowIndex;j++){

                innerRow=inputWs.getRow(j);
                innerCell=innerRow.getCell(0);

                if(outerCell.getStringCellValue().equals(innerCell.getStringCellValue())){
                    name.add(innerRow.getCell(0).getStringCellValue());
                    salary.add(innerRow.getCell(1).getNumericCellValue());
                }


                if(!outerCell.getStringCellValue().equals(innerCell.getStringCellValue())){

                    break;
                }


            }

            name.add(outerRow.getCell(0).getStringCellValue());
            salary.add(outerRow.getCell(1).getNumericCellValue());
            i=j;
            outputSheets.add(outputWb.createSheet("sheet"+sheetCounter));
            writeToSheet(name,salary,sheetCounter, outputSheets);
            sheetCounter++;
            name.clear();
            salary.clear();

            Row tempRow=inputWs.getRow(i);
            try{
                name.add(tempRow.getCell(0).getStringCellValue());
                salary.add(tempRow.getCell(1).getNumericCellValue());
            }catch(Exception e){
                continue;
            }
        }
        writeToWorkbook(outputWb);
    }
}

关于java - 如何使用apache poi将excel拆分为多个excel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21897412/

相关文章:

excel - 当数百设置为0时更改excel中行的高度

java - Apache POI - 将 .html 电子表格转换为 .xls 电子表格

java - 使用 Scala 的 poi-ooxml-schemas-3.9.jar 的 Sbt 编译错误

Excel 在 Apache POI Stream API 中导出 150K+ 数据时出现 JAVA OutOfMemoryError 问题

java - 在 Linux 中永久设置类路径

java - 输出说明

javascript - VSCode Launch.json 配置无法启动 :desktop -- --app excel fails error 1 for Excel Office-Addin

java - 覆盖 ZipEntry

java - 如何保存文件而不覆盖现有同名文件

vba - Excel VBA检查值是否不在范围内