在做excel导出时如何将excel直接写在输出流
1个回答
2018-09-17 · 百度认证:云南新华电脑职业培训学校官方账号
云南新华电脑学校
云南新华电脑学校是经云南省教育厅批准成立的省(部)级重点计算机专业学校,采用三元化管理模式,教学设备先进,师资雄厚学生毕业即就业,学院引进了电商企业入驻,创建心为电商创业园区,实现在校即创业
向TA提问
关注
展开全部
之前做excel导出时,我都是先将文件写在服务器上,然后再下载下来,后来发现原来可以直接将文件写在输出流里边。
下面是一个小demo:
package com.huaqin.fcstrp.util; import java.io.IOException;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.net.URLEncoder; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet; public class WriteExcle { private HSSFWorkbook workbook = null; /** * * @param response 下载请求的response */ public void createExcel(HttpServletResponse response){ //创建workbook workbook = new HSSFWorkbook(); //添加Worksheet(不添加sheet时生成的xls文件打开时会报错) Sheet sheet1 = workbook.createSheet("sheet1"); OutputStream out = null; try { out = response.getOutputStream(); String fileName = "test.xls";// 文件名 response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8")); Row row = workbook.getSheet("sheet1").createRow(0); //创建第一行 for(int i = 0;i < 10;i++){ Cell cell = row.createCell(i); cell.setCellValue("测试数据"+i); } workbook.write(out); } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }}
调用:
/** * 下载2 * @param request * @param response * @return * @throws FileNotFoundException */ @RequestMapping(value = "/download2") @ResponseBody public void download2(HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException { WriteExcel writeExcle = new WriteExcel(); writeExcel.createExcel(response); }
下面是一个小demo:
package com.huaqin.fcstrp.util; import java.io.IOException;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.net.URLEncoder; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet; public class WriteExcle { private HSSFWorkbook workbook = null; /** * * @param response 下载请求的response */ public void createExcel(HttpServletResponse response){ //创建workbook workbook = new HSSFWorkbook(); //添加Worksheet(不添加sheet时生成的xls文件打开时会报错) Sheet sheet1 = workbook.createSheet("sheet1"); OutputStream out = null; try { out = response.getOutputStream(); String fileName = "test.xls";// 文件名 response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8")); Row row = workbook.getSheet("sheet1").createRow(0); //创建第一行 for(int i = 0;i < 10;i++){ Cell cell = row.createCell(i); cell.setCellValue("测试数据"+i); } workbook.write(out); } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }}
调用:
/** * 下载2 * @param request * @param response * @return * @throws FileNotFoundException */ @RequestMapping(value = "/download2") @ResponseBody public void download2(HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException { WriteExcel writeExcle = new WriteExcel(); writeExcel.createExcel(response); }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询