将其他工作表中的行复制到新工作表中

时间:2013-10-16 13:56:39

标签: java excel apache-poi

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

enter image description here

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

enter image description here enter image description here

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

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争吵。我真的很感激你对如何实现这个问题的答案?

0 个答案:

没有答案