首页 > 编程语言 > 详细

Excel处理写入(SpringBoot Mybatis-Plus)

时间:2021-05-12 00:16:46      阅读:26      评论:0      收藏:0      [点我收藏+]

gradle

compile group: ‘org.apache.poi‘, name: ‘poi‘, version: ‘3.9‘;
compile group: ‘org.apache.poi‘, name: ‘poi-excelant‘, version: ‘3.9‘;
compile group: ‘org.apache.poi‘, name: ‘poi-scratchpad‘, version: ‘3.9‘;

demo

@PostMapping(value = "/excelWrite")
    @ResponseBody
    public HttpResult excelWrite(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        HSSFSheet sheet = hssfWorkbook.createSheet();
        Row row0 = sheet.createRow(0);
        int columnIndex = 0;
        row0.createCell(columnIndex).setCellValue("");
        row0.createCell(++columnIndex).setCellValue("名称");
        row0.createCell(++columnIndex).setCellValue("规格");
        row0.createCell(++columnIndex).setCellValue("单位");
        row0.createCell(++columnIndex).setCellValue("数量");
        row0.createCell(++columnIndex).setCellValue("单价");
        row0.createCell(++columnIndex).setCellValue("总额");

        for (int i = 0; i < productList.size(); i++) {
            Product product = productList.get(i);
            Row row = sheet.createRow(i + 1);
            for (int j = 0; j < columnIndex + 1; j++) {
                row.createCell(j);
            }
            columnIndex = 0;
//            row.getCell(columnIndex).setCellValue(i + 1);
            row.getCell(++columnIndex).setCellValue(product.getName());
            row.getCell(++columnIndex).setCellValue(product.getType());
            row.getCell(++columnIndex).setCellValue(product.getNum());
        }


        FileOutputStream out = new FileOutputStream("D:\\汇总.xls");
        hssfWorkbook.write(out);
        out.flush();

        return HttpResult.ok();
    }

Excel处理写入(SpringBoot Mybatis-Plus)

原文:https://www.cnblogs.com/ideaAI/p/14757728.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!