如何使用Apache POI将数据从一个excel文件的单元格复制到java中的另一个excel文件(在所需的行和列中)

时间:2016-12-29 15:00:18

标签: java excel apache-poi

以下是从一张表中获取数据的代码。现在我的查询是如何存储所有输出数据,以便我可以编写另一种方法将其粘贴到不同的表格中。

public void ReadCount(String Path) {
    String []module;
    int [][]status;

    try {
        FileInputStream fs = new FileInputStream(new File(Path));
        XSSFWorkbook workBook = new XSSFWorkbook(fs);

        boolean isPresent = false;
        XSSFSheet sheet = workBook.getSheetAt(0);

        for (int i=0; i<sheet.getLastRowNum(); i++) {
            Row r = sheet.getRow(i);
            for (int cn=0; cn<r.getLastCellNum(); cn++) {
                Cell c = r.getCell(cn);
                c.setCellType ( Cell.CELL_TYPE_STRING );
                String text = c.getStringCellValue();
                if(text.trim().equalsIgnoreCase("Module Name")) {   
                    for(int a = i+2; a<sheet.getLastRowNum(); a++) {
                        Row p = sheet.getRow(a);
                        Cell q = p.getCell(cn);
                        q.setCellType ( Cell.CELL_TYPE_STRING );
                        String m = q.getStringCellValue();
                        System.out.println(m);

                        Cell k = p.getCell(cn+1);
                        k.setCellType ( Cell.CELL_TYPE_NUMERIC );
                        double PASS = k.getNumericCellValue();
                        int P = (int)PASS;
                        System.out.println(P);

                        Cell l = p.getCell(cn+2);
                        l.setCellType ( Cell.CELL_TYPE_NUMERIC );
                        double FAIL = l.getNumericCellValue();
                        int F = (int)FAIL;
                        System.out.println(F);

                        Cell n = p.getCell(cn+3);
                        n.setCellType ( Cell.CELL_TYPE_NUMERIC );
                        double ONHOLD = n.getNumericCellValue();
                        int O = (int)ONHOLD;
                        System.out.println(O);
                    }   
                    //return module;
                }       
            }
        }
    } catch(IOException e) {
        e.printStackTrace();
    }
}

此方法的控制台输出是

ROW1 1 0 0 ROW2 1 0 0 ROW3 0 1 0 ROW4 0 1 1

0 个答案:

没有答案