• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

jeecgboot的flowable流程任务excel导出功能

武飞扬头像
宁波阿成
帮助1

       因为之前没有做这个功能,用传统的jeecgboot的excel导出功能也不好,而且不实用,所以临时做了一个excel导出,不过以后还需要完善,支持查询结果以及更多功能。

     首先建一个ExcelUtils<T>类 ,T为需要输出的对象

     下面就是输出excel函数了,主要下面参数,一个标题,一个头列名称,一个是列名,dataset是list对象数据,filename是文件名,可以随便输入(因为实际暂时也不用),最后一个是日期格式

      public void exportExcel(HttpServletResponse response, String title, String[] headers, String[] columns, Collection<T> dataset, String filename, String datePattern)

  1.  
    package org.jeecg.common.util;
  2.  
     
  3.  
    import java.io.FileOutputStream;
  4.  
    import java.io.IOException;
  5.  
    import java.io.OutputStream;
  6.  
    import java.lang.reflect.Field;
  7.  
    import java.lang.reflect.InvocationTargetException;
  8.  
    import java.lang.reflect.Method;
  9.  
    import java.net.URLEncoder;
  10.  
    import java.text.SimpleDateFormat;
  11.  
    import java.util.Collection;
  12.  
    import java.util.Date;
  13.  
    import java.util.Iterator;
  14.  
    import java.util.Objects;
  15.  
    import java.util.regex.Matcher;
  16.  
    import java.util.regex.Pattern;
  17.  
     
  18.  
    import javax.servlet.http.HttpServletResponse;
  19.  
     
  20.  
    import org.apache.poi.hssf.usermodel.HSSFCell;
  21.  
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  22.  
    import org.apache.poi.hssf.usermodel.HSSFFont;
  23.  
    import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  24.  
    import org.apache.poi.hssf.usermodel.HSSFRow;
  25.  
    import org.apache.poi.hssf.usermodel.HSSFSheet;
  26.  
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  27.  
    import org.apache.poi.hssf.util.HSSFColor;
  28.  
    import org.apache.poi.ss.usermodel.BorderStyle;
  29.  
    import org.apache.poi.ss.usermodel.FillPatternType;
  30.  
    import org.apache.poi.ss.usermodel.HorizontalAlignment;
  31.  
    import org.apache.poi.ss.usermodel.VerticalAlignment;
  32.  
    import org.springframework.beans.factory.annotation.Autowired;
  33.  
    import org.springframework.beans.factory.annotation.Value;
  34.  
    import org.springframework.stereotype.Component;
  35.  
     
  36.  
    import lombok.extern.slf4j.Slf4j;
  37.  
     
  38.  
    /**
  39.  
    * @Description: ExcelUtils<T> excel导出通用方法
  40.  
    * @Author: nbacheng
  41.  
    * @Date: 2023-03-01
  42.  
    * @Version: V1.0
  43.  
    */
  44.  
    @Slf4j
  45.  
    public class ExcelUtils<T> {
  46.  
     
  47.  
    public void exportExcel(HttpServletResponse response, String title, String[] headers, String[] columns, Collection<T> dataset, String filename, String datePattern){
  48.  
    // 声明一个工作薄
  49.  
    HSSFWorkbook workbook = new HSSFWorkbook();
  50.  
    // 生成一个表格
  51.  
    HSSFSheet sheet = workbook.createSheet(title);
  52.  
    // 设置表格默认列宽度为15个字节
  53.  
    sheet.setDefaultColumnWidth((int) 15);
  54.  
     
  55.  
    // 生成一个样式(用于标题)
  56.  
    HSSFCellStyle style = workbook.createCellStyle();
  57.  
    // 设置这些样式
  58.  
    style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.SKY_BLUE.getIndex());
  59.  
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  60.  
    style.setBorderBottom(BorderStyle.THIN);
  61.  
    style.setBorderLeft(BorderStyle.THIN);
  62.  
    style.setBorderRight(BorderStyle.THIN);
  63.  
    style.setBorderTop(BorderStyle.THIN);
  64.  
    style.setAlignment(HorizontalAlignment.CENTER);
  65.  
    // 生成一个字体
  66.  
    HSSFFont font = workbook.createFont();
  67.  
    font.setColor(HSSFColor.HSSFColorPredefined.VIOLET.getIndex());
  68.  
    font.setFontHeightInPoints((short) 12);
  69.  
    font.setBold(true);
  70.  
    // 把字体应用到当前的样式
  71.  
    style.setFont(font);
  72.  
     
  73.  
    // 生成并设置另一个样式(用于内容)
  74.  
    HSSFCellStyle style2 = workbook.createCellStyle();
  75.  
    style2.setFillForegroundColor(HSSFColor.HSSFColorPredefined.LIGHT_YELLOW.getIndex());
  76.  
    style2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  77.  
    style2.setBorderBottom(BorderStyle.THIN);
  78.  
    style2.setBorderLeft(BorderStyle.THIN);
  79.  
    style2.setBorderRight(BorderStyle.THIN);
  80.  
    style2.setBorderTop(BorderStyle.THIN);
  81.  
    style2.setAlignment(HorizontalAlignment.CENTER);
  82.  
    style2.setVerticalAlignment(VerticalAlignment.CENTER);
  83.  
    // 生成另一个字体
  84.  
    HSSFFont font2 = workbook.createFont();
  85.  
    font2.setBold(true);
  86.  
    // 把字体应用到当前的样式
  87.  
    style2.setFont(font2);
  88.  
     
  89.  
    // 产生表格标题行
  90.  
    HSSFRow row = sheet.createRow(0);
  91.  
    for (int i = 0; i < headers.length; i ) {
  92.  
    HSSFCell cell = row.createCell(i);
  93.  
    cell.setCellStyle(style);
  94.  
    HSSFRichTextString text = new HSSFRichTextString(headers[i]);
  95.  
    cell.setCellValue(text);
  96.  
    }
  97.  
     
  98.  
    // 遍历集合数据,产生数据行
  99.  
    Iterator<T> it = dataset.iterator();
  100.  
    int index = 0;
  101.  
    while (it.hasNext()) {
  102.  
    index ;
  103.  
    row = sheet.createRow(index);
  104.  
    T t = (T) it.next();
  105.  
    // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
  106.  
    //Field[] fields = t.getClass().getDeclaredFields();
  107.  
    //for (int i = 0; i < fields.length; i ) {
  108.  
    for (int i = 0; i < columns.length; i ) {
  109.  
    HSSFCell cell = row.createCell(i);
  110.  
    cell.setCellStyle(style2);
  111.  
    //Field field = fields[i];
  112.  
    //String fieldName = field.getName();
  113.  
    String fieldName = columns[i];
  114.  
    String getMethodName = "get"
  115.  
    fieldName.substring(0, 1).toUpperCase()
  116.  
    fieldName.substring(1);
  117.  
    try {
  118.  
    Class<? extends Object> tCls = t.getClass();
  119.  
    Method getMethod = tCls.getMethod(getMethodName,
  120.  
    new Class[] {});
  121.  
    Object value = getMethod.invoke(t, new Object[] {});
  122.  
    // 判断值的类型后进行强制类型转换
  123.  
    String textValue = null;
  124.  
     
  125.  
    if (value instanceof Boolean) {
  126.  
    boolean bValue = (Boolean) value;
  127.  
    textValue = "男";
  128.  
    if (!bValue) {
  129.  
    textValue = "女";
  130.  
    }
  131.  
    } else if (value instanceof Date) {
  132.  
    Date date = (Date) value;
  133.  
    SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
  134.  
    textValue = sdf.format(date);
  135.  
    } else {
  136.  
    // 其它数据类型都当作字符串简单处理
  137.  
    if(Objects.nonNull(value)) {
  138.  
    textValue = value.toString();
  139.  
    }
  140.  
    else {
  141.  
    textValue = "";
  142.  
    }
  143.  
     
  144.  
    }
  145.  
    // 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成
  146.  
    if (textValue != null) {
  147.  
    Pattern p = Pattern.compile("^//d (//.//d )?$");
  148.  
    Matcher matcher = p.matcher(textValue);
  149.  
    if (matcher.matches()) {
  150.  
    // 是数字当作double处理
  151.  
    cell.setCellValue(Double.parseDouble(textValue));
  152.  
    } else {
  153.  
    HSSFRichTextString richString = new HSSFRichTextString(
  154.  
    textValue);
  155.  
    HSSFFont font3 = workbook.createFont();
  156.  
    //font3.setColor(HSSFColor.BLUE.index);
  157.  
    richString.applyFont(font3);
  158.  
    cell.setCellValue(richString);
  159.  
    }
  160.  
    }
  161.  
    } catch (SecurityException e) {
  162.  
    e.printStackTrace();
  163.  
    } catch (NoSuchMethodException e) {
  164.  
    e.printStackTrace();
  165.  
    } catch (IllegalArgumentException e) {
  166.  
    e.printStackTrace();
  167.  
    } catch (IllegalAccessException e) {
  168.  
    e.printStackTrace();
  169.  
    } catch (InvocationTargetException e) {
  170.  
    e.printStackTrace();
  171.  
    } finally {
  172.  
    // 清理资源
  173.  
    }
  174.  
    }
  175.  
    }
  176.  
    try {
  177.  
    //OutputStream out = new FileOutputStream("/opt/upFiles/" filename);
  178.  
    //workbook.write(out);
  179.  
    response.setCharacterEncoding("UTF-8");
  180.  
    response.setHeader("content-Type", "application/vnd.ms-excel");
  181.  
    response.setHeader("Content-Disposition",
  182.  
    "attachment;filename=" URLEncoder.encode(filename, "UTF-8"));
  183.  
    workbook.write(response.getOutputStream());
  184.  
    //out.close();
  185.  
    log.info("导出成功");
  186.  
    } catch (IOException e) {
  187.  
    e.printStackTrace();
  188.  
    }finally{
  189.  
    try {
  190.  
    workbook.close();
  191.  
    } catch (IOException e) {
  192.  
    e.printStackTrace();
  193.  
    }
  194.  
    }
  195.  
    }
  196.  
    }

