使用poi通过excel模板导出excel数据

期望:用预先的xlsx的Excel表格模板

参考:两篇博客

步骤:

模板+数据:


相关jar报 坐标:(注意jar版本 否则再

tempWorkBook = new XSSFWorkbook(inputstream);

会报  classnotFoundeException!!!!!)\

pom:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.8</version>
</dependency>

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

<dependency>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>2.3.0</version>
</dependency>

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.1</version>
</dependency>

代码:

@RequestMapping(value = "/export",method = RequestMethod.POST)
public void ExportExcell(HttpServletRequest request, HttpServletResponse response){
    try {
    ExcellUtil excellUtil=new ExcellUtil();
    List<Map<String,String>> data=new ArrayList<>();
    for (int i=0; i<2 ;i++) {
        Map map=new HashMap();
        map.put("preAmount","2.0");
        map.put("plusAmount","2.0");
        map.put("reduceAmount","2.0");
        map.put("afterAmount","2.0");
        map.put("accountsType","66");
        data.add(map);
    }
        File file=new File("D:\\test.xlsx");
        if (!file.exists()){
            file.createNewFile();
        }
        OutputStream outputStream=new FileOutputStream(file);
        excellUtil.createExcel(data,outputStream,request);
    } catch (Exception e) {
        e.printStackTrace();
    }


}

excelUtils.java

public void createExcel(List<Map<String, String>> list, OutputStream output,HttpServletRequest request) {

    Workbook tempWorkBook = null;
    Sheet tempSheet = null;
    int rowIndex = 5;
    Row tempRow = null;
    Cell tempCell = null;
    InputStream inputstream = null;
    try {
        //inputstream = request.getSession().getServletContext().getResourceAsStream("/templates/test.xlsx");
        //inputstream=ExcellUtil.class.getResourceAsStream("/templates/test.xlsx");
        String path = ExcellExportController.class.getResource("/").getPath() + "/templates/test.xlsx";
        File file=new File(path);
        boolean exists = file.exists();
        System.out.println(exists);
        FileInputStream fileInputStream=new FileInputStream(file);
        inputstream=fileInputStream;
        // 获取模板
        tempWorkBook = new XSSFWorkbook(inputstream);
        // 获取模板sheet        tempSheet = tempWorkBook.getSheetAt(0);

        // 用于统计的变量
        double preAmount = 0;
        double plusAmount = 0;
        double reduceAmount = 0;
        double afterAmount = 0;

        // 将数据写入excel
        for (int i = 0; i < list.size(); i++) {

            int cellNo = 0;
            // 获取指定行
            tempRow = tempSheet.getRow(0);

            //获取当前sheet最后一行数据对应的行索引
            int currentLastRowIndex = tempSheet.getLastRowNum();
            int newRowIndex = currentLastRowIndex + 1;
            XSSFRow newRow = (XSSFRow) tempSheet.createRow(newRowIndex);
            //开始创建并设置该行每一单元格的信息,该行单元格的索引从 0 开始
            int cellIndex = 0;

            //创建一个单元格,设置其内的数据格式为字符串,并填充内容,其余单元格类同
            XSSFCell newNameCell = newRow.createCell(cellIndex++, Cell.CELL_TYPE_STRING);
            newNameCell.setCellValue(list.get(i).get("accountsType"));
            XSSFCell newGenderCell = newRow.createCell(cellIndex++, Cell.CELL_TYPE_STRING);
            newGenderCell.setCellValue(list.get(i).get("preAmount"));
            XSSFCell newAgeCell = newRow.createCell(cellIndex++, Cell.CELL_TYPE_NUMERIC);
            newAgeCell.setCellValue(list.get(i).get("curpreAmount"));
            XSSFCell newAddressCell = newRow.createCell(cellIndex++, Cell.CELL_TYPE_NUMERIC);
            newAddressCell.setCellValue(list.get(i).get("reduceAmount"));

           

        }
        // 将内容写入Excel
        tempWorkBook.write(output);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}至此结束。



猜你喜欢

转载自blog.csdn.net/qq_27727251/article/details/79364533