天天看點

java實作建立Excel和Zip檔案的工具類,java和jsp兩種版本

java實作建立Excel和Zip檔案的工具類,java和jsp兩種版本

實作

java工具類

package com.day0417;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.List;
import java.util.Map;

public class DocumentUtil {

    private static final Logger log = LoggerFactory.getLogger(DocumentUtil.class);

    /**
     * 根據給定資料生成excel文檔
     * @param path 文檔存放路徑,絕對路徑
     * @param title 自定義标題
     * @param resultList 資料
     * @param colNames 表頭
     * @return
     */
    public static String createExcelFile(String path,String title, List<Map<String, Object>> resultList,Map<String, String> colNames){
        OutputStream out = null;
        try {
            out = new FileOutputStream(path);

            Workbook wb = new HSSFWorkbook();

            setStyleAndData4Wb(resultList, colNames,title, wb);

            wb.write(out);
            return path;
        } catch (Exception e) {
            log.error("error",e);
            return null;
        } finally {
            IOUtils.closeQuietly(out);
        }
    }

    private static void setStyleAndData4Wb(List<Map<String, Object>> resultList, Map<String, String> colNames,String title, Workbook wb) {
        Sheet sheet = wb.createSheet(title);
        sheet.setDefaultColumnWidth(20);

        //設定标題
        setSheetTitle(colNames, wb,title, sheet);

        //設定表頭
        setSheetHeader(resultList, colNames, wb, sheet);

        CellStyle cellStyle = getCellStyle(wb, false, (short) 11, Font.COLOR_NORMAL);

        for (int i = 0; i < resultList.size(); i++) {
            //内容
            Row row = sheet.createRow(i + 2);

            Map<String, Object> data = resultList.get(i);
            int j = 0;
            for (String cellTitle : colNames.keySet()) {
                Cell cell = row.createCell(j++);
                cell.setCellValue(data.get(cellTitle) == null ? "" : String.valueOf(data.get(cellTitle)));
                cell.setCellStyle(cellStyle);
            }
        }
    }

    private static void setSheetTitle(Map<String, String> colNames, Workbook wb,String title, Sheet sheet) {
        int mergeCellNum = colNames.size();

        Row row = sheet.createRow(0);
        row.setHeightInPoints((short) 30);
        Cell cell = row.createCell(0);
        cell.setCellValue(title);
        cell.setCellStyle(getCellStyle(wb, true, (short) 14, Font.COLOR_NORMAL));
        CellRangeAddress cellRangeAddress = new CellRangeAddress(0,0,0,mergeCellNum - 1);
        sheet.addMergedRegion(cellRangeAddress);
    }

    private static void setSheetHeader(List<Map<String, Object>> resultList, Map<String, String> colNames, Workbook wb, Sheet sheet) {
        if (resultList != null && resultList.size() > 0) {
            Row row = sheet.createRow(1);
            CellStyle cellStyle = getCellStyle(wb, true, (short) 12, Font.COLOR_NORMAL);
            int j = 0;
            for (Map.Entry<String, String> entry : colNames.entrySet()) {
                String value = entry.getValue();
                Cell cell = row.createCell(j++);
                cell.setCellValue(value);
                cell.setCellStyle(cellStyle);
            }
        }
    }

    private static CellStyle getCellStyle(Workbook wb, boolean isBold, short fontSize, short fontColor) {
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        Font cellFont = wb.createFont();
        cellFont.setBold(isBold);
        cellFont.setFontHeightInPoints(fontSize);
        cellFont.setColor(fontColor);

        cellStyle.setFont(cellFont);

        return cellStyle;
    }

