国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java中使用apache POI導(dǎo)出excel案例(提供下載)

2019-11-06 09:06:06
字體:
供稿:網(wǎng)友

前端代碼

<script type="text/javascript">

window.location = '${servePath}/pc/wuye/doExportExcel?type=0';

</script> 

特別注意一下這不能使用Ajax進行傳參數(shù)和跳轉(zhuǎn)頁面,因為ajax不提供下載。

后臺代碼

                      // 在內(nèi)存中創(chuàng)建一個Excel文件,通過輸出流寫到客戶端提供下載                     HSSFWorkbook workbook = new HSSFWorkbook();                     // 創(chuàng)建一個sheet頁                     HSSFSheet sheet = workbook.createSheet("物業(yè)投訴");                                          // 創(chuàng)建標題行                     HSSFRow headRow = sheet.createRow(0);                     headRow.createCell(0).setCellValue("投訴類型");                     headRow.createCell(1).setCellValue("投訴內(nèi)容");                     headRow.createCell(2).setCellValue("創(chuàng)建時間");                     headRow.createCell(3).setCellValue("創(chuàng)建人");                     headRow.createCell(4).setCellValue("更新時間");                     headRow.createCell(5).setCellValue("更新人");                     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");                     for (logistics_complaintsAO complaintsA : orderResult) {                         HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);                         dataRow.createCell(0).setCellValue(complaintsA.getSecondary_classification());                         dataRow.createCell(1).setCellValue(complaintsA.getContent());                         dataRow.createCell(2).setCellValue(df.format(complaintsA.getCreate_time()));                         dataRow.createCell(3).setCellValue(complaintsA.getCreate_by());                         dataRow.createCell(4).setCellValue(df.format(complaintsA.getUpdate_time()));                         dataRow.createCell(4).setCellValue(complaintsA.getUpdate_by());                     }                     String filename = "物業(yè)投訴.xls";                     String agent = request.getHeader("User-Agent");                     filename = FileUtils.encodeDownloadFilename(filename, agent);                     //一個流兩個頭                     ServletOutputStream out = response.getOutputStream();                     String contentType = response.getContentType();                     response.setContentType(contentType);                     response.setHeader("content-disposition", "attchment;filename="+filename);                     workbook.write(out);

其中使用到一個工具類

public class FileUtils {        /**         * 下載文件時,針對不同瀏覽器,進行附件名的編碼         *          * @param filename         *            下載文件名         * @param agent         *            客戶端瀏覽器         * @return 編碼后的下載附件名         * @throws IOException         */        public static String encodeDownloadFilename(String filename, String agent)                throws IOException {            if (agent.contains("Firefox")) { // 火狐瀏覽器                filename = "=?UTF-8?B?"                        + new BASE64Encoder().encode(filename.getBytes("utf-8"))                        + "?=";                filename = filename.replaceAll("/r/n", "");            } else { // IE及其他瀏覽器                filename = URLEncoder.encode(filename, "utf-8");                filename = filename.replace("+"," ");            }            return filename;        }}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 新建县| 天水市| 鄂伦春自治旗| 铜川市| 绿春县| 万载县| 新平| 车致| 松江区| 方山县| 铁岭县| 三江| 长治市| 台东市| 新源县| 合阳县| 松原市| 安乡县| 乌拉特前旗| 赤城县| 嘉善县| 东兰县| 林口县| 宣城市| 尼玛县| 宝山区| 南康市| 繁峙县| 邹城市| 禹州市| 扶余县| 聂荣县| 抚顺县| 广安市| 大石桥市| 胶南市| 四川省| 阳山县| 桂东县| 赣榆县| 衡东县|