1

我正在使用apache poi,我想做的是:

在此处输入图像描述

我有一张看起来像这样的工作表,我想将具有特定名称的行处理到另一张这样的工作表:

在此处输入图像描述 在此处输入图像描述

对于这个操作,我写了几行代码:

public void read() throws Exception {
    InputStream inp = new FileInputStream(inputFile);
    System.out.println(inputFile);
    // InputStream inp = new FileInputStream("workbook.xlsx");

    wb = WorkbookFactory.create(inp);

    Sheet sheet = wb.getSheetAt(0);

    // 1. get the Name Values in an array list
    for (Row r : sheet) {
        Cell c = r.getCell(4);
        c.setCellType(Cell.CELL_TYPE_STRING);
        if (c != null) {
            nameList.add(c.getStringCellValue());
        }
    }

    // 2. Remove the duplicates
    removeDuplicates(nameList);

    for (Row r : sheet) {
        Cell c = r.getCell(4);
        c.setCellType(Cell.CELL_TYPE_STRING);

        //Create new workbook
        Workbook workbook = new HSSFWorkbook();
        for (int i = 0; i < nameList.size(); i++) {

            //if the list with the name equals the cell in the excel sheet
            if (nameList.get(i).equals(c.toString())) {

                // Output path
                fileOut = new FileOutputStream(
                        "C:/Users/Desktop/"
                                + nameList.get(i) + ".xls");

                // 3. create workbook and sheet
                worksheet = workbook.createSheet(nameList.get(i));

                //TODO Problem: How to get the rows from the other sheet?

                System.out.println("Write out Sheet: " + nameList.get(i));
            }
        }

        workbook.write(fileOut);
        fileOut.close();
    }
}

如您所见,我正在与getting the rows from the other sheet part. 我真的很感谢您对如何实施的回答?

4

0 回答 0