背景:本次实现基于原生JDBC连接数据库、Struts2框架。
1.JDBC连接数据库部分比较简单,就不详细介绍,简述:创建一个JavaBean类,连接数据库获取该JavaBean对象的集合;
2.创建通用工具类,用于生成Excel模板:
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelUtil {
/*
* 导出Excel
*/
public static HSSFWorkbook getHSSFWorkbook(String sheetName,
String[] title, String[][] values, HSSFWorkbook wb) {
// 第一步,创建一个HSSFWorkbook,对应一个Excel文件
if (wb == null) {
wb = new HSSFWorkbook();
}
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel行数列数有限制
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 声明列对象
HSSFCell cell = null;
// 创建标题
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
// 创建内容
for (int i = 0; i < values.length; i++) {
row = sheet.createRow(i + 1);
for (int j = 0; j < values[i].length; j++) {
// 将内容按顺序赋给对应的列对象
row.createCell(j).setCellValue(values[i][j]);
}
}
return wb;
}
}
3.Action层,调用Dao层方法和Util层方法将数据转成Excel输出:
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import com.kun.bean.Goods;
import com.kun.dao.TestQuery;
import com.kun.util.ExcelUtil;
import com.opensymphony.xwork2.ActionSupport;
public class Test extends ActionSupport {
private static final long serialVersionUID = 2543844745175758118L;
private String goodsid;
public String getGoodsid() {
return goodsid;
}
public void setGoodsid(String goodsid) {
this.goodsid = goodsid;
}
public void export() {
// HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
// 获取数据
TestQuery testQuery = new TestQuery();
List<Goods> list = testQuery.queryGoods();
//Excel标题
String[] title = {"商品编码","商品名称"};
//Excel文件名
String fileName = "烟草商品信息表" + System.currentTimeMillis() + ".xls";
//sheet名
String sheetName = "小宝";
String[][] content = new String[list.size()][];
for(int i=0; i<list.size(); i++){
content[i] = new String[title.length];
Goods g = list.get(i);
content[i][0] = g.getGoodsid();
content[i][1] = g.getGoodsname();
}
//创建HSSFWorkbook
HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
try {
this.setResponseHeader(response, fileName);
OutputStream out = response.getOutputStream();
wb.write(out);
out.flush();
out.close();
wb.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//发送响应流方法
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
/* try {
fileName = new String(fileName.getBytes(),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
4.JSP页面表单提交请求:
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="user!export.action" method="post">
<input id="gi" name="goodsid" type="hidden" value="" />
<button id="excel" type="submit">导出Excel</button>
</form>
</body>
</html>
注意:如果遇到异常java.lang.NoSuchMethodException;
解决方式:
①检查Action类是否实现ActionSupport接口,Action类中是否有调用的方法;
②检查jar包、该方法是否public修饰,是否有传参(Struts2不允许方法有参数);
③清理Tomcat缓存,重新部署项目。
总结:现阶段对于JQuery和Spring框架不熟悉,先用这样的方式将功能实现。