Java poi操作excel文档

1. 加入依赖



    org.apache.poi
    poi-ooxml
    3.17

2. 简单生成一个xlsx文档,格式直接在xxx.xlsx文件设置好了。有兴趣希望代码设置的同学请看poi源码

public XSSFWorkbook exportExcelFile() throws Exception {
        try {
            // 加载资源文件xxx.xlsx(第一行已有标题,从第二行开始写数据),所以startline=1;
            InputStream file = ReadExecl.class.getClassLoader().getResourceAsStream("xxx.xlsx");
            XSSFWorkbook workbook = null;
            workbook = new XSSFWorkbook(file);
            file.close();
            int startLine = 1;
            Row row;
            Cell cell;
            row = workbook.getSheetAt(0).createRow(startLine);
            
            cell = row.createCell(0);
            cell.setCellValue("false");
            
            cell = row.createCell(1);
            cell.setCellValue("false");
            return workbook;
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        }
    }

 

你可能感兴趣的:(Spring)