¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.common.utils.poi; |
| | | |
| | | import cn.hutool.core.util.IdUtil; |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; |
| | | import com.ruoyi.common.utils.DictUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.file.FileUtils; |
| | | |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.net.URLEncoder; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * Excelç¸å
³å¤ç |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | public class ExcelUtils { |
| | | |
| | | /** |
| | | * 对excel表åé»è®¤ç¬¬ä¸ä¸ªç´¢å¼åè½¬æ¢ælistï¼EasyExcelï¼ |
| | | * |
| | | * @param is è¾å
¥æµ |
| | | * @return 转æ¢åéå |
| | | */ |
| | | public static <T> List<T> importExcel(InputStream is, Class<T> clazz) { |
| | | return EasyExcel.read(is).head(clazz).sheet().doReadSync(); |
| | | } |
| | | |
| | | /** |
| | | * 对listæ°æ®æºå°å
¶éé¢çæ°æ®å¯¼å
¥å°excel表åï¼EasyExcelï¼ |
| | | * |
| | | * @param list å¯¼åºæ°æ®éå |
| | | * @param sheetName å·¥ä½è¡¨çåç§° |
| | | * @return ç»æ |
| | | */ |
| | | public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, HttpServletResponse response) { |
| | | try { |
| | | String filename = encodingFilename(sheetName); |
| | | response.reset(); |
| | | response.addHeader("Access-Control-Allow-Origin", "*"); |
| | | response.addHeader("Access-Control-Expose-Headers", "Content-Disposition"); |
| | | FileUtils.setAttachmentResponseHeader(response, URLEncoder.encode(filename, StandardCharsets.UTF_8.toString())); |
| | | response.setContentType("application/vnd.ms-excel;charset=UTF-8"); |
| | | ServletOutputStream os = response.getOutputStream(); |
| | | EasyExcel.write(os, clazz) |
| | | .autoCloseStream(false) |
| | | // èªå¨éé
|
| | | .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) |
| | | .sheet(sheetName).doWrite(list); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException("导åºExcelå¼å¸¸"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è§£æå¯¼åºå¼ 0=ç·,1=女,2=æªç¥ |
| | | * |
| | | * @param propertyValue åæ°å¼ |
| | | * @param converterExp ç¿»è¯æ³¨è§£ |
| | | * @param separator åé符 |
| | | * @return è§£æåå¼ |
| | | */ |
| | | public static String convertByExp(String propertyValue, String converterExp, String separator) { |
| | | StringBuilder propertyString = new StringBuilder(); |
| | | String[] convertSource = converterExp.split(","); |
| | | for (String item : convertSource) { |
| | | String[] itemArray = item.split("="); |
| | | if (StringUtils.containsAny(separator, propertyValue)) { |
| | | for (String value : propertyValue.split(separator)) { |
| | | if (itemArray[0].equals(value)) { |
| | | propertyString.append(itemArray[1] + separator); |
| | | break; |
| | | } |
| | | } |
| | | } else { |
| | | if (itemArray[0].equals(propertyValue)) { |
| | | return itemArray[1]; |
| | | } |
| | | } |
| | | } |
| | | return StringUtils.stripEnd(propertyString.toString(), separator); |
| | | } |
| | | |
| | | /** |
| | | * ååè§£æå¼ ç·=0,女=1,æªç¥=2 |
| | | * |
| | | * @param propertyValue åæ°å¼ |
| | | * @param converterExp ç¿»è¯æ³¨è§£ |
| | | * @param separator åé符 |
| | | * @return è§£æåå¼ |
| | | */ |
| | | public static String reverseByExp(String propertyValue, String converterExp, String separator) { |
| | | StringBuilder propertyString = new StringBuilder(); |
| | | String[] convertSource = converterExp.split(","); |
| | | for (String item : convertSource) { |
| | | String[] itemArray = item.split("="); |
| | | if (StringUtils.containsAny(separator, propertyValue)) { |
| | | for (String value : propertyValue.split(separator)) { |
| | | if (itemArray[1].equals(value)) { |
| | | propertyString.append(itemArray[0] + separator); |
| | | break; |
| | | } |
| | | } |
| | | } else { |
| | | if (itemArray[1].equals(propertyValue)) { |
| | | return itemArray[0]; |
| | | } |
| | | } |
| | | } |
| | | return StringUtils.stripEnd(propertyString.toString(), separator); |
| | | } |
| | | |
| | | /** |
| | | * è§£æåå
¸å¼ |
| | | * |
| | | * @param dictValue åå
¸å¼ |
| | | * @param dictType åå
¸ç±»å |
| | | * @param separator åé符 |
| | | * @return åå
¸æ ç¾ |
| | | */ |
| | | public static String convertDictByExp(String dictValue, String dictType, String separator) { |
| | | return DictUtils.getDictLabel(dictType, dictValue, separator); |
| | | } |
| | | |
| | | /** |
| | | * ååè§£æå¼åå
¸å¼ |
| | | * |
| | | * @param dictLabel åå
¸æ ç¾ |
| | | * @param dictType åå
¸ç±»å |
| | | * @param separator åé符 |
| | | * @return åå
¸å¼ |
| | | */ |
| | | public static String reverseDictByExp(String dictLabel, String dictType, String separator) { |
| | | return DictUtils.getDictValue(dictType, dictLabel, separator); |
| | | } |
| | | |
| | | /** |
| | | * ç¼ç æä»¶å |
| | | */ |
| | | public static String encodingFilename(String filename) { |
| | | return IdUtil.fastSimpleUUID() + "_" + filename + ".xlsx"; |
| | | } |
| | | |
| | | } |