首页 > 编程 > Java > 正文

java中使用apache POI导出excel案例(提供下载)

2019-11-06 09:06:06
字体:
来源:转载
供稿:网友

前端代码

<script type="text/javascript">

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

</script> 

特别注意一下这不能使用Ajax进行传参数和跳转页面,因为ajax不提供下载。

后台代码

                      // 在内存中创建一个Excel文件,通过输出流写到客户端提供下载                     HSSFWorkbook workbook = new HSSFWorkbook();                     // 创建一个sheet页                     HSSFSheet sheet = workbook.createSheet("物业投诉");                                          // 创建标题行                     HSSFRow headRow = sheet.createRow(0);                     headRow.createCell(0).setCellValue("投诉类型");                     headRow.createCell(1).setCellValue("投诉内容");                     headRow.createCell(2).setCellValue("创建时间");                     headRow.createCell(3).setCellValue("创建人");                     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 = "物业投诉.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;        }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表