定制化开发使用EasyPoi导出Excel

1、引入Java包依赖

  1. <dependency>
  2. <groupId>cn.afterturn</groupId>
  3. <artifactId>easypoi-base</artifactId>
  4. <version>3.2.0</version>
  5. </dependency>

2、定制化开发创建导出的Excel样式类:

  1. import org.apache.poi.ss.usermodel.BorderStyle;
  2. import org.apache.poi.ss.usermodel.CellStyle;
  3. import org.apache.poi.ss.usermodel.FillPatternType;
  4. import org.apache.poi.ss.usermodel.Font;
  5. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  6. import org.apache.poi.ss.usermodel.IndexedColors;
  7. import org.apache.poi.ss.usermodel.VerticalAlignment;
  8. import org.apache.poi.ss.usermodel.Workbook;
  9. import cn.afterturn.easypoi.excel.export.styler.AbstractExcelExportStyler;
  10. import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;
  11. /**
  12. * @ClassName: ExcelExportMyStylerImpl
  13. * @Description: 定制化开发自定义报表导出样式,定制化开发可以修改表头颜色,高度等
  14. * @Author: sunt
  15. * @Date: 2019/8/29 21:39
  16. * @Version 1.0
  17. **/
  18. public class ExcelExportMyStylerImpl extends AbstractExcelExportStyler implements IExcelExportStyler {
  19. public ExcelExportMyStylerImpl(Workbook workbook) {
  20. super.createStyles(workbook);
  21. }
  22. @Override
  23. public CellStyle getTitleStyle(short color) {
  24. CellStyle titleStyle = workbook.createCellStyle();
  25. Font font = workbook.createFont();
  26. font.setBold(true);// 加粗
  27. titleStyle.setFont(font);
  28. titleStyle.setAlignment(HorizontalAlignment.CENTER);// 居中
  29. titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
  30. titleStyle.setFillForegroundColor(IndexedColors.AQUA.index);// 设置颜色
  31. titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  32. titleStyle.setBorderRight(BorderStyle.THIN);
  33. titleStyle.setWrapText(true);
  34. return titleStyle;
  35. }
  36. @SuppressWarnings("deprecation")
  37. @Override
  38. public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) {
  39. CellStyle style = workbook.createCellStyle();
  40. style.setAlignment(CellStyle.ALIGN_CENTER);
  41. style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
  42. style.setDataFormat(STRING_FORMAT);
  43. if (isWarp) {
  44. style.setWrapText(true);
  45. }
  46. return style;
  47. }
  48. @Override
  49. public CellStyle getHeaderStyle(short color) {
  50. CellStyle titleStyle = workbook.createCellStyle();
  51. Font font = workbook.createFont();
  52. font.setBold(true);// 加粗
  53. font.setColor(IndexedColors.RED.index);
  54. font.setFontHeightInPoints((short) 11);
  55. titleStyle.setFont(font);
  56. titleStyle.setAlignment(HorizontalAlignment.CENTER);// 居中
  57. titleStyle.setFillForegroundColor(IndexedColors.WHITE.index);// 设置颜色
  58. titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
  59. titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  60. titleStyle.setBorderRight(BorderStyle.THIN);
  61. titleStyle.setWrapText(true);
  62. return titleStyle;
  63. }
  64. @SuppressWarnings("deprecation")
  65. @Override
  66. public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) {
  67. CellStyle style = workbook.createCellStyle();
  68. style.setAlignment(CellStyle.ALIGN_CENTER);
  69. style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
  70. style.setDataFormat(STRING_FORMAT);
  71. if (isWarp) {
  72. style.setWrapText(true);
  73. }
  74. return style;
  75. }
  76. }

 

