java操作excel用POI

public class TestExcel {

    // read
    @Test
    public void test() {

        try {

            String path = this.getClass().getClassLoader().getResource("poi/test.xlsx").toString();
            System.out.println(path);
            path.replace("\\", "/");
            path = path.substring(path.indexOf(":") + 2);
            System.out.println(path);

            System.out.println("sdaff");
            String fileName = path;
            FileInputStream fis = new FileInputStream(fileName);
            Workbook workbook = null;
            // 判断excel的两种格式xls,xlsx
            if (fileName.toLowerCase().endsWith("xlsx")) {
                workbook = new XSSFWorkbook(fis);
            } else if (fileName.toLowerCase().endsWith("xls")) {
                workbook = new HSSFWorkbook(fis);
            }

            // 得到sheet的总数
            int numberOfSheets = workbook.getNumberOfSheets();
            System.out.println("一共" + numberOfSheets + "个sheet");

            // 循环每一个sheet
            for (int i = 0; i < numberOfSheets; i++) {
                // 得到第i个sheet
                Sheet sheet = workbook.getSheetAt(i);
                System.out.println(sheet.getSheetName() + "  sheet");
                // 得到行的迭代器
                Iterator<Row> rowIterator = sheet.iterator();
                int rowCount = 0;
                // 循环每一行
                List<List> bigList = new ArrayList<List>();
                while (rowIterator.hasNext()) {

                    System.out.println("第" + (rowCount++) + "行  ");

                    // 得到一行对象
                    Row row = rowIterator.next();
                    // 得到列对象
                    Iterator<Cell> cellIterator = row.cellIterator();
                    int columnCount = 0;

                    // 循环每一列
                    List<String> list = new ArrayList<String>();
                    while (cellIterator.hasNext()) {
                        // 得到单元格对象
                        Cell cell = cellIterator.next();
                        // 检查数据类型
                        switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                            // System.out.print(cell.getStringCellValue() +
                            // "   ");
                            list.add(cell.getStringCellValue());
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            // System.out.print(cell.getNumericCellValue() +
                            // "   ");
                            list.add(cell.getNumericCellValue() + "");
                        }
                    }

                    bigList.add(list);

                    System.out.println();
                }

                System.out.println("bigList.size:" + bigList.size());

                List<InspectDomain> inspectList = new ArrayList<InspectDomain>();

                for (int m = 1; m < bigList.size(); m++) {
                    InspectDomain inspectDomain = new InspectDomain();
                    inspectDomain.setId(bigList.get(m).get(0).toString());
                    inspectDomain.setReportTitle(bigList.get(m).get(1)
                            .toString());
                    System.out.println("~~~" + bigList.get(m));
                    inspectList.add(inspectDomain);
                }

                System.out.println("inspectList" + inspectList);

            }

            System.out.println("\nread excel successfully...");
            fis.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    // write excel
    @Test
    public void testWrite() {

        // 定义表头
        String[] title = { "id", "reportTitle" };
        // 创建excel工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        // 创建工作表sheet
        XSSFSheet sheet = workbook.createSheet();
        // 创建第一行
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = null;
        // 插入第一行数据的表头
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
        }

        // 写入数据
        for (int i = 1; i <= 10; i++) {
            XSSFRow nrow = sheet.createRow(i);
            
            XSSFCell ncell = nrow.createCell(0);
            ncell.setCellValue("id" + i);

            ncell = nrow.createCell(1);
            ncell.setCellValue("reportTitle" + i);

        }
        // 创建excel文件
        File file = new File("asdfasdf.xlsx");
        try {
            // 将excel写入
            FileOutputStream stream = new FileOutputStream(file);
            workbook.write(stream);
            stream.close();
            
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    

}
 

Excel2007依赖

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.7</version>
</dependency>

猜你喜欢

转载自blog.csdn.net/zflb2008/article/details/89326467