    public static String createZipFile(String path,String sourceDir){
        FileInputStream in = null;
        ZipOutputStream out = null;
        try {
            //建立目标檔案夾
            File file = createDestFolder(path);

            //建立壓縮包
            out = new ZipOutputStream(file);

            //壓縮包内第一個檔案夾
            String innerFolder = StringUtils.substring(file.getName(),0,StringUtils.lastIndexOf(file.getName(),'.'));

            //從源檔案夾内取出檔案夾和檔案寫入壓縮包
            File sourceDirFile = new File(sourceDir);
            writeToZipFromFile(in,out,sourceDirFile,innerFolder);

            return path;
        } catch (Exception e) {
            log.error("error",e);
            return null;
        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
    }

    private static File createDestFolder(String path) {
        File file = new File(path);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        return file;
    }

    private static void writeToZipFromFile(FileInputStream in,ZipOutputStream out,File sourceDirFile,String innerFolder) throws IOException {
        for (File file : sourceDirFile.listFiles()) {
            if (file.isFile()) {
                in = new FileInputStream(file);
                ZipEntry zipEntry = new ZipEntry(innerFolder + File.separator + file.getName());
                out.putNextEntry(zipEntry);
                out.write(IOUtils.toByteArray(in));
                out.flush();
            } else if (file.isDirectory()) {
                String dirName = file.getName();
                writeToZipFromFile(in,out,file,innerFolder + File.separator + dirName);
            }
        }
    }

}


           

jsp版本工具類

<%@ page import="org.springframework.util.ObjectUtils" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="org.apache.commons.io.IOUtils" %>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFWorkbook" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.List" %>
<%@ page import="org.apache.commons.lang3.exception.ExceptionUtils" %>
<%@ page import="org.apache.poi.ss.usermodel.*" %>
<%@ page import="org.apache.poi.ss.util.CellRangeAddress" %>
<%@ page import="org.apache.tools.zip.ZipOutputStream" %>
<%@ page import="java.io.*" %>
<%@ page import="org.apache.tools.zip.ZipEntry" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.net.URLDecoder" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>documentUtil</title>
</head>
<body>

    <%
        String document_type = ObjectUtils.getDisplayString(session.getAttribute("document_type"));
        if (StringUtils.equals(document_type,"excel")) {
            String path = ObjectUtils.getDisplayString(session.getAttribute("path"));
            String title = ObjectUtils.getDisplayString(session.getAttribute("title"));
            List<Map<String, Object>> resultList = (List<Map<String, Object>>) session.getAttribute("resultList");
            Map<String, String> colNames = (Map<String, String>) session.getAttribute("colNames");
            String excelFilePath = createExcelFile(path, title, resultList, colNames);

//            out.println(excelFilePath);
            out.clear();
            out = pageContext.pushBody();

            downloadFile(request,response,excelFilePath);
        } else if (StringUtils.equals(document_type,"zip")) {
            String path = ObjectUtils.getDisplayString(session.getAttribute("path"));
            String sourceDir = ObjectUtils.getDisplayString(session.getAttribute("sourceDir"));
            String zipFilePath = createZipFile(path, sourceDir);

//            out.println(zipFilePath);
            out.clear();
            out = pageContext.pushBody();

            downloadFile(request,response,zipFilePath);
        } else {
            out.println("NO COMMAND!");
        }
    %>

    <%!
        public static void downloadFile(HttpServletRequest request, HttpServletResponse response, String path) throws IOException {
            path = URLDecoder.decode(path, "UTF-8");
            File file = new File(path);
            String filename = file.getName();

            String contentType = request.getServletContext().getMimeType(filename);
            String contentDisposition = "attachment;filename=" + URLEncoder.encode(filename, "UTF-8");
            response.setHeader("Content-Type", contentType);
            response.setHeader("Content-Disposition", contentDisposition);
            response.setHeader("result", "success");
            FileInputStream in = new FileInputStream(path);
            ServletOutputStream out = response.getOutputStream();
            IOUtils.copy(in, out);
        }
    %>

    <%!
        public static String createExcelFile(String path, String title, List<Map<String, Object>> resultList, Map<String, String> colNames){
            OutputStream _out = null;
            try {
                _out = new FileOutputStream(path);

                Workbook wb = new HSSFWorkbook();

                setStyleAndData4Wb(resultList, colNames,title, wb);

                wb.write(_out);
                return path;
            } catch (Exception e) {
                return ExceptionUtils.getStackTrace(e);
            } finally {
                IOUtils.closeQuietly(_out);
            }
        }
    %>

