123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- /**
- * Copyright (c) 2018 佳缘科技 All rights reserved.
- * <p>
- * https://www.scjydz.com
- * <p>
- * 版权所有,侵权必究!
- */
- package com.wjp.common.utils;
- import com.alibaba.excel.EasyExcel;
- import com.alibaba.excel.converters.longconverter.LongStringConverter;
- import jakarta.servlet.http.HttpServletResponse;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.BeanUtils;
- import java.io.IOException;
- import java.net.URLEncoder;
- import java.nio.charset.StandardCharsets;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- /**
- * excel工具类
- *
- * @author Toby javatangbin@163.com
- */
- public class ExcelUtils {
- /**
- * Excel导出
- *
- * @param response response
- * @param fileName 文件名
- * @param sheetName sheetName
- * @param list 数据List
- * @param pojoClass 对象Class
- */
- public static void exportExcel(HttpServletResponse response, String fileName, String sheetName, List<?> list,
- Class<?> pojoClass) throws IOException {
- if (StringUtils.isBlank(fileName)) {
- //当前日期
- fileName = DateUtils.format(new Date());
- }
- response.setContentType("application/vnd.ms-excel");
- response.setCharacterEncoding("UTF-8");
- fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
- response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
- EasyExcel.write(response.getOutputStream(), pojoClass).registerConverter(new LongStringConverter()).sheet(sheetName).doWrite(list);
- }
- /**
- * Excel导出,先sourceList转换成List<targetClass>,再导出
- *
- * @param response response
- * @param fileName 文件名
- * @param sheetName sheetName
- * @param sourceList 原数据List
- * @param targetClass 目标对象Class
- */
- public static void exportExcelToTarget(HttpServletResponse response, String fileName, String sheetName, List<?> sourceList,
- Class<?> targetClass) throws Exception {
- List targetList = new ArrayList<>(sourceList.size());
- for (Object source : sourceList) {
- Object target = targetClass.newInstance();
- BeanUtils.copyProperties(source, target);
- targetList.add(target);
- }
- exportExcel(response, fileName, sheetName, targetList, targetClass);
- }
- }
|