MyExcelUtil.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package com.fdkankan.ucenter.util;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.util.ObjectUtil;
  5. import com.alibaba.excel.EasyExcel;
  6. import com.alibaba.excel.ExcelWriter;
  7. import com.alibaba.excel.write.metadata.WriteSheet;
  8. import com.alibaba.excel.write.metadata.fill.FillConfig;
  9. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  10. import com.fdkankan.ucenter.entity.Invoice;
  11. import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
  12. import org.apache.poi.xssf.usermodel.XSSFDrawing;
  13. import org.apache.poi.xssf.usermodel.XSSFSheet;
  14. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Component;
  17. import javax.imageio.ImageIO;
  18. import javax.servlet.http.HttpServletResponse;
  19. import java.awt.image.BufferedImage;
  20. import java.io.*;
  21. import java.net.URLEncoder;
  22. import java.util.*;
  23. public class MyExcelUtil {
  24. //根据模板导出excel
  25. public static String excelTemplteToPdf(String localPath, String templateName, Object obj) {
  26. String fileName = templateName + UUID.randomUUID().toString().replace("-","") + ".xlsx";
  27. // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
  28. // 填充list 的时候还要注意 模板中{.} 多了个点 表示list
  29. //获取文件路径
  30. InputStream inputStream = MyExcelUtil.class.getClassLoader().getResourceAsStream("template/"+templateName+ ".xlsx");
  31. ExcelWriter excelWriter = null;
  32. String filePath = localPath+File.separator + fileName;
  33. if(!new File(filePath).getParentFile().exists()){
  34. new File(filePath).mkdirs();
  35. }
  36. excelWriter = EasyExcel.write(filePath)
  37. .withTemplate(inputStream)
  38. //调用合并策略
  39. .autoCloseStream(true)
  40. .build();
  41. WriteSheet writeSheet = EasyExcel.writerSheet().build();
  42. // 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。
  43. // forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用
  44. // 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存
  45. // 如果数据量大 list不是最后一行 参照下一个
  46. FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
  47. if(excelWriter != null && obj != null){
  48. excelWriter.fill(obj, fillConfig, writeSheet);
  49. }
  50. assert excelWriter != null;
  51. excelWriter.finish();
  52. TestForExcel2PDF.excelToPdf(filePath,filePath.replace(".xlsx",".pdf"));
  53. return filePath.replace(".xlsx",".pdf");
  54. }
  55. public static void main(String[] args) {
  56. Invoice invoice = new Invoice();
  57. invoice.setInvoiceNumber("11111111111");
  58. MyExcelUtil.excelTemplteToPdf("D:\\abc\\1212\\111","invoiceTemplate",invoice);
  59. }
  60. }