springboot 使用poi根据excel模版写入新的数据,如何使用POI导出复杂的excel

    最近在做 Excel 导出的时候,需要导出固定格式的excel,比如订单格式,如下所示

需要往里面填充公司信息 客户信息 和订单信息,完成后的excel如下

这里金额合计 大写 小写 是excel自动计算的 ,首先需要的maven组件如下

   <!-- excel工具 -->

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

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

  这里是业务层的代码示例

// 模板路径 如 D:\home\app\模板.xlsx        
String filePath = RuoYiConfig.getDownloadPath()+"/送货单模板.xlsx";
        File file = new File(filePath);
        FileInputStream in =new FileInputStream(file);
        //读取excel模板
        XSSFWorkbook wb = new XSSFWorkbook(in);
        //读取了模板内所有sheet内容
        XSSFSheet sheet = wb.getSheetAt(0);
        //如果这行没有了,整个公式都不会有自动计算的效果的
        sheet.setForceFormulaRecalculation(true);

        // 这里是查询填充模板需要的信息  换成你自己的
        orderInfo.setId((long) 18);
        OrderInfo info = orderInfoService.selectOrderInfoById(orderInfo.getId());
        List<OrderDetailInfo> list=info.getList();
        // 客户信息
        CostomInfo costomInfo=costomInfoService.selectCostomInfoById(info.getcId());
        // 公司信息
        List<CompanyInfo> list1=companyInfoService.selectCompanyInfoList(null);
        // 设置表头公司信息
        sheet.getRow(0).getCell(0).setCellValue(list1.get(0).getCompanyName());
        sheet.getRow(1).getCell(0).setCellValue(list1.get(0).getCompanyAdress());
        sheet.getRow(2).getCell(0).setCellValue(list1.get(0).getPhoneNumber());
        // 设置客户信息
        sheet.getRow(3).getCell(8).setCellValue(info.getOrderNo());
        //sheet.getRow(5).getCell(8).setCellValue("2020/08/15");
        sheet.getRow(4).getCell(1).setCellValue(costomInfo.getCustomName());
        sheet.getRow(4).getCell(4).setCellValue(costomInfo.getCustomPhone());
        sheet.getRow(5).getCell(1).setCellValue(costomInfo.getCustomAddress());
        sheet.getRow(5).getCell(4).setCellValue(costomInfo.getCustomPerson());

        int j = 7;
        // 订单明细列表信息 从第7行开始插入明细
        for (int i = 0; i < list.size() ; i++) {
            sheet.getRow( (j+i) ).getCell(1).setCellValue(
                    productInfoService.selectProductInfoById(list.get(i).getpId()).getProductName());
            sheet.getRow((j+i)).getCell(2).setCellValue(list.get(i).getSpecs());
            sheet.getRow((j+i)).getCell(3).setCellValue("KG");
            sheet.getRow((j+i)).getCell(4).setCellValue(list.get(i).getNumber());
            sheet.getRow((j+i)).getCell(5).setCellValue(list.get(i).getPrice());
            sheet.getRow((j+i)).getCell(6).setCellValue(Integer.parseInt(list.get(i).getTotalPrice()));
        }


        // 保存文件的路径
        //String realPath = "D:\\app\\uploadPath\\download\\";

        String newFileName = UUID.randomUUID().toString() + "_" + "送货单" + ".xlsx";

        //修改模板内容导出新模板 
        FileOutputStream out = new FileOutputStream(getAbsoluteFile(newFileName));
        wb.write(out);
        out.close();

String filePath = RuoYiConfig.getDownloadPath()+"/送货单模板.xlsx";   是模板路径,可以写成你本地的模板路径

getAbsoluteFile 方式是获取下载的文件路径 ,可以写成 realPath 

这里是Test 测试,可以先测试再写serveice

package com.ruoyi.project.tool;


import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;


/**
 * @version V1.0
 * @author: hqk
 * @date: 2020/8/12 17:52
 * @Description:
 */
public class Test {

    public static void main(String[] args) throws Exception {

        //excel模板路径

        String filePath = "D:\\送货单模板.xlsx";
        File file = new File(filePath);
        FileInputStream in =new FileInputStream(file);
        //读取excel模板
        XSSFWorkbook wb = new XSSFWorkbook(in);
        //读取了模板内所有sheet内容
        XSSFSheet sheet = wb.getSheetAt(0);
        //如果这行没有了,整个公式都不会有自动计算的效果的
        sheet.setForceFormulaRecalculation(true);

        sheet.getRow(0).getCell(0).setCellValue("摩尔化学");
        //在相应的单元格进行赋值
        sheet.getRow(3).getCell(8).setCellValue("N018922");
        sheet.getRow(5).getCell(8).setCellValue("2020/08/15");
        sheet.getRow(4).getCell(1).setCellValue("长泰吴彦祖");
        sheet.getRow(4).getCell(4).setCellValue("17521311027");

        sheet.getRow(5).getCell(1).setCellValue("上海市浦东新区芳华路");

        sheet.getRow(12).getCell(5).setCellValue("");
        sheet.getRow(12).getCell(6).setCellValue(3);
        sheet.getRow(12).getCell(7).setCellValue(4);

        sheet.getRow(13).getCell(5).setCellValue("再来");
        sheet.getRow(13).getCell(6).setCellValue(100);
        sheet.getRow(13).getCell(7).setCellValue(4);


        // 保存文件的路径
        String realPath = "D:\\app\\uploadPath\\download\\";

        String newFileName = "report-1" +".xlsx";

        //修改模板内容导出新模板
        FileOutputStream out = new FileOutputStream(realPath+newFileName);
        wb.write(out);
        out.close();

    }

}

文章参考

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值