使用响应流来输出文件(适合导出文件使用,本文主要测试的是导出excel)
来源:互联网 发布:iptv与网络电视的区别 编辑:程序博客网 时间:2024/06/05 19:01
/////////这是个响应请求的方法@RequestMapping(method = RequestMethod.GET, value = "testResponseExportXLS")public void testResponseExportXLS(HttpServletRequest request,HttpServletResponse response) {try {exportXLSfromMaps("exportTest",response);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}
/////具体实现方法如下:
public void exportXLSfromMaps(String fileName, HttpServletResponse response)throws Exception {downloadFile(fileName + ".xls",response);java.io.OutputStream out = response.getOutputStream();WritableWorkbook workbook = null;WritableSheet sheet=null;workbook = Workbook.createWorkbook(out);sheet = workbook.createSheet("test Sheet", 0);// 添加label对象Label labelC = new Label(0, 0, "aaa");sheet.addCell(labelC);// 添加带有字型Formatting的对象WritableFont wf = new WritableFont(WritableFont.TIMES, 11, WritableFont.BOLD, true);WritableCellFormat wcfF = new WritableCellFormat(wf);jxl.write.Label labelCF = new jxl.write.Label(1, 0,"bbb", wcfF);sheet.addCell(labelCF);// 添加带有字体颜色Formatting的对象WritableFont wfc = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false,UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);WritableCellFormat wcfFC = new WritableCellFormat(wfc);wcfFC.setAlignment(jxl.format.Alignment.CENTRE); Label labelCFC = new Label(2, 0,"ccc", wcfFC);sheet.addCell(labelCFC);sheet.mergeCells(0,0,0,1);sheet.mergeCells(2,0,3,0);// 添加带有字体颜色Formatting的对象WritableFont wf1 = new WritableFont(WritableFont.TIMES, 11, WritableFont.BOLD, true);WritableCellFormat wcfFC1 = new WritableCellFormat(wf1);Label labelCFC1 = new Label(3, 0,"ddd", wcfFC1);sheet.addCell(labelCFC1);// 2.添加Number对象jxl.write.Number labelN = new jxl.write.Number(0, 1, 12345);sheet.addCell(labelN);Label labelCFC2 = new Label(1, 1,"eee");sheet.addCell(labelCFC2);Label labelCFC3 = new Label(2, 1,"fff");sheet.addCell(labelCFC3);Label labelCFC4 = new Label(3, 1,"ggg");sheet.addCell(labelCFC4);workbook.write(); workbook.close();out.flush();out.close();}
public void initAttachmentHeader(HttpServletResponse response){response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");response.setHeader("Content-Type", "application/force-download");response.setHeader("Content-Type", "application/octet-stream");response.setHeader("Content-Type", "application/download");response.setHeader("Cache-Control", "private, max-age=0, must-revalidate");response.setHeader("Pragma", "public");}public void downloadFile(String fileName,HttpServletResponse response){downloadFile(fileName, Const.nullInt,response);}public void downloadFile(String fileName, long length,HttpServletResponse response){initAttachmentHeader(response);response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");if(length != (long)Const.nullInt) response.setHeader("Content-Length", String.valueOf(length));}
- 使用响应流来输出文件(适合导出文件使用,本文主要测试的是导出excel)
- 使用poi来导入导出excel文件
- delphi 使用把数据集导出为excel文件 (适合安装Excel的机器)
- ThinkPHP的使用(四)Excel文件导出
- ThinkPHP使用PHPexcel导出excel文件(已封装的输出类)
- 使用NPOI导出Excel文件
- 使用NPOI导出Excel文件
- 使用POI导出excel文件
- 使用Qt导出Excel文件。
- 2、 excel 导出。这种输入方式,适合后台输出,不需要打开EXCEL文件,不需要选择输出路径,只是后台自己默默的记录EXCEL文件。
- 使用POI导入和导出 Excel文件
- 使用POI导入和导出Excel文件
- 使用POI导入和导出Excel文件
- 使用JXL导出和解析EXCEL文件
- 使用POI导入和导出Excel文件
- jxls 使用模板文件导出生成excel
- MVC 使用FileResult导出excel文件实例
- 简易使用POI导入导出excel文件
- Programming Best Practices Tidbits
- JDK1.5新特性
- android102--MVC原理 和在安卓中的应用
- HDU 1011 Starship Troopers - 01树形dp 有坑啊!!
- SQL中的循环、for循环、游标
- 使用响应流来输出文件(适合导出文件使用,本文主要测试的是导出excel)
- spring MVC入门
- 【COCOS2DX-LUA 脚本开发之七】解决( error in function ‘addChild’. argument #2 is ‘xx’; ‘CCNode’ expected. )异常报错
- 【MS编程之美资格赛】Problem A及Problem B
- A+B
- VirtualBox报“uuid already exists”错误
- app store 申请
- MyBatis源码赏析3-配置
- Webservice使用