Java处理数据导出功能
来源:互联网 发布:windows下脚本编程实例 编辑:程序博客网 时间:2024/06/11 03:56
一:思路
前提:首先要获取要导出的数据列表(一般都是通过请求查询条件从数据库中获取数据);然后调用封装好的生成Excel文件方法,在业务层中渲染文件中的数据列表。
二:步骤
1.导入使用的 jxl.jar ;
在页面定义“导出”按钮,发送导出功能请求;
在控制层中,获取要导出的数据集,传入文件名,执行生成Excel文件方法;
@RequestMapping("/exportChkinm")@ResponseBodypublic boolean exportChkinm(HttpServletResponse response, HttpServletRequest request, HttpSession session, Chkinm chkinm, String checkOrNot, Page page, Date madedEnd) throws Exception{ //获取数据 List<Chkinm> chkinmList = chkinmService.findAllChkinm(checkOrNot, chkinm, page, madedEnd, session.getAttribute("locale").toString()); //文件名 String fileName = "已审核入库单汇总"; //执行方法 setExpArg(response, request, fileName); //返回文件 return chkinmService.exportRUChkinm(response.getOutputStream(), chkinmList);}//生成Excel文件方法public void setExpArg(HttpServletResponse response,HttpServletRequest request,String fileName) throws UnsupportedEncodingException { response.setContentType("application/msexcel; charset=UTF-8"); if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) { //IE fileName = URLEncoder.encode(fileName, "UTF-8"); }else{ fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1"); } response.setHeader("Content-disposition", "attachment; filename="+ fileName + ".xls");}
2.渲染文件数据
在业务层中
public boolean exportRUChkinm(ServletOutputStream outputStream, List<Chkinm> chkinmList) { //获取工作薄 WritableWorkbook workBook = null; //设置工作簿样式 WritableFont titleFont = new WritableFont(WritableFont.TIMES, 16, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat titleStyle = new WritableCellFormat(titleFont); try { titleStyle.setAlignment(Alignment.CENTRE); workBook = Workbook.createWorkbook(outputStream); WritableSheet sheet = workBook.createSheet("已审核入库单汇总导出表", 0); sheet.addCell(new Label(0, 0, "已审核入库单汇总导出表", titleStyle)); sheet.mergeCells(0, 0, 11, 0); sheet.addCell(new Label(0, 1, "入库单号")); sheet.addCell(new Label(1, 1, "凭证号")); sheet.addCell(new Label(2, 1, "单据类型")); sheet.addCell(new Label(3, 1, "制单日期")); sheet.addCell(new Label(4, 1, "制单时间")); sheet.addCell(new Label(5, 1, "入库仓位")); sheet.addCell(new Label(6, 1, "供应商")); sheet.addCell(new Label(7, 1, "总金额")); sheet.addCell(new Label(8, 1, "税前总金额")); sheet.addCell(new Label(9, 1, "制单人")); sheet.addCell(new Label(10, 1, "审核人")); //遍历list填充表格内容 int index = 1; for(int j=0; j<chkinmList.size(); j++){ if(j == chkinmList.size()){ break; } index += 1; sheet.addCell(new Label(0, index, chkinmList.get(j).getChkinno().toString())); sheet.addCell(new Label(1, index, chkinmList.get(j).getVouno())); sheet.addCell(new Label(2, index, chkinmList.get(j).getTyp())); sheet.addCell(new Label(3, index, new SimpleDateFormat("yyyy-MM-dd").format(chkinmList.get(j).getMaded()))); sheet.addCell(new Label(4, index, chkinmList.get(j).getMadet())); sheet.addCell(new Label(5, index, String.valueOf(chkinmList.get(j).getPositn().getDes()))); sheet.addCell(new Label(6, index, String.valueOf(chkinmList.get(j).getDeliver().getDes()))); sheet.addCell(new Label(7, index, Float.toString(chkinmList.get(j).getTotalamt()))); sheet.addCell(new Label(8, index, Double.toString(chkinmList.get(j).getNoTaxTotalamt()))); sheet.addCell(new Label(9, index, chkinmList.get(j).getMadeby())); sheet.addCell(new Label(10, index,chkinmList.get(j).getChecby())); } workBook.write(); outputStream.flush(); } catch (WriteException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ try { if(workBook!=null){ workBook.close(); } outputStream.close(); } catch (WriteException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return true; }
1 0
- Java处理数据导出功能
- JAVA 数据导出Word文档模板的功能实现
- java导出功能
- java Execl导出功能
- java Excel导出功能
- Java中导出数据到Excel处理方法
- 数据报表导出Excel功能
- ASP.NET导出数据功能
- 在MS CRM中使用Excel导入/导出的功能对数据进行批量处理
- Java的Excel导出功能
- Java 实现导出Excel功能
- POI(JAVA Excel导出功能)
- java实现导出excel功能
- java的表格导出功能
- java 数据 导出excel
- Java数据导出Excel
- java excel导出数据
- Java导出Excel数据
- mybatis 中 foreach collection的三种用法
- [乐意黎转载]AngularJS快速入门指南06:过滤器
- 【Geeker】中间人攻击(Man-in-the-MiddleAttack)
- 01.JUC 集合
- 怎么编辑pdf文件的背景颜色
- Java处理数据导出功能
- 2017.2.12【初中部 GDKOI】模拟赛B组 T2:宿敌
- tomcat启动报错解决
- spring事务的几种传播特性和事务的隔离级别
- mysql之sql语法基本使用
- 聚簇索引(Clustered Index)和非聚簇索引 (Non- Clustered Index)
- Matlab中模糊控制器的使用
- 小白学习之((Lua 05))
- Go项目的目录结构