標籤:ppa 評價 auto exists tty 資料 back and esc
頁面放置一個button進行點擊匯出事件
<h:commandLink target="_parent" value="匯出"
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之匯出下載