• 使用JXLS+Excel模板制作灵活的excel导出


    前期一直卡在模板的批注上,改了很多遍的模板批注最终才成功导入,记录下方便以后寻找。

    话不多说直接上代码:

    Report

    1. package com.example.jxls.common;
    2. import java.io.IOException;
    3. import java.io.InputStream;
    4. import java.io.OutputStream;
    5. import java.util.Map;
    6. import java.util.Map.Entry;
    7. import org.jxls.common.Context;
    8. import org.jxls.util.JxlsHelper;
    9. import org.slf4j.Logger;
    10. import org.slf4j.LoggerFactory;
    11. public class Report {
    12. private static final Logger logger = LoggerFactory.getLogger(Report.class);
    13. public void createDocument(OutputStream outStream, String templateName, Map data) {
    14. logger.debug("Start creation of document");
    15. String pathTemplateName = ("/reports/").concat(templateName).concat(".xls");
    16. try(InputStream input = this.getClass().getResourceAsStream(pathTemplateName)) {//1
    17. Context context = new Context();
    18. for (Entry element : data.entrySet()) { // 2
    19. context.putVar(element.getKey(), element.getValue());
    20. }
    21. // System.out.println("context = " + context);
    22. JxlsHelper.getInstance().processTemplate(input, outStream, context); // 3
    23. } catch (Exception exception) {
    24. logger.error("Fail to generate the document", exception);
    25. } finally {
    26. closeAndFlushOutput(outStream); // 4
    27. }
    28. }
    29. private void closeAndFlushOutput(OutputStream outStream) {
    30. try {
    31. outStream.flush();
    32. outStream.close();
    33. } catch (IOException exception) {
    34. logger.error("Fail to flush and close the output", exception);
    35. }
    36. }
    37. }

     

    CommonDao

     

    1. package com.example.jxls.dao;
    2. import com.example.jxls.model.Client;
    3. import com.github.javafaker.Faker;
    4. import java.math.BigDecimal;
    5. import java.util.ArrayList;
    6. import java.util.List;
    7. public class CommonDao {
    8. public List getAllClients() {
    9. List clients = new ArrayList<>();
    10. Faker faker = new Faker();
    11. for (int i = 0; i < 20; i++) {
    12. clients.add(new Client(faker.name().firstName(),
    13. faker.name().lastName(),
    14. faker.bool().bool(),
    15. "SSN",
    16. faker.idNumber().ssnValid(),
    17. BigDecimal.valueOf(faker.number().numberBetween(1, 100000))));
    18. }
    19. return clients;
    20. }
    21. }

    UserTask

    1. package com.example.jxls.model;
    2. import lombok.Data;
    3. public class UserTask {
    4. //用户id
    5. private String id;
    6. //用户名称
    7. private String userName;
    8. //任务Id
    9. private String taskId;
    10. //插件id
    11. private String plugId;
    12. //班级id
    13. private String classId;
    14. public UserTask(String id, String userName, String taskId, String plugId, String classId) {
    15. this.id = id;
    16. this.userName = userName;
    17. this.taskId = taskId;
    18. this.plugId = plugId;
    19. this.classId = classId;
    20. }
    21. public String getId() {
    22. return id;
    23. }
    24. public void setId(String id) {
    25. this.id = id;
    26. }
    27. public String getUserName() {
    28. return userName;
    29. }
    30. public void setUserName(String userName) {
    31. this.userName = userName;
    32. }
    33. public String getTaskId() {
    34. return taskId;
    35. }
    36. public void setTaskId(String taskId) {
    37. this.taskId = taskId;
    38. }
    39. public String getPlugId() {
    40. return plugId;
    41. }
    42. public void setPlugId(String plugId) {
    43. this.plugId = plugId;
    44. }
    45. public String getClassId() {
    46. return classId;
    47. }
    48. public void setClassId(String classId) {
    49. this.classId = classId;
    50. }
    51. }

    CommonService

    1. package com.example.jxls.service;
    2. import com.example.jxls.common.Report;
    3. import com.example.jxls.dao.CommonDao;
    4. import com.example.jxls.model.UserTask;
    5. import java.io.FileNotFoundException;
    6. import java.io.FileOutputStream;
    7. import java.io.OutputStream;
    8. import java.text.ParseException;
    9. import java.text.SimpleDateFormat;
    10. import java.util.*;
    11. public class CommonService {
    12. private CommonDao dao;
    13. public CommonService() {
    14. dao = new CommonDao();
    15. }
    16. private void createCommonClientReport(String templateName, String outputName) throws FileNotFoundException, ParseException {
    17. Report report = new Report();
    18. OutputStream outStream = new FileOutputStream(outputName);
    19. List employees = generateSampleEmployeeData();
    20. Map data = new HashMap<>();
    21. // data.put("createdAt", "2021-01-01");
    22. // data.put("clients", dao.getAllClients());
    23. data.put("employees", employees);
    24. data.put("nowdate", new Date());
    25. data.put("data2", "222222qwer");
    26. report.createDocument(outStream, templateName, data);
    27. }
    28. public static List generateSampleEmployeeData() throws ParseException {
    29. List employees = new ArrayList();
    30. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd", Locale.US);
    31. employees.add( new UserTask("2L", "1970-Jul-10", "1500L", "0.15", "1500L") );
    32. employees.add( new UserTask("3L", "1973-Apr-30", "2300L", "0.15","2300L") );
    33. employees.add( new UserTask("4L", "1975-Oct-05", "2500L", "0.15","2500L") );
    34. employees.add( new UserTask("5L", "1978-Jan-07", "1700L", "0.15","1700L") );
    35. employees.add( new UserTask("6L", "1969-May-30", "2800L", "0.15","2800L") );
    36. return employees;
    37. }
    38. public void createClientReport1() throws FileNotFoundException, ParseException {
    39. createCommonClientReport("clientsTemplate", "target/clients.xls");
    40. }
    41. public void createClientReport2() throws FileNotFoundException, ParseException {
    42. createCommonClientReport("2111", "target/clients3.xls");
    43. }
    44. // public void createClientReportWithConditions() throws FileNotFoundException, ParseException {
    45. // createCommonClientReport("clientsMarkInactiveTemplate", "target/clientsMarkInactive.xls");
    46. // }
    47. }

    Application

    1. package com.example.jxls;
    2. import com.example.jxls.service.CommonService;
    3. import java.io.FileNotFoundException;
    4. import java.text.ParseException;
    5. public class Application {
    6. public static void main(String[] args) throws FileNotFoundException, ParseException {
    7. CommonService service = new CommonService();
    8. service.createClientReport2();
    9. // service.createClientReportWithConditions();
    10. }
    11. }

    demo1模板示例:

    jx:area(lastCell=”D4“)

    效果展示:

  • 相关阅读:
    QEMU DirtyLimit特性介绍
    韦东山FreeRTOS(1)ARM架构简明教程
    文科类文献综述怎么写?
    图解LeetCode——667. 优美的排列 II(难度:中等)
    IDEA社区版(Community Edition)创建Springboot-Web项目,Java
    mysql数据库常见问题及解决办法
    v-bind绑定
    理科爱好者杂志理科爱好者杂志社理科爱好者编辑部2022年第3期目录
    计算机组成原理
    BUUCTF reverse wp 71 - 75
  • 原文地址:https://blog.csdn.net/weixin_44698624/article/details/138093139