Java學習之道:Java操作Excel之匯出下載,javaexcel
頁面放置一個按鈕進行點擊匯出事件
<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怎將匯出的excel下載到用戶端
package com.mr; import java.io.IOException;import java.io.PrintWriter; import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * 利用Servlet匯出Excel * @author CHUNBIN * */public class ExportExcelServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8");//設定request的編碼方式,防止中文亂碼 String fileName ="匯出資料";//設定匯出的檔案名稱 StringBuffer sb = new StringBuffer(request.getParameter("tableInfo"));//將表格資訊放入記憶體 String contentType = "application/vnd.ms-excel";//定義匯出檔案的格式的字串 String recommendedName = new String(fileName.getBytes(),"iso_8859_1");//設定檔案名稱的編碼格式 response.setContentType(contentType);//設定匯出檔案格式 response.setHeader("Content-Disposition", "attachment; filename=" + recommendedName + "\"");// response.resetBuffer(); //利用輸出輸......餘下全文>>
java excel匯出到使用者本地
一般來說做下載功能的確是先導到伺服器的一個臨時目錄上的,然後再用一段代碼把這個excel讀出來,並且輸出到response流裡面去,給你一段可以用的代碼
//------------------------------ //step1. 儲存一個臨時excel到temp目錄下 //------------------------------ //這部分自己實現,我相信你已經實現了 //假設你已經實現了儲存一個excel到一個臨時檔案夾裡面去 //並且已經產生了一個File 指向這個臨時的 excel,名叫exportFile //-------------------------------//step2. 彈出下載對話方塊//-------------------------------if(exportFile == null){logger.error("產生excel錯誤! exportFile 為空白");return;}//先建立一個檔案讀取流去讀取這個臨時excel檔案FileInputStream fs = null;try {fs = new FileInputStream(exportFile);} catch (FileNotFoundException e) {logger.error("產生excel錯誤! " + exportFile + " 不存在!",e);return;}// 設定回應標頭和儲存檔案名稱HttpServletResponse response = ServletActionContext.getResponse();//這個一定要設定,告訴瀏覽器這次請求是一個下載的資料流response.setContentType("APPLICATION/OCTET-STREAM");try {//這邊的 "採購部門本月採購報表.xls" 替換成你自己要顯示給使用者的檔案名稱excelName = URLEncoder.encode("採購部門本月採購報表.xls", "UTF-8");} catch (UnsupportedEncodingException e1) {logger.error("轉換excel名稱編碼錯誤!",e1);}response.setHeader("Content-Disposition", "attachment; filename=\"" + excelName + "\"");// 寫出流資訊int b = 0;try { ......餘下全文>>