springboot导出数据到excel模板,使用hutool导出数据到指定excel,java写入数据到excel模板

最近遇到一个需求,需要从数据库查询数据,写入到对应的excel导入模板中。再把导出的数据进行修改,上传。
我们项目用的是easyExcel,一顿百度搜索,不得其法。
主要是要把数据填充到指定单元格中,跟平时用到的导出不一样。项目中也没有引入其它poi。

后来忽然想起来,项目中集成的有hutool,可以用hutool的导入导出功能,先把excel导入模板读取出来,再给excel模板加工数据,最后再导出。

然后又开始了百度之旅~

终于在Two thousand years later~

写一个样例:
excel导入模板长这样:(随便画得)
springboot导出数据到excel模板,使用hutool导出数据到指定excel,java写入数据到excel模板_第1张图片
springboot导出数据到excel模板,使用hutool导出数据到指定excel,java写入数据到excel模板_第2张图片
springboot导出数据到excel模板,使用hutool导出数据到指定excel,java写入数据到excel模板_第3张图片

想要的效果大概是这样:
稍微写了一点点数据,根据业务,自己完善。
springboot导出数据到excel模板,使用hutool导出数据到指定excel,java写入数据到excel模板_第4张图片
springboot导出数据到excel模板,使用hutool导出数据到指定excel,java写入数据到excel模板_第5张图片
springboot导出数据到excel模板,使用hutool导出数据到指定excel,java写入数据到excel模板_第6张图片

代码:

关于hutool版本,和导入导出所需依赖,可参考我篇文章。hutool导入导出,java使用hutool导入导出,hutool导出多级标题复杂表头,hutool大数据量导出

因为我这个项目是springboot的,所以我把excel模板放在了这里。方便读取。

springboot导出数据到excel模板,使用hutool导出数据到指定excel,java写入数据到excel模板_第7张图片

 package com.demo.controller;

import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "控制类")
@RequestMapping("/user")
public class UserController {

    @GetMapping("/download")
    public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //添加测试数据
        List<Map<String,String>> list1 = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        map.put("q1","moveNo");
        map.put("q2","moveNo");
        map.put("q3","78");
        map.put("q4","moveNo");
        map.put("q5","moveNo");
        map.put("q6","moveNo");
        map.put("q7","moveNo");
        Map<String, String> map1 = new HashMap<>();
        map1.put("q5","moveNo");
        map1.put("q6","moveNo");
        map1.put("q7","moveNo");
        list1.add(map);
        list1.add(map1);


        //添加测试数据
        List<Map<String,Object>> printVoList = new ArrayList<>();
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("COL1","123");
        hashMap.put("COL2","234");
        hashMap.put("COL3","345");
        hashMap.put("COL4","456");
        Map<String, Object> hashMap1 = new HashMap<>();
        hashMap1.put("COL1","哈");
        hashMap1.put("COL2","吧");
        hashMap1.put("COL3","吗");
        printVoList.add(hashMap);
        printVoList.add(hashMap1);


        //获取文件输入流
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("123.xlsx");

        //设置返回
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("我测试的", "utf-8") + ".xlsx");
        OutputStream out = response.getOutputStream();

        if (inputStream == null){
            throw new RuntimeException("文件未找到!");
        }

        //获取工作簿
        Workbook workbook = new XSSFWorkbook(inputStream);

        //获取创建工作簿的第一页
        Sheet sheet=workbook.getSheetAt(0);
        //给指定的sheet命名
//        workbook.setSheetName(0,"dataSheet");
        //获取当前工作簿的行数
//        int totalRows=sheet.getPhysicalNumberOfRows();
        //****遍历模板sheet,根据当中的设定进行赋值****
        int newRowIndex = 3;//起始行 从第4行开始
        for (int i=0;i<list1.size();i++) {
            Row row = sheet.createRow(newRowIndex++);
            int column = row.getLastCellNum();
            int j = 0;
            (row.getCell(j)!=null?row.getCell(j):row.createCell(j)).setCellValue(list1.get(i).get("q1"));
            j++;(row.getCell(j)!=null?row.getCell(j):row.createCell(j) ).setCellValue(list1.get(i).get("q2"));
            j++;(row.getCell(j)!=null?row.getCell(j):row.createCell(j) ).setCellValue(list1.get(i).get("q3"));
            j++;(row.getCell(j)!=null?row.getCell(j):row.createCell(j) ).setCellValue(list1.get(i).get("q4"));
            j++;(row.getCell(j)!=null?row.getCell(j):row.createCell(j) ).setCellValue(list1.get(i).get("q5"));
            j++;(row.getCell(j)!=null?row.getCell(j):row.createCell(j) ).setCellValue(list1.get(i).get("q6"));
            j++;(row.getCell(j)!=null?row.getCell(j):row.createCell(j) ).setCellValue(list1.get(i).get("q7"));
        }


        //获取创建工作簿的第2页
        Sheet sheet1=workbook.getSheetAt(1);
        //获取当前工作簿的行数
        int totalRows=sheet1.getPhysicalNumberOfRows();
        for (int i = 0;i<totalRows;i++){
            Row row = sheet1.getRow(i);
            if (row == null) continue;
            System.out.println(row.getRowNum() + JSONUtil.toJsonStr(row));;
        }
        sheet1.getRow(4).getCell(3).setCellValue("1");
        sheet1.getRow(5).getCell(3).setCellValue("1");
        //获取创建工作簿的第3页
        Sheet sheet2=workbook.getSheetAt(2);
        System.out.println("================");
        for (int i = 0;i<sheet2.getPhysicalNumberOfRows();i++){
            Row row = sheet2.getRow(i);
            if (row == null) continue;
            System.out.println(row.getRowNum() + JSONUtil.toJsonStr(row));;
        }
        //获取当前工作簿的行数
//        int totalRows2=sheet1.getPhysicalNumberOfRows();

        sheet2.getRow(2).getCell(3).setCellValue("1");
        sheet2.getRow(3).getCell(3).setCellValue("3");
        sheet2.getRow(5).getCell(3).setCellValue("111");
        sheet2.getRow(5).getCell(5).setCellValue("3333");
        sheet2.getRow(11).getCell(3).setCellValue("3333");
        sheet2.getRow(11).getCell(5).setCellValue("3333");
        /*int newRowIndex = 3;//起始行 从第4行开始
        //获取模板对应的数据表字段的值
        Row dataRow=sheet.getRow(newRowIndex++);
        //获得该行对应的字段的数量
        int columnNum=dataRow.getPhysicalNumberOfCells();

        //将结果集渲染到当前sheet当中
        for (Map pageData:printVoList) {
            sheet.shiftRows(newRowIndex, totalRows + 1, 1); //在startRow和endRow之间的行移动一行
            Row newRow = sheet.createRow(newRowIndex++);
            //创建需要插入的目标行,该值需要在每次完成一行记录值插入后重新归0
            int cellIndex = 0;

            //ic此处为cells数组的结果集的字段的位置
            for (int i = 1; i <= columnNum; i++) {
                //从pageData当中取出目标单元格需要的值
                String cellContent = pageData.get("COL"+i) != null ? String.valueOf(pageData.get("COL"+i)) : "";
                //设置目标单元格的位置和类型
                Cell cell = newRow.createCell(cellIndex++, Cell.CELL_TYPE_STRING);
                //设置目标单元格的值
                cell.setCellValue(cellContent);
            }
        }*/
        workbook.write(out);
        out.flush();
        out.close();

    }

}

你可能感兴趣的:(java,java)