From 8124c458cf477dfd4142a6b84af28d66288b585a Mon Sep 17 00:00:00 2001
From: gdg <764716047@qq.com>
Date: 星期二, 08 十二月 2020 17:00:53 +0800
Subject: [PATCH] primefaces的导出工具

---
 src/main/java/com/nanometer/smartlab/util/ExcelUtils.java |  110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 110 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/nanometer/smartlab/util/ExcelUtils.java b/src/main/java/com/nanometer/smartlab/util/ExcelUtils.java
new file mode 100644
index 0000000..ba95396
--- /dev/null
+++ b/src/main/java/com/nanometer/smartlab/util/ExcelUtils.java
@@ -0,0 +1,110 @@
+package com.nanometer.smartlab.util;
+
+import org.apache.poi.hssf.usermodel.HSSFRow;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+
+import javax.faces.context.FacesContext;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ExcelUtils {
+
+    public static HSSFWorkbook exportExcelNew(Map<String, String> map, List<Map> dataList, String name){
+        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
+        Integer total = null;
+        //sheet名字
+        HSSFSheet sheet = hssfWorkbook.createSheet(name);
+        HSSFRow titlerRow = sheet.createRow(0);
+
+        int k = 0;
+        for(Map.Entry<String, String> entry:map.entrySet()){
+            //表头
+            titlerRow.createCell(k).setCellValue(entry.getValue());
+            k++;
+        }
+        //数据
+        for (int i = 0; i < dataList.size(); i++) {
+            HSSFRow dataRow = sheet.createRow(i + 1);
+            int j = 0;
+            for(Map.Entry<String, String> entry:map.entrySet()){
+                //表头
+                dataRow.createCell(j).setCellValue(dataList.get(i).get(entry.getKey()) == null ? "" : String.valueOf(dataList.get(i).get(entry.getKey())));
+                j++;
+            }
+        }
+
+
+
+        return hssfWorkbook;
+    }
+
+
+    public static void export2Excel(List<Map> list, String name, Map<String, String> map) throws Exception {
+        if (list == null || list.size() == 0) {
+            throw new Exception("没有数据可以导出");
+        }
+
+        ServletOutputStream out = null;
+        InputStream is = null;
+        try {
+            FacesContext ctx = FacesContext.getCurrentInstance();
+            ctx.responseComplete();
+            String contentType = "application/x-download";
+            HttpServletResponse response = (HttpServletResponse) ctx
+                    .getExternalContext().getResponse();
+            response.setContentType(contentType);
+            StringBuffer contentDisposition = new StringBuffer();
+            contentDisposition.append("attachment;");
+            contentDisposition.append("filename=\"");
+            contentDisposition.append(name).append(".xls");
+            contentDisposition.append("\"");
+            response.setHeader(
+                    "Content-Disposition",
+                    new String(contentDisposition.toString().getBytes(
+                            System.getProperty("file.encoding")), "ISO8859-1"));
+            out = response.getOutputStream();
+            HSSFWorkbook hssfWorkbook = ExcelUtils.exportExcelNew(map, list, name);
+            hssfWorkbook.write(out);
+//            out.flush();
+            ctx.responseComplete();
+
+        } catch (Exception e) {
+            if (is != null) {
+                try {
+                    is.close();
+                } catch (IOException e1) {
+                    e1.printStackTrace();
+                }
+            }
+            if (out != null) {
+                try {
+                    out.close();
+                } catch (IOException e1) {
+                    e1.printStackTrace();
+                }
+            }
+            e.printStackTrace();
+        } finally {
+            if (is != null) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (out != null) {
+                try {
+                    out.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+}

--
Gitblit v1.9.2