最近在做 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();
}
}