    <%!
        private static void setStyleAndData4Wb(List<Map<String, Object>> resultList, Map<String, String> colNames,String title, Workbook wb) {
            Sheet sheet = wb.createSheet(title);
            sheet.setDefaultColumnWidth(20);

            //設定标題
            setSheetTitle(colNames, wb,title, sheet);

            //設定表頭
            setSheetHeader(resultList, colNames, wb, sheet);

            CellStyle cellStyle = getCellStyle(wb, false, (short) 11, Font.COLOR_NORMAL);

            for (int i = 0; i < resultList.size(); i++) {
                //内容
                Row row = sheet.createRow(i + 2);

                Map<String, Object> data = resultList.get(i);
                int j = 0;
                for (String cellTitle : colNames.keySet()) {
                    Cell cell = row.createCell(j++);
                    cell.setCellValue(data.get(cellTitle) == null ? "" : String.valueOf(data.get(cellTitle)));
                    cell.setCellStyle(cellStyle);
                }
            }
        }
    %>

    <%!
        private static void setSheetTitle(Map<String, String> colNames, Workbook wb,String title, Sheet sheet) {
            int mergeCellNum = colNames.size();

            Row row = sheet.createRow(0);
            row.setHeightInPoints((short) 30);
            Cell cell = row.createCell(0);
            cell.setCellValue(title);
            cell.setCellStyle(getCellStyle(wb, true, (short) 14, Font.COLOR_NORMAL));
            CellRangeAddress cellRangeAddress = new CellRangeAddress(0,0,0,mergeCellNum - 1);
            sheet.addMergedRegion(cellRangeAddress);
        }
    %>

    <%!
        private static void setSheetHeader(List<Map<String, Object>> resultList, Map<String, String> colNames, Workbook wb, Sheet sheet) {
            if (resultList != null && resultList.size() > 0) {
                Row row = sheet.createRow(1);
                CellStyle cellStyle = getCellStyle(wb, true, (short) 12, Font.COLOR_NORMAL);
                int j = 0;
                for (Map.Entry<String, String> entry : colNames.entrySet()) {
                    String value = entry.getValue();
                    Cell cell = row.createCell(j++);
                    cell.setCellValue(value);
                    cell.setCellStyle(cellStyle);
                }
            }
        }
    %>

    <%!
        private static CellStyle getCellStyle(Workbook wb, boolean isBold, short fontSize, short fontColor) {
            CellStyle cellStyle = wb.createCellStyle();
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

            Font cellFont = wb.createFont();
            cellFont.setBold(isBold);
            cellFont.setFontHeightInPoints(fontSize);
            cellFont.setColor(fontColor);

            cellStyle.setFont(cellFont);

            return cellStyle;
        }
    %>

    <%!
        public static String createZipFile(String path,String sourceDir){
            FileInputStream in = null;
            ZipOutputStream out = null;
            try {
                //建立目标檔案夾
                File file = createDestFolder(path);

                //建立壓縮包
                out = new ZipOutputStream(file);

                //壓縮包内第一個檔案夾
                String innerFolder = StringUtils.substring(file.getName(),0,StringUtils.lastIndexOf(file.getName(),'.'));

                //從源檔案夾内取出檔案夾和檔案寫入壓縮包
                File sourceDirFile = new File(sourceDir);
                writeToZipFromFile(in,out,sourceDirFile,innerFolder);

                return path;
            } catch (Exception e) {
                return ExceptionUtils.getStackTrace(e);
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
            }
        }
    %>

    <%!
        private static File createDestFolder(String path) {
            File file = new File(path);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            return file;
        }
    %>

    <%!
        private static void writeToZipFromFile(FileInputStream in,ZipOutputStream out,File sourceDirFile,String innerFolder) throws IOException {
            for (File file : sourceDirFile.listFiles()) {
                if (file.isFile()) {
                    in = new FileInputStream(file);
                    ZipEntry zipEntry = new ZipEntry(innerFolder + File.separator + file.getName());
                    out.putNextEntry(zipEntry);
                    out.write(IOUtils.toByteArray(in));
                    out.flush();
                } else if (file.isDirectory()) {
                    String dirName = file.getName();
                    writeToZipFromFile(in,out,file,innerFolder + File.separator + dirName);
                }
            }
        }
    %>

</body>
</html>

           

java版本測試類

package com.day0417;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

import java.util.*;

public class Demo2 {

