| package com.zt.common.utils; | 
|   | 
| import cn.hutool.core.date.DateUtil; | 
| import com.alibaba.excel.EasyExcel; | 
| import com.alibaba.excel.ExcelWriter; | 
| import com.alibaba.excel.metadata.TableStyle; | 
| import com.alibaba.excel.write.metadata.WriteSheet; | 
| import com.alibaba.excel.write.metadata.style.WriteCellStyle; | 
| import org.apache.commons.beanutils.BeanUtils; | 
| import org.apache.commons.lang3.StringUtils; | 
|   | 
| import javax.servlet.http.HttpServletResponse; | 
| import java.io.IOException; | 
| import java.net.URLEncoder; | 
| import java.util.ArrayList; | 
| import java.util.Collection; | 
| import java.util.List; | 
|   | 
| import static java.lang.Thread.currentThread; | 
| import static java.lang.Thread.sleep; | 
|   | 
| /** | 
|  * excel工具类 | 
|  * | 
|  * @author Mark sunlightcs@gmail.com | 
|  */ | 
| public class ExcelUtils { | 
|   | 
|     public static void export(HttpServletResponse response, String fileName, List list, Class<?> pojoClass) | 
|             throws IOException { | 
|         if (StringUtils.isBlank(fileName)) { | 
|             // 当前日期 | 
|             fileName = DateUtil.now(); | 
|         } else { | 
|             fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20"); | 
|             fileName = fileName + "_" + DateUtil.now(); | 
|         } | 
|   | 
|         response.setContentType("application/vnd.ms-excel"); | 
|         response.setCharacterEncoding("utf-8"); | 
|         response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); | 
|         EasyExcel.write(response.getOutputStream(), pojoClass).sheet("sheet1").doWrite(list); | 
|     } | 
|   | 
|     public static void export(HttpServletResponse response, String fileName, String[] sheets, List[] lists) | 
|             throws IOException { | 
|         if (StringUtils.isBlank(fileName)) { | 
|             // 当前日期 | 
|             fileName = DateUtil.now(); | 
|         } else { | 
|             fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20"); | 
|             fileName = fileName + "_" + DateUtil.now(); | 
|         } | 
|   | 
|         response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); | 
|         response.setContentType("application/vnd.ms-excel"); | 
|         response.setCharacterEncoding("utf-8"); | 
|         ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build(); ; | 
|         try { | 
|             for (int i = 0; i < sheets.length; i++) { | 
|                 String sheet = sheets[i]; | 
|                 List datas = lists[i]; | 
|                 if (datas != null && datas.size() > 0) { | 
|                     WriteSheet writeSheet = EasyExcel.writerSheet(i, sheet).head(datas.get(0).getClass()).build(); | 
|                     excelWriter.write(datas, writeSheet); | 
|                 } | 
|             } | 
|         } finally { | 
|             // 千万别忘记finish 会帮忙关闭流 | 
|             if (excelWriter != null) { | 
|                 excelWriter.finish(); | 
|             } | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * Excel导出,先sourceList转换成List<targetClass>,再导出 | 
|      * | 
|      * @param response | 
|      *            response | 
|      * @param fileName | 
|      *            文件名 | 
|      * @param sourceList | 
|      *            原数据List | 
|      * @param targetClass | 
|      *            目标对象Class | 
|      */ | 
|     public static void exportExcelToTarget(HttpServletResponse response, String fileName, Collection<?> 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); | 
|         } | 
|   | 
|         export(response, fileName, targetList, targetClass); | 
|     } | 
|   | 
|     public static List<List<String>> parseClipboard(String clipboard) { | 
|         // 1. 将双引号替换成特殊字符串 | 
|         String text = clipboard.replace("\"\"", "goma---Hirake"); | 
|         // 2. 将引号之间的换行替换成特殊字符串 | 
|         StringBuilder sb = new StringBuilder(); | 
|         boolean inQuote = false; | 
|         for (int i=0; i< text.length(); i++) { | 
|             char chr = text.charAt(i); | 
|             if (chr == '"') { | 
|                 inQuote = !inQuote; | 
|             } | 
|             if (inQuote) { | 
|                 if (chr == '\n') { | 
|                     sb.append("goma+++Hirake"); | 
|                 } else { | 
|                     sb.append(chr); | 
|                 } | 
|             } else { | 
|                 sb.append(chr); | 
|             } | 
|         } | 
|         // 3. 切分行和列 | 
|         List<List<String>> lists = new ArrayList<>(); | 
|         String[] rows = sb.toString().split("\n"); | 
|         for (int i=0; i<rows.length; i++) { | 
|             List<String> list = new ArrayList<>(); | 
|             String[] columns = rows[i].split("\t"); | 
|             for (int j=0; j<columns.length; j++) { | 
|                 String cell = columns[j].replace("\"", ""). | 
|                         replace("goma---Hirake", "\""). | 
|                         replace("goma+++Hirake", "\n"); | 
|                 list.add(cell); | 
|             } | 
|             lists.add(list); | 
|         } | 
|   | 
|         return lists; | 
|     } | 
|   | 
|     public static void main(String[] args) { | 
|         String clipboard = "\"新增意见反馈功能:\n" + | 
|                 "1)页面标题区加意见反馈按钮,放在帮助旁\"\"\"\"\n" + | 
|                 "2)所有用户都可以看,都可以提意见反馈\n" + | 
|                 "3)填写如下内容(都是必填):意见、反馈人姓名、单位、联系方式(电话)、是否采纳(采纳/解释)、闭环情况(是/否)、备注(解释内容)。由admin用户填写是否采纳,如果是解释,则在备注中填写解释内容。\"\t2023/6/9\n" + | 
|                 "工程勘验、施工明细和修理工程单的列表:鼠标所在的行要明显些,框线要明显些,字体调大,做成18px。(投屏时看不清晰)\t2023/6/9\n" + | 
|                 "优化列表显示速度\t2023/6/9\n"; | 
|         List<List<String>> res = parseClipboard(clipboard); | 
|         for (List<String> list : res) { | 
|             for (String s : list) { | 
|                 System.out.println(s); | 
|             } | 
|         } | 
|     } | 
| } |