| | |
| | | package com.yuanchu.mom.utils.easyexcel; |
| | | |
| | | |
| | | import cn.hutool.core.date.DateUtil; |
| | | import com.alibaba.excel.EasyExcelFactory; |
| | | import com.alibaba.excel.ExcelReader; |
| | | import com.alibaba.excel.ExcelWriter; |
| | |
| | | import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; |
| | | import com.yuanchu.mom.exception.ExcelException; |
| | | import org.apache.poi.openxml4j.exceptions.InvalidFormatException; |
| | | import org.apache.poi.ss.formula.functions.T; |
| | | import org.apache.poi.ss.usermodel.Cell; |
| | | import org.apache.poi.ss.usermodel.*; |
| | | import org.apache.poi.xssf.usermodel.XSSFSheet; |
| | | import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.*; |
| | | import java.net.URLEncoder; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.Calendar; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | **/ |
| | | public class EasyExcelUtils { |
| | | |
| | | |
| | | public static HorizontalCellStyleStrategy getStyleStrategy() { |
| | | // 头的策略 |
| | | WriteCellStyle headWriteCellStyle = new WriteCellStyle(); |
| | |
| | | WriteFont headWriteFont = new WriteFont(); |
| | | headWriteFont.setFontHeightInPoints((short) 12); |
| | | // 字体样式 |
| | | headWriteFont.setFontName("Frozen"); |
| | | headWriteFont.setFontName("宋体"); |
| | | headWriteCellStyle.setWriteFont(headWriteFont); |
| | | // 自动换行 |
| | | headWriteCellStyle.setWrapped(false); |
| | |
| | | contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex()); |
| | | WriteFont contentWriteFont = new WriteFont(); |
| | | // 字体大小 |
| | | contentWriteFont.setFontHeightInPoints((short) 12); |
| | | contentWriteFont.setFontHeightInPoints((short) 11); |
| | | // 字体样式 |
| | | contentWriteFont.setFontName("Calibri"); |
| | | contentWriteFont.setFontName("宋体"); |
| | | contentWriteCellStyle.setWriteFont(contentWriteFont); |
| | | // 内容左对齐 |
| | | contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT); |
| | | // 内容居中对齐 |
| | | contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); |
| | | //内容垂直居中 |
| | | contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | //边框设置 |
| | | contentWriteCellStyle.setBorderTop(BorderStyle.THIN); |
| | | contentWriteCellStyle.setBorderRight(BorderStyle.THIN); |
| | | contentWriteCellStyle.setBorderBottom(BorderStyle.THIN); |
| | | contentWriteCellStyle.setBorderLeft(BorderStyle.THIN); |
| | | // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现 |
| | | return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle); |
| | | } |
| | |
| | | // 自动换行 |
| | | headWriteCellStyle.setWrapped(false); |
| | | // 水平对齐方式 |
| | | headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT); |
| | | headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); |
| | | // 垂直对齐方式 |
| | | headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | // 内容的策略 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 设置response |
| | | * @param response |
| | | * @param targetName |
| | | * @return |
| | | */ |
| | | public static HttpServletResponse getResponse(HttpServletResponse response, String targetName){ |
| | | String fileName; |
| | | try { |
| | | fileName = String.valueOf(new StringBuilder() |
| | | .append(targetName) |
| | | .append("_") |
| | | .append(DateUtil.today()) |
| | | .append(ExcelTypeEnum.XLS.getValue())); |
| | | fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString()); |
| | | response.setContentType("application/vnd.ms-excel"); |
| | | response.setHeader("Cache-Control", "no-cache"); |
| | | response.setHeader("Content-Disposition", "attachment;filename=" + fileName); |
| | | response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); |
| | | return response; |
| | | } catch (UnsupportedEncodingException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * excel文件读取 |
| | | * |
| | | * @param clazz model类型Class对象 |