    @Test
    public void test1(){
        String json = "[\n" +
                "  {\n" +
                "    \"id_\": \"0000aef3-72a8-4239-b2ef-bf70a1ad8c25\",\n" +
                "    \"mailid_\": \"59bf2165-5bf6-4957-b73b-8d20e775f4eb\",\n" +
                "    \"title_\": \"02.台盟電子盟務工作管理平台建設項目編碼計劃書【完】\",\n" +
                "    \"hasattach_\": 1,\n" +
                "    \"senduser_\": \"501069\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"景春燕\",\n" +
                "    \"touser_\": \"501079\",\n" +
                "    \"touseraddr_\": null,\n" +
                "    \"tousername_\": null,\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"主送\",\n" +
                "    \"state_\": 0,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": null,\n" +
                "    \"createdate_\": \"2021-01-06 09:12:08\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id_\": \"000f898f-0d7f-43a3-bb1e-ca9233e9ab04\",\n" +
                "    \"mailid_\": \"a70f54e0-9761-4acf-a43d-14edb8acb3c5\",\n" +
                "    \"title_\": \"發送015\",\n" +
                "    \"hasattach_\": 0,\n" +
                "    \"senduser_\": \"501072\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"賈沁穎\",\n" +
                "    \"touser_\": null,\n" +
                "    \"touseraddr_\": \"\",\n" +
                "    \"tousername_\": \"王永樂\",\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"已發送\",\n" +
                "    \"state_\": -2,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": null,\n" +
                "    \"createdate_\": \"2021-01-22 18:07:49\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id_\": \"00599f96-556b-4a94-a891-94083e4801f9\",\n" +
                "    \"mailid_\": \"59bf2165-5bf6-4957-b73b-8d20e775f4eb\",\n" +
                "    \"title_\": \"02.台盟電子盟務工作管理平台建設項目編碼計劃書【完】\",\n" +
                "    \"hasattach_\": 1,\n" +
                "    \"senduser_\": \"501069\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"景春燕\",\n" +
                "    \"touser_\": \"501059\",\n" +
                "    \"touseraddr_\": null,\n" +
                "    \"tousername_\": null,\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"主送\",\n" +
                "    \"state_\": 0,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": null,\n" +
                "    \"createdate_\": \"2021-01-06 09:12:08\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id_\": \"00666a28-afcd-4b11-8a75-0c8aadb2c991\",\n" +
                "    \"mailid_\": \"4724c934-578c-4631-9b77-7b6c8b040700\",\n" +
                "    \"title_\": \"第二遍不能\",\n" +
                "    \"hasattach_\": 0,\n" +
                "    \"senduser_\": \"501069\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"景春燕\",\n" +
                "    \"touser_\": null,\n" +
                "    \"touseraddr_\": \"\",\n" +
                "    \"tousername_\": \"陸萍,王永樂\",\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"已發送\",\n" +
                "    \"state_\": -1,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": null,\n" +
                "    \"createdate_\": \"2021-01-22 19:07:15\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id_\": \"0171c7eb-cd14-40bb-a0cd-6f26f820be69\",\n" +
                "    \"mailid_\": \"59bf2165-5bf6-4957-b73b-8d20e775f4eb\",\n" +
                "    \"title_\": \"02.台盟電子盟務工作管理平台建設項目編碼計劃書【完】\",\n" +
                "    \"hasattach_\": 1,\n" +
                "    \"senduser_\": \"501069\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"景春燕\",\n" +
                "    \"touser_\": \"501016\",\n" +
                "    \"touseraddr_\": null,\n" +
                "    \"tousername_\": null,\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"主送\",\n" +
                "    \"state_\": 0,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": null,\n" +
                "    \"createdate_\": \"2021-01-06 09:12:08\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id_\": \"021a475d-432f-45ae-85f1-d1319eecfe35\",\n" +
                "    \"mailid_\": \"d6891ea1-aca4-4290-a4fd-e5f2ef2a0063\",\n" +
                "    \"title_\": \"林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛\",\n" +
                "    \"hasattach_\": 0,\n" +
                "    \"senduser_\": \"501069\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"景春燕\",\n" +
                "    \"touser_\": \"501019\",\n" +
                "    \"touseraddr_\": null,\n" +
                "    \"tousername_\": null,\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"主送\",\n" +
                "    \"state_\": 1,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": \"2021-01-22 18:04:58\",\n" +
                "    \"createdate_\": \"2021-01-22 18:04:30\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id_\": \"0225b8fc-d9ca-4692-8e76-a2f53589101c\",\n" +
                "    \"mailid_\": \"59bf2165-5bf6-4957-b73b-8d20e775f4eb\",\n" +
                "    \"title_\": \"02.台盟電子盟務工作管理平台建設項目編碼計劃書【完】\",\n" +
                "    \"hasattach_\": 1,\n" +
                "    \"senduser_\": \"501069\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"景春燕\",\n" +
                "    \"touser_\": \"501080\",\n" +
                "    \"touseraddr_\": null,\n" +
                "    \"tousername_\": null,\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"主送\",\n" +
                "    \"state_\": 0,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": null,\n" +
                "    \"createdate_\": \"2021-01-06 09:12:08\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id_\": \"0257a764-779b-4ac5-853d-e0bc13daabfb\",\n" +
                "    \"mailid_\": \"42bcaa5c-138a-4ced-a89a-988d268863cb\",\n" +
                "    \"title_\": \"發送001\",\n" +
                "    \"hasattach_\": 0,\n" +
                "    \"senduser_\": \"501072\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"賈沁穎\",\n" +
                "    \"touser_\": null,\n" +
                "    \"touseraddr_\": \"\",\n" +
                "    \"tousername_\": \"王永樂\",\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"已發送\",\n" +
                "    \"state_\": -2,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": null,\n" +
                "    \"createdate_\": \"2021-01-22 17:57:40\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id_\": \"02e80944-98ba-4836-a5c7-cb608766eff3\",\n" +
                "    \"mailid_\": \"329c4529-95b0-4708-ab85-763a35868423\",\n" +
                "    \"title_\": \"發送008\",\n" +
                "    \"hasattach_\": 0,\n" +
                "    \"senduser_\": \"501072\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"賈沁穎\",\n" +
                "    \"touser_\": null,\n" +
                "    \"touseraddr_\": \"\",\n" +
                "    \"tousername_\": \"王永樂\",\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"已發送\",\n" +
                "    \"state_\": -2,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": null,\n" +
                "    \"createdate_\": \"2021-01-22 18:04:17\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id_\": \"05501224-719d-4f35-aa0b-fa389d172ebd\",\n" +
                "    \"mailid_\": \"59bf2165-5bf6-4957-b73b-8d20e775f4eb\",\n" +
                "    \"title_\": \"02.台盟電子盟務工作管理平台建設項目編碼計劃書【完】\",\n" +
                "    \"hasattach_\": 1,\n" +
                "    \"senduser_\": \"501069\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"景春燕\",\n" +
                "    \"touser_\": \"501014\",\n" +
                "    \"touseraddr_\": null,\n" +
                "    \"tousername_\": null,\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"主送\",\n" +
                "    \"state_\": 0,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": null,\n" +
                "    \"createdate_\": \"2021-01-06 09:12:08\"\n" +
                "  }\n" +
                "]";
        JSONArray ja = JSON.parseArray(json);
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < ja.size(); i++) {
            JSONObject jsonObject = ja.getJSONObject(i);
            Map<String, Object> obj = new HashMap<>(jsonObject.size());
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                obj.put(key,value);
            }
            list.add(obj);
        }
        Map<String,String> colNames = new LinkedHashMap<>(3);
        colNames.put("sendusername_","發送人");
        colNames.put("title_","标題");
        colNames.put("createdate_","發送時間");
        String path = DocumentUtil.createExcelFile("C:\\1.xls", "測試", list, colNames);
        System.out.println(path);
    }

    @Test
    public void test2(){
        String zipFile = DocumentUtil.createZipFile("C:\\1\\lib.zip", "C:\\lib");
        System.out.println(zipFile);
    }

}

           