3、定制化开发创建核心导出工具类

  1. import cn.afterturn.easypoi.excel.ExcelExportUtil;
  2. import cn.afterturn.easypoi.excel.entity.ExportParams;
  3. import com.sunny.spring.boot.poi.common.ExcelExportMyStylerImpl;
  4. import com.sunny.spring.boot.poi.pojo.StudentInfoBean;
  5. import org.apache.poi.ss.formula.functions.T;
  6. import org.apache.poi.ss.usermodel.Workbook;
  7. import javax.servlet.ServletOutputStream;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.net.URLEncoder;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Collection;
  12. import java.util.Date;
  13. import java.util.List;
  14. /**
  15. * @ClassName: ExcelExportUtil
  16. * @Description: Exceld导出工具类
  17. * @Author: sunt
  18. * @Date: 2019/8/30 14:49
  19. * @Version 1.0
  20. **/
  21. public class MyExcelExportUtil {
  22. /**
  23. * Excel文件导出,导出的文件名默认为:headTitle+当前系统时间
  24. * @param listData 要导出的list数据
  25. * @param pojoClass 定义excel属性信息
  26. * @param headTitle Excel文件头信息
  27. * @param sheetName Excel文件sheet名称
  28. * @param response
  29. */
  30. public static void exportExcel(Collection<?> listData,Class<?> pojoClass, String headTitle, String sheetName, HttpServletResponse response) {
  31. ExportParams params = new ExportParams(headTitle, sheetName);
  32. params.setHeight((short) 8);
  33. params.setStyle(ExcelExportMyStylerImpl.class);
  34. try {
  35. Workbook workbook = ExcelExportUtil.exportExcel(params, pojoClass, listData);
  36. String fileName = headTitle + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
  37. fileName = URLEncoder.encode(fileName, "UTF8");
  38. response.setContentType("application/vnd.ms-excel;chartset=utf-8");
  39. response.setHeader("Content-Disposition", "attachment;filename="+fileName + ".xls");
  40. ServletOutputStream out=response.getOutputStream();
  41. workbook.write(out);
  42. out.flush();
  43. out.close();
  44. } catch (Exception e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. }

 

4、创建导出对象实体Bean

注意日期类型 注解内要加上: exportFormat = "yyyy-MM-dd hh:mm:ss"

  1. import cn.afterturn.easypoi.excel.annotation.Excel;
  2. import com.baomidou.mybatisplus.annotation.TableField;
  3. import com.baomidou.mybatisplus.annotation.TableId;
  4. import com.baomidou.mybatisplus.annotation.TableName;
  5. import lombok.Data;
  6. import lombok.EqualsAndHashCode;
  7. import lombok.experimental.Accessors;
  8. import java.io.Serializable;
  9. import java.math.BigDecimal;
  10. /**
  11. * <p>
  12. * 学生基本信息表
  13. * </p>
  14. *
  15. * @author sunt
  16. * @since 2019-08-29
  17. */
  18. @Data
  19. @EqualsAndHashCode(callSuper = false)
  20. @Accessors(chain = true)
  21. @TableName("T_STUDENT")
  22. public class StudentInfoBean implements Serializable {
  23. private static final long serialVersionUID = 1L;
  24. /**
  25. * 学号
  26. */
  27. @TableId("ID")
  28. @Excel(name = "学号", width = 20, orderNum = "1")
  29. private String id;
  30. /**
  31. * 姓名
  32. */
  33. @TableField("NAME")
  34. @Excel(name = "姓名", width = 20, orderNum = "2")
  35. private String name;
  36. /**
  37. * 性别(1:男 2:女)
  38. * replace:导出是{a_id,b_id} 导入反过来,注意大括号里面单独引号引起来的
  39. */
  40. @TableField("SEX")
  41. @Excel(name = "性别", width = 20, replace = { "男_1", "女_2" },orderNum = "3")
  42. private String sex;
  43. /**
  44. * 年龄
  45. */
  46. @TableField("AGE")
  47. @Excel(name = "年龄", width = 20, orderNum = "4")
  48. private Integer age;
  49. /**
  50. * 出生日期
  51. */
  52. @TableField("BIRTHDAY")
  53. @Excel(name = "商品创建时间", width = 20, orderNum = "12",exportFormat = "yyyy-MM-dd hh:mm:ss")
  54. private String birthday;
  55. /**
  56. * 入学时间
  57. */
  58. @TableField("REGIST_DATE")
  59. @Excel(name = "入学时间",width = 20,orderNum = "6")
  60. private String registDate;
  61. /**
  62. * 学费
  63. */
  64. @TableField("FEE")
  65. @Excel(name = "学费", width = 20, orderNum = "7")
  66. private BigDecimal fee;
  67. }

 

属性字段属性值
@TableField这个字段代表数据库表的字段
@Excelname代表导出Excel列名称
@ExcelorderNum代表Excel列排在第几列
@Excelreplace一般数据库存的性别例如0和1,导出的值0展示为男性,女展示为女性

 

5、后台方法:

直接调用查询方法,返回给前台就OK

  1. @RequestMapping("/exportStudent")
  2. public void exportStudent(HttpServletResponse response) {
  3. try {
  4. List<StudentInfoBean> sutdentList = studentService.queryAllStudent();
  5. MyExcelExportUtil.exportExcel(sutdentList,StudentInfoBean.class,"学生基本信息","新生入学信息",response);
  6. } catch (Exception e) {
  7. e.printStackTrace();
  8. }
  9. }

6、前台的方法

不能使用ajax方法,返回的是字符串,后台返回的是流,如果用ajax返回的是乱码,并且浏览器不下载

  1. //导出excel
  2. excel(){
  3. window.open("http://localhost:88/api/shop/shop/exportShop")
  4. },

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发