版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhengshuoa/article/details/52396687
思路:把table转成json数据传到后台(需要用到jquery.tabletojson.min.js),json转成list<list<String>>类型。利用poi导出excel
需要先导入poi jar包
前台代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>POI</title>
<meta http-equiv="pragma" content="no-cache"> <span id="transmark"></span>
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.tabletojson.js"></script>
<script type="text/javascript">
function exportExcel(fileName,tableId){
var table = $("#"+tableId).tableToJSON();
console.log(table);
var json = JSON.stringify(table);
var nodes = $("#"+tableId+" thead tr").children();
var headers = "";
$.each(nodes,function(i,item){
headers += item.innerHTML+",";
})
//调用post方法
post('${ctx}/user/p2pUserInformation/exportExcel', {fileName :fileName,headers:headers,json:json});
}
function post(url, params) {
var temp = document.createElement("form");
temp.action = url;
temp.method = "post";
temp.style.display = "none";
for (var x in params) {
var opt = document.createElement("input");
opt.name = x;
opt.value = params[x];
temp.appendChild(opt);
}
document.body.appendChild(temp);
temp.submit();
return temp;
}
</script>
</head>
<body>
<table id="test">
<thead>
<tr><td>序号</td><td>姓名/td><td>name</td></tr>
</thead>
<tr><td>1</td><td>张三</td><td>zhangsan</td></tr>
<tr><td>2</td><td>李四</td><td>lisi</td></tr>
<tr><td>3</td><td>王五</td><td>wangwu</td></tr>
</table>
<button onclick="formSubmit('ceshi','test')">Export excel 2</button><br><br>
</body>
</html>
后台servlet:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import testExport.ExportExcel;
public class TestJsonServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//json字符串
String jsonStr = request.getParameter("json");
//表格表头
String sheaders = request.getParameter("headers");
//表格标题名
String title = request.getParameter("fileName");
//表格文件名
String fileName = request.getParameter("fileName")+".xls";
ExportExcel ex = new ExportExcel();
ex.export(jsonStr,sheaders,fileName,title, response, request);
}
}
ExportExcel.java
package testExport;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFComment;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* 利用开源组件POI3.0.2动态导出EXCEL文档
*
* @version v1.0
* @param <T>
* 应用泛型,代表任意一个符合javabean风格的类
* 注意这里为了简单起见,boolean型的属性xxx的get器方式为getXxx(),而不是isXxx()
* byte[]表jpg格式的图片数据
*/
public class ExportExcel{
//文件分隔符"\"(在 UNIX 系统中是“/”)
public static final String FILE_SEPARATOR = System.getProperties().getProperty("file.separator");
/**
* @param jsonStr
* json字符串
* @param sheaders
* 表格表头
* @param fileName
* 文件名XXX.xls
* @param title
* 表格标题名
*/
public void export(String jsonStr,String sheaders,String fileName,String title,HttpServletResponse response,HttpServletRequest request) {
try {
// 将json字符串转换为json对象
JSONArray jsonArray = new JSONArray(jsonStr);
String[] headers= sheaders.substring(0,sheaders.length()-1).split(",");
System.out.println(sheaders.substring(0,sheaders.length()-1));
int iSize = jsonArray.length();
List<List> list = new ArrayList<List>();
for (int i = 0; i < iSize; i++) {
List<String> line = new ArrayList<String>();
JSONObject jsonObject = jsonArray.getJSONObject(i);
System.out.println(jsonObject.toString()+"-----");
Iterator iterator = jsonObject.keys();
String value = null;
int j=0;
while (iterator.hasNext()) {
iterator.next();
value = jsonObject.getString(headers[j]);
//表格内容
line.add(value);
j++;
System.out.println(value);
}
list.add(line);
}
for(List<String> line:list){
for(String s:line){
System.out.print(s+"\t");
}
System.out.println();
}
String docsPath = request.getSession().getServletContext().getRealPath("exportExcel");
//文件路径
String filePath = docsPath + FILE_SEPARATOR + fileName;
System.out.println(filePath);
OutputStream out = new FileOutputStream(filePath);
exportExcel(title,headers, list, out);
out.close();
JOptionPane.showMessageDialog(null, "导出成功!");
System.out.println("excel导出成功!");
//下载
download(filePath, response);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 这是一个通用的方法,利用了JAVA的反射机制,可以将放置在JAVA集合中并且符号一定条件的数据以EXCEL 的形式输出到指定IO设备上
*
* @param title
* 表格标题名
* @param headers
* 表格属性列名数组
* @param list
* 需要显示的数据集合
* @param out
* 与输出设备关联的流对象,可以将EXCEL文档导出到本地文件或者网络中
*/
private void exportExcel(String title, String[] headers,List<List> list, OutputStream out) {
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = workbook.createSheet(title);
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((short) 15);
// 生成一个样式
HSSFCellStyle style = workbook.createCellStyle();
// 设置这些样式
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成一个字体
HSSFFont font = workbook.createFont();
font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font);
// 生成并设置另一个样式
HSSFCellStyle style2 = workbook.createCellStyle();
style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 生成另一个字体
HSSFFont font2 = workbook.createFont();
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style2.setFont(font2);
// 声明一个画图的顶级管理器
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
// 定义注释的大小和位置,详见文档
HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0,
0, 0, 0, (short) 4, 2, (short) 6, 5));
// 设置注释内容
comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));
// 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.
comment.setAuthor("leno");
// 产生表格标题行
HSSFRow row = sheet.createRow(0);
for (short i = 0; i < headers.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
// 遍历集合数据,产生数据行
for(int i=0;i<list.size();i++){
List<String> line = list.get(i);
row = sheet.createRow(i+1);
for(int j=0;j<line.size();j++){
HSSFCell cell = row.createCell(j);
cell.setCellStyle(style2);
String value = line.get(j);
try {
// 判断值的类型后进行强制类型转换
String textValue = null;
// 其它数据类型都当作字符串简单处理
textValue = value.toString();
// 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成
if (textValue != null) {
HSSFRichTextString richString = new HSSFRichTextString(textValue);
HSSFFont font3 = workbook.createFont();
font3.setColor(HSSFColor.BLUE.index);
richString.applyFont(font3);
cell.setCellValue(richString);
}
} catch (SecurityException e) {
e.printStackTrace();
} finally {
// 清理资源
}
}
}
try {
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
}
}
private void download(String path, HttpServletResponse response) {
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename="+ new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/vnd.ms-excel;charset=gb2312");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}