jsp版本測試類

<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="com.alibaba.fastjson.JSON" %>
<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>createexcel</title>
</head>
<body>

    <%
        String json = "[\n" +
                "  {\n" +
                "    \"id_\": \"0000aef3-72a8-4239-b2ef-bf70a1ad8c25\",\n" +
                "    \"mailid_\": \"59bf2165-5bf6-4957-b73b-8d20e775f4eb\",\n" +
                "    \"title_\": \"02.台盟電子盟務工作管理平台建設項目編碼計劃書【完】\",\n" +
                "    \"hasattach_\": 1,\n" +
                "    \"senduser_\": \"501069\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"景春燕\",\n" +
                "    \"touser_\": \"501079\",\n" +
                "    \"touseraddr_\": null,\n" +
                "    \"tousername_\": null,\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"主送\",\n" +
                "    \"state_\": 0,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": null,\n" +
                "    \"createdate_\": \"2021-01-06 09:12:08\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id_\": \"000f898f-0d7f-43a3-bb1e-ca9233e9ab04\",\n" +
                "    \"mailid_\": \"a70f54e0-9761-4acf-a43d-14edb8acb3c5\",\n" +
                "    \"title_\": \"發送015\",\n" +
                "    \"hasattach_\": 0,\n" +
                "    \"senduser_\": \"501072\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"賈沁穎\",\n" +
                "    \"touser_\": null,\n" +
                "    \"touseraddr_\": \"\",\n" +
                "    \"tousername_\": \"王永樂\",\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"已發送\",\n" +
                "    \"state_\": -2,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": null,\n" +
                "    \"createdate_\": \"2021-01-22 18:07:49\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id_\": \"00599f96-556b-4a94-a891-94083e4801f9\",\n" +
                "    \"mailid_\": \"59bf2165-5bf6-4957-b73b-8d20e775f4eb\",\n" +
                "    \"title_\": \"02.台盟電子盟務工作管理平台建設項目編碼計劃書【完】\",\n" +
                "    \"hasattach_\": 1,\n" +
                "    \"senduser_\": \"501069\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"景春燕\",\n" +
                "    \"touser_\": \"501059\",\n" +
                "    \"touseraddr_\": null,\n" +
                "    \"tousername_\": null,\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"主送\",\n" +
                "    \"state_\": 0,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": null,\n" +
                "    \"createdate_\": \"2021-01-06 09:12:08\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id_\": \"00666a28-afcd-4b11-8a75-0c8aadb2c991\",\n" +
                "    \"mailid_\": \"4724c934-578c-4631-9b77-7b6c8b040700\",\n" +
                "    \"title_\": \"第二遍不能\",\n" +
                "    \"hasattach_\": 0,\n" +
                "    \"senduser_\": \"501069\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"景春燕\",\n" +
                "    \"touser_\": null,\n" +
                "    \"touseraddr_\": \"\",\n" +
                "    \"tousername_\": \"陸萍,王永樂\",\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"已發送\",\n" +
                "    \"state_\": -1,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": null,\n" +
                "    \"createdate_\": \"2021-01-22 19:07:15\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id_\": \"0171c7eb-cd14-40bb-a0cd-6f26f820be69\",\n" +
                "    \"mailid_\": \"59bf2165-5bf6-4957-b73b-8d20e775f4eb\",\n" +
                "    \"title_\": \"02.台盟電子盟務工作管理平台建設項目編碼計劃書【完】\",\n" +
                "    \"hasattach_\": 1,\n" +
                "    \"senduser_\": \"501069\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"景春燕\",\n" +
                "    \"touser_\": \"501016\",\n" +
                "    \"touseraddr_\": null,\n" +
                "    \"tousername_\": null,\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"主送\",\n" +
                "    \"state_\": 0,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": null,\n" +
                "    \"createdate_\": \"2021-01-06 09:12:08\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id_\": \"021a475d-432f-45ae-85f1-d1319eecfe35\",\n" +
                "    \"mailid_\": \"d6891ea1-aca4-4290-a4fd-e5f2ef2a0063\",\n" +
                "    \"title_\": \"林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛林聲剛\",\n" +
                "    \"hasattach_\": 0,\n" +
                "    \"senduser_\": \"501069\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"景春燕\",\n" +
                "    \"touser_\": \"501019\",\n" +
                "    \"touseraddr_\": null,\n" +
                "    \"tousername_\": null,\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"主送\",\n" +
                "    \"state_\": 1,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": \"2021-01-22 18:04:58\",\n" +
                "    \"createdate_\": \"2021-01-22 18:04:30\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id_\": \"0225b8fc-d9ca-4692-8e76-a2f53589101c\",\n" +
                "    \"mailid_\": \"59bf2165-5bf6-4957-b73b-8d20e775f4eb\",\n" +
                "    \"title_\": \"02.台盟電子盟務工作管理平台建設項目編碼計劃書【完】\",\n" +
                "    \"hasattach_\": 1,\n" +
                "    \"senduser_\": \"501069\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"景春燕\",\n" +
                "    \"touser_\": \"501080\",\n" +
                "    \"touseraddr_\": null,\n" +
                "    \"tousername_\": null,\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"主送\",\n" +
                "    \"state_\": 0,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": null,\n" +
                "    \"createdate_\": \"2021-01-06 09:12:08\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id_\": \"0257a764-779b-4ac5-853d-e0bc13daabfb\",\n" +
                "    \"mailid_\": \"42bcaa5c-138a-4ced-a89a-988d268863cb\",\n" +
                "    \"title_\": \"發送001\",\n" +
                "    \"hasattach_\": 0,\n" +
                "    \"senduser_\": \"501072\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"賈沁穎\",\n" +
                "    \"touser_\": null,\n" +
                "    \"touseraddr_\": \"\",\n" +
                "    \"tousername_\": \"王永樂\",\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"已發送\",\n" +
                "    \"state_\": -2,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": null,\n" +
                "    \"createdate_\": \"2021-01-22 17:57:40\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id_\": \"02e80944-98ba-4836-a5c7-cb608766eff3\",\n" +
                "    \"mailid_\": \"329c4529-95b0-4708-ab85-763a35868423\",\n" +
                "    \"title_\": \"發送008\",\n" +
                "    \"hasattach_\": 0,\n" +
                "    \"senduser_\": \"501072\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"賈沁穎\",\n" +
                "    \"touser_\": null,\n" +
                "    \"touseraddr_\": \"\",\n" +
                "    \"tousername_\": \"王永樂\",\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"已發送\",\n" +
                "    \"state_\": -2,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": null,\n" +
                "    \"createdate_\": \"2021-01-22 18:04:17\"\n" +
                "  },\n" +
                "  {\n" +
                "    \"id_\": \"05501224-719d-4f35-aa0b-fa389d172ebd\",\n" +
                "    \"mailid_\": \"59bf2165-5bf6-4957-b73b-8d20e775f4eb\",\n" +
                "    \"title_\": \"02.台盟電子盟務工作管理平台建設項目編碼計劃書【完】\",\n" +
                "    \"hasattach_\": 1,\n" +
                "    \"senduser_\": \"501069\",\n" +
                "    \"senduseraddr_\": \"\",\n" +
                "    \"sendusername_\": \"景春燕\",\n" +
                "    \"touser_\": \"501014\",\n" +
                "    \"touseraddr_\": null,\n" +
                "    \"tousername_\": null,\n" +
                "    \"type_\": null,\n" +
                "    \"sendtype_\": \"主送\",\n" +
                "    \"state_\": 0,\n" +
                "    \"settop_\": 0,\n" +
                "    \"readdate_\": null,\n" +
                "    \"createdate_\": \"2021-01-06 09:12:08\"\n" +
                "  }\n" +
                "]";
        JSONArray ja = JSON.parseArray(json);
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < ja.size(); i++) {
            JSONObject jsonObject = ja.getJSONObject(i);
            Map<String, Object> obj = new HashMap<>(jsonObject.size());
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                obj.put(key,value);
            }
            list.add(obj);
        }
        Map<String,String> colNames = new LinkedHashMap<>(3);
        colNames.put("sendusername_","發送人");
        colNames.put("title_","标題");
        colNames.put("createdate_","發送時間");

        session.setAttribute("document_type","excel");
        session.setAttribute("path","/disk1/tomcat_jxjmh_txl/webapps/oa/test.xls");
        session.setAttribute("title","test");
        session.setAttribute("resultList",list);
        session.setAttribute("colNames",colNames);

        Enumeration<String> attributeNames = session.getAttributeNames();
        while (attributeNames.hasMoreElements()) {
            String element = attributeNames.nextElement();
            out.println(element);
            out.println("<br/>");
        }
    %>

</body>
</html>

           
<%@ page import="java.util.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>createzip</title>
</head>
<body>

    <%
        session.setAttribute("document_type","zip");
        session.setAttribute("path","/disk1/tomcat_jxjmh_txl/webapps/oa/test.zip");
        session.setAttribute("sourceDir","/disk1/tomcat_jxjmh_txl/webapps/oa/WEB-INF/classes");

        Enumeration<String> attributeNames = session.getAttributeNames();
        while (attributeNames.hasMoreElements()) {
            String element = attributeNames.nextElement();
            out.println(element);
            out.println("<br/>");
        }
    %>

</body>
</html>