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);
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>