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
原创粉丝点击