java導出excel 瀏覽器直接下載或者或以文件形式導出
看代碼吧~
/** * excel表格直接下載 */ public static void exportExcelByDownload(HSSFWorkbook wb,HttpServletResponse httpServletResponse,String fileName) throws Exception { //響應類型為application/octet- stream情況下使用瞭這個頭信息的話,那就意味著不想直接顯示內容 httpServletResponse.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); //attachment為以附件方式下載 httpServletResponse.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode( fileName + ".xls", "utf-8")); /** * 代碼裡面使用Content-Disposition來確保瀏覽器彈出下載對話框的時候。 * response.addHeader("Content-Disposition","attachment");一定要確保沒有做過關於禁止瀏覽器緩存的操作 */ httpServletResponse.setHeader("Cache-Control", "No-cache"); httpServletResponse.flushBuffer(); wb.write(httpServletResponse.getOutputStream()); wb.close(); } /** * excel以文件的形式導出 * @throws Exception */ public static void exportExcelByFile(HSSFWorkbook wb,String fileName,String path) throws Exception{ ByteArrayOutputStream stream = new ByteArrayOutputStream(); wb.write(stream); FileOutputStream outputStream = new FileOutputStream(path + fileName); outputStream.write(stream.toByteArray()); stream.close(); outputStream.close(); }
java查詢數據導出excel並返回給瀏覽器下載
效果圖:
1.點擊導出表按鈕
2.接著就會出現下圖
3.點擊上圖中的確定按鈕再接著就會出現下圖
4.點擊上圖中的保存按鈕接著就會出現下圖,瀏覽器下載完成後的提示
5.打開下載好的文件如下圖
好瞭,廢話不多少,上代碼
jsp前端代碼
<div style="height:30px;"> <a>時間:</a> <input id="startDateConsume" type="text" class="easyui-datebox"> <a>-</a> <input id="endDateConsume" type="text" class="easyui-datebox"> <a>消費類型:</a> <select id="consumesType" name=""> <option value="0" selected="selected">所有</option> <option value="1">報名費</option> <option value="2">酒水零食類</option> </select> <a>支付狀態:</a> <select id="conPaymentStatus" name=""> <option value="0" selected="selected">所有</option> <option value="1">未支付</option> <option value="2">已支付</option> </select> <a id="btnConsumesSearch" class="easyui-linkbutton" data-options="iconCls:'icon-search'" style="margin-left:10px">查詢</a><a>(查詢出來的數據可統計)</a> <a id="consumesOutExcel" class="easyui-linkbutton" style="" data-options="iconCls:'icon-redo'">導出表</a> </div>
js前端代碼
$(function() { //導出excel表 $('#consumesOutExcel').on('click',function(){ exportExcel(); }); }); function exportExcel() { $.messager.confirm('確認', '確認把該搜索結果導出Excel表格 ?', function(r) { if (r) { var startTime = $('#startDateConsume').val(); var endTime = $('#endDateConsume').val(); var consumesType = $('#consumesType').val(); var conPaymentStatus = $('#conPaymentStatus').val(); $.messager.progress({ title : '處理中', msg : '請稍後', }); $.messager.progress('close'); location.href="web/vip/exportExcel.xlsx?startTime=" rel="external nofollow" +startTime+"&endTime="+endTime+"&consumesType="+consumesType+"&conPaymentStatus="+conPaymentStatus; } }); }
java後端代碼
@Controller @RequestMapping("/vip") public class VipController { //文件下載:導出excel表 @RequestMapping(value = "/exportExcel.xlsx",method = RequestMethod.GET) @ResponseBody public void exportExcel(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{ //一、從後臺拿數據 if (null == request || null == response) { return; } List<VipConsumes> list = null; String startTime = request.getParameter("startTime"); String endTime = request.getParameter("endTime"); int consumesType = Integer.parseInt(request.getParameter("consumesType")); int conPaymentStatus =Integer.parseInt(request.getParameter("conPaymentStatus")); VipConsumesExample example = new VipConsumesExample(); if(consumesType!=0 && conPaymentStatus!=0){ example.createCriteria().andTimeBetween(startTime, endTime).andConsumeTypeEqualTo(consumesType).andStatusEqualTo(conPaymentStatus); }else if(consumesType ==0 && conPaymentStatus!=0) { example.createCriteria().andTimeBetween(startTime, endTime).andStatusEqualTo(conPaymentStatus); }else if(consumesType!=0 && conPaymentStatus==0){ example.createCriteria().andTimeBetween(startTime, endTime).andConsumeTypeEqualTo(consumesType); }else { example.createCriteria().andTimeBetween(startTime, endTime); } list = this.vipConsumesDao.selectByExample(example); //二、 數據轉成excel request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("application/x-download"); String fileName = "消費記錄.xlsx"; fileName = URLEncoder.encode(fileName, "UTF-8"); response.addHeader("Content-Disposition", "attachment;filename=" + fileName); // 第一步:定義一個新的工作簿 XSSFWorkbook wb = new XSSFWorkbook(); // 第二步:創建一個Sheet頁 XSSFSheet sheet = wb.createSheet("startTimeendTime"); sheet.setDefaultRowHeight((short) (2 * 256));//設置行高 sheet.setColumnWidth(0, 4000);//設置列寬 sheet.setColumnWidth(1,5500); sheet.setColumnWidth(2,5500); sheet.setColumnWidth(3,5500); sheet.setColumnWidth(11,3000); sheet.setColumnWidth(12,3000); sheet.setColumnWidth(13,3000); XSSFFont font = wb.createFont(); font.setFontName("宋體"); font.setFontHeightInPoints((short) 16); XSSFRow row = sheet.createRow(0); XSSFCell cell = row.createCell(0); cell.setCellValue("流水號 "); cell = row.createCell(1); cell.setCellValue("微信名 "); cell = row.createCell(2); cell.setCellValue("微信訂單號"); cell = row.createCell(3); cell.setCellValue("消費時間"); cell = row.createCell(4); cell.setCellValue("消費類型"); cell = row.createCell(5); cell.setCellValue("剩餘積分 "); cell = row.createCell(6); cell.setCellValue("新增積分 "); cell = row.createCell(7); cell.setCellValue("扣除積分 "); cell = row.createCell(8); cell.setCellValue("消費金額"); cell = row.createCell(9); cell.setCellValue("支付方式"); cell = row.createCell(10); cell.setCellValue("支付狀態 "); cell = row.createCell(11); cell.setCellValue("錢包原始金額"); cell = row.createCell(12); cell.setCellValue("錢包扣除金額"); cell = row.createCell(13); cell.setCellValue("錢包剩餘金額"); XSSFRow rows; XSSFCell cells; for (int i = 0; i < list.size(); i++) { // 第三步:在這個sheet頁裡創建一行 rows = sheet.createRow(i+1); // 第四步:在該行創建一個單元格 cells = rows.createCell(0); // 第五步:在該單元格裡設置值 cells.setCellValue(list.get(i).getConsumeId()); cells = rows.createCell(1); cells.setCellValue(list.get(i).getName()); cells = rows.createCell(2); cells.setCellValue(list.get(i).getOrderNumber()); cells = rows.createCell(3); cells.setCellValue(list.get(i).getTime()); cells = rows.createCell(4); if (list.get(i).getConsumeType() == 2) { cells.setCellValue("酒水零食費"); } else { cells.setCellValue("報名費"); } cells = rows.createCell(5); cells.setCellValue(list.get(i).getIntegral()); cells = rows.createCell(6); cells.setCellValue(list.get(i).getIntegralIn()); cells = rows.createCell(7); cells.setCellValue(list.get(i).getIntegralOut()); cells = rows.createCell(8); cells.setCellValue(list.get(i).getMoney()); cells = rows.createCell(9); if (list.get(i).getPayment() == 2) { cells.setCellValue("積分抵現"); } else if (list.get(i).getPayment() == 3) { cells.setCellValue("微信支付"); } else if (list.get(i).getPayment() == 4) { cells.setCellValue("現金"); } else if (list.get(i).getPayment() == 1) { cells.setCellValue("錢包"); } cells = rows.createCell(10); if (list.get(i).getStatus() == 2) { cells.setCellValue("已支付"); } else if (list.get(i).getStatus() == 1) { cells.setCellValue("未支付"); } cells = rows.createCell(11); cells.setCellValue(list.get(i).getWalletOriginal()); cells = rows.createCell(12); cells.setCellValue(list.get(i).getWalletOut()); cells = rows.createCell(13); cells.setCellValue(list.get(i).getWalletSurplus()); } try { OutputStream out = response.getOutputStream(); wb.write(out); out.close(); wb.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。
推薦閱讀:
- 教你如何使用JAVA POI
- Java導出Excel統計報表合並單元格的方法詳解
- C#通過NPOI導入導出數據EXCEL
- C#使用NPOI實現Excel導入導出功能
- Java使用POI導出Excel(二):多個sheet