|
|
@@ -0,0 +1,128 @@
|
|
|
+package com.skyversation.xjcy.util;
|
|
|
+
|
|
|
+import org.apache.poi.hssf.usermodel.*;
|
|
|
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.springframework.core.io.ClassPathResource;
|
|
|
+import sun.misc.BASE64Encoder;
|
|
|
+
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @ClassName fileImportData
|
|
|
+ * @Description TODO
|
|
|
+ * @Author @刘浩湉
|
|
|
+ * @Date 2022/8/8 11:16
|
|
|
+ * @Version 1.0
|
|
|
+ */
|
|
|
+public class FileIOData {
|
|
|
+
|
|
|
+ private FileIOData(){};
|
|
|
+
|
|
|
+ public static enum PreDefinedHeader {
|
|
|
+ CLUE {
|
|
|
+ @Override
|
|
|
+ public Workbook getWorkbook() {
|
|
|
+ Workbook workbook = null;
|
|
|
+ try {
|
|
|
+ workbook = WorkbookFactory.create(new ClassPathResource("excel/predefine/clue.xlsx").getInputStream());
|
|
|
+ } catch (IOException | InvalidFormatException e) {
|
|
|
+ return workbook;
|
|
|
+ }
|
|
|
+ return workbook;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<String> getHeaders() {
|
|
|
+ return Arrays.asList("#skip","#index","c_clue_source","c_clue_name","industry","industry_segment",
|
|
|
+ "industry_type","other_industry","c_enterprise_name","enterprise_type",
|
|
|
+ "c_enterprise_introduction","construction_method","land_nature","land_area",
|
|
|
+ "expected_investment","project_introduction","c_clue_status","#last_week_progress",
|
|
|
+ "responsible_leader","c_economic_zone_name","c_responsible_person_name");
|
|
|
+ }
|
|
|
+ };
|
|
|
+ public abstract Workbook getWorkbook();
|
|
|
+ public abstract List<String> getHeaders();
|
|
|
+
|
|
|
+ }
|
|
|
+ public static void exportExcelWithPreDefinedHeader(String sheetName, PreDefinedHeader header, List<Map<String,Object>> data, HttpServletRequest request, HttpServletResponse response){
|
|
|
+ Workbook workbook = createWorkbook( header, data);
|
|
|
+
|
|
|
+ response.setContentType("application/vnd.ms-excel");
|
|
|
+ try {
|
|
|
+ //获取浏览器名称
|
|
|
+ String agent=request.getHeader("user-agent");
|
|
|
+ String filename=sheetName+".xls";
|
|
|
+ //不同浏览器需要对文件名做特殊处理
|
|
|
+ if (agent.contains("Firefox")) { // 火狐浏览器
|
|
|
+ filename = "=?UTF-8?B?"
|
|
|
+ + new BASE64Encoder().encode(filename.getBytes("utf-8"))
|
|
|
+ + "?=";
|
|
|
+ filename = filename.replaceAll("\r\n", "");
|
|
|
+ } else { // IE及其他浏览器
|
|
|
+ filename = URLEncoder.encode(filename, "utf-8");
|
|
|
+ filename = filename.replace("+"," ");
|
|
|
+ }
|
|
|
+ //推送浏览器
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + filename);//UTF-8
|
|
|
+ ServletOutputStream out = response.getOutputStream();
|
|
|
+ workbook.write(out);
|
|
|
+ out.close();
|
|
|
+ workbook.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public static Workbook createWorkbook(PreDefinedHeader header, List<Map<String,Object>> data){
|
|
|
+ Workbook workbook = header.getWorkbook();
|
|
|
+ Sheet sheet = workbook.getSheet("sheet1");
|
|
|
+
|
|
|
+ Sheet hideSheet = workbook.getSheet("hide");
|
|
|
+ Row modelRow = hideSheet.getRow(0);
|
|
|
+
|
|
|
+
|
|
|
+ for (Map<String, Object> dataRow : data) {
|
|
|
+ Row row = sheet.createRow(sheet.getLastRowNum() + 1);
|
|
|
+ row.setHeight(modelRow.getHeight());
|
|
|
+ List<String> headers = header.getHeaders();
|
|
|
+ for (int i = 0, headersSize = headers.size(); i < headersSize; i++) {
|
|
|
+ String key = headers.get(i);
|
|
|
+ Cell modelCell = modelRow.getCell(i);
|
|
|
+ Cell cell = row.createCell(i);
|
|
|
+ if (modelCell != null) {
|
|
|
+ cell.setCellStyle(modelCell.getCellStyle());
|
|
|
+ }
|
|
|
+ cell.setCellValue(dataRow.getOrDefault(key,"").toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return workbook;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ List<Map<String,Object>> data = new ArrayList<>();
|
|
|
+ Map<String,Object> map = new HashMap<>();
|
|
|
+ map.put("a","a");
|
|
|
+ map.put("b","b");
|
|
|
+ map.put("c","c");
|
|
|
+ data.add(map);
|
|
|
+ Map<String,Object> map2 = new HashMap<>();
|
|
|
+ map2.put("e","a1");
|
|
|
+ map2.put("f","b1");
|
|
|
+ map2.put("z","c1");
|
|
|
+ data.add(map2);
|
|
|
+
|
|
|
+ Workbook workbook = createWorkbook(PreDefinedHeader.CLUE,data);
|
|
|
+ FileOutputStream fos = new FileOutputStream("D:\\output\\result.xlsx");
|
|
|
+ workbook.write(fos);
|
|
|
+ fos.close();
|
|
|
+ workbook.close();
|
|
|
+ }
|
|
|
+}
|