调用的例子

  1.  
    /**
  2.  
    * 导出excel
  3.  
    *
  4.  
    * @param request
  5.  
    * @param HttpServletResponse response, FlowTaskDto flowTaskDto
  6.  
    */
  7.  
    @RequestMapping(value = "/myExportXls")
  8.  
    public void myExportXls(HttpServletResponse response, FlowTaskDto flowTaskDto) {
  9.  
     
  10.  
    String[] headers = { "任务编号", "流程名称", "流程类别", "流程版本", "业务主键", "提交时间","流程状态","耗时","当前节点","办理"};
  11.  
    String[] columns = { "procInsId","procDefName","category","procDefVersion","businessKey","createTime","finishTime","duration","taskName","assigneeName"};
  12.  
    List<FlowTaskDto> listflowtask = ((Page<FlowTaskDto>)flowTaskService.myProcessNew(1, 10, flowTaskDto).getResult()).getRecords();
  13.  
    ExcelUtils<FlowTaskDto> eu = new ExcelUtils<FlowTaskDto>();
  14.  
    eu.exportExcel(response, "标题", headers, columns, listflowtask, "test.xls", "yyyy-MM-dd HH:mm:ss");//目前这个文件名没有什么用,前端传过来会修改掉
  15.  
    }

前端修改一下地址就可以:

如:    exportXlsUrl: "/flowable/task/myExportXls",

实际效果如下:

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhghabke
系列文章
更多 icon
同类精品
更多 icon
继续加载