首页 > 代码库 > Java学习之道:Java操作Excel之导出下载
Java学习之道:Java操作Excel之导出下载
页面放置一个button进行点击导出事件
<h:commandLink target="_parent" value="http://www.mamicode.com/导出"
action="#{eveluatesubject.doSaveExcel}"
styleClass="cssButtonBG">
</h:commandLink>
后台的导出方法
/**
* 导出EXCEL
*
* @param request
* @return
*/
public String doSaveExcel() {
HttpServletRequest request = (HttpServletRequest) FacesContext
.getCurrentInstance().getExternalContext().getRequest();
dooutof(request);
return null;
}
public String dooutof(HttpServletRequest request) {
// 文件名与路径
String realPath = request.getRealPath("");
String fileName = "导出的文件名称.xls";
File file = new File(realPath + "//xxfzpj//tempPath//");//导出文件存放的位置
if (!file.exists()) {
file.mkdirs();
}
realPath = realPath + "//xxfzpj//tempPath//" + fileName;
// 建立工作薄并写表头
try {
WritableWorkbook wwb = Workbook.createWorkbook(new File(realPath));
WritableSheet ws = wwb.createSheet("Sheet1", 0);// 建立工作簿
// 写表头
jxl.write.Label label1 = new jxl.write.Label(0, 0, "列1");
jxl.write.Label label2 = new jxl.write.Label(1, 0, "列2");
jxl.write.Label label3 = new jxl.write.Label(2, 0, "列3");
jxl.write.Label label4 = new jxl.write.Label(3, 0, "列4");
jxl.write.Label label5 = new jxl.write.Label(4, 0, "列5");
ws.addCell(label1);// 放入工作簿
ws.addCell(label2);
ws.addCell(label3);
ws.addCell(label4);
ws.addCell(label5);
// 写入信息
List<Map> listmsg = 数据库中读出的内容(运行SQL);
for (int i = 0; i < listmsg.size(); i++) {
label1 = new jxl.write.Label(0, i + 1, listmsg.get(i).get(
"subject_id").toString().trim());// 建立第一列
label2 = new jxl.write.Label(1, i + 1, listmsg.get(i).get(
"subject_name").toString().trim());// 建立第二列
if (listmsg.get(i).get("object_name") != null
&& "" != listmsg.get(i).get("object_name")) {
label3 = new jxl.write.Label(2, i + 1, listmsg.get(i).get(
"object_name").toString().trim());// 建立第三列
} else {
label3 = new jxl.write.Label(2, i + 1, "未定义评价对象名称!");
}
label4 = new jxl.write.Label(3, i + 1, listmsg.get(i).get(
"mode_name").toString().trim());// 建立第四列
label5 = new jxl.write.Label(4, i + 1, listmsg.get(i).get(
"type_name").toString().trim());// 建立第五列
ws.addCell(label1);// 放入工作簿
ws.addCell(label2);
ws.addCell(label3);
ws.addCell(label4);
ws.addCell(label5);
}
// 写入Exel工作表
wwb.write();
// 关闭Excel工作薄对象
wwb.close();
// 下载
FacesContext ctx = FacesContext.getCurrentInstance();
String contentType = "application/x-download";
HttpServletResponse response = (HttpServletResponse) ctx
.getExternalContext().getResponse();
response.setContentType(contentType);
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(fileName.getBytes("gb2312"), "ISO8859-1"));
HttpServletRequest request1 = (HttpServletRequest) FacesContext
.getCurrentInstance().getExternalContext().getRequest();
ServletOutputStream out = response.getOutputStream();
byte[] bytes = new byte[0xffff];
InputStream is = new FileInputStream(new File(realPath));
int b = 0;
while ((b = is.read(bytes, 0, 0xffff)) > 0) {
out.write(bytes, 0, b);
}
is.close();
out.flush();
ctx.responseComplete();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// 此处建立路径
return null;
}
Java学习之道:Java操作Excel之导出下载