jxl 导出excel

来源:互联网 发布:淘宝女装退货率太高了 编辑:程序博客网 时间:2024/06/02 06:22

本文记录下用jxl.jar进行数据导出至excel的操作,在这博主特别提示一点,请求后台导出数据时使用form表单提交,不要用ajax!不要用ajax!不要用ajax!否则各种莫名其妙的error自己去收拾吧~

废话不多说了,小二,上代码!

java

@RequestMapping(value = "queryPostOrderForExcel", produces = "text/html; charset=UTF-8")@ResponseBodypublic void queryPostOrderForExcel(String startDate, String endDate, HttpServletRequest request, HttpServletResponse response)    {        // 查询数据        List<ExportPostOrderBean> list = orderService.queryPostOrderForExcel(startDate, endDate);        // 将数据导出        Date now = new Date();        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");        String curDate = dateFormat.format(now);        try        {            ByteArrayOutputStream OutExcel = new ByteArrayOutputStream();            WritableWorkbook book = Workbook.createWorkbook(OutExcel);            WritableSheet sheet = book.createSheet("信息表", 0);            sheet.setRowView(0, 600);            Label lab = null;            lab = new Label(3, 0, "姓名");            sheet.addCell(lab);            lab = new Label(4, 0, "号码");            sheet.addCell(lab);            lab = new Label(5, 0, "地址");            sheet.addCell(lab);            for (int i = 0; i < list.size(); i++)            {                //查询出来需要导出的数据源                ExportPostOrderBean postOrder = list.get(i);                String[] strList = new String[5];                strList[0] = postOrder.getUserName();                strList[1] = postOrder.getTelPhone();                strList[2] = address;                for (int j = 0; j < strList.length; j++)                {                    lab = new Label(j, (i + 1), strList[j]); // Label(col,row,str);                    sheet.addCell(lab);                }            }            book.write();            book.close();            String ExcelFileName = "信息表" + curDate + ".xls";            response.setCharacterEncoding("UTF-8");            response.setContentType("application/x-msdownload");            response.setContentLength(OutExcel.size());            response.addHeader("Content-Disposition", "attachment; filename=" + ExcelFileName);            ServletOutputStream out = response.getOutputStream();            OutExcel.writeTo(out);            OutExcel.flush();        }        catch (Exception e)        {            e.printStackTrace();            logger.error("导出信息表异常。");        }    }}

实现效果如图:

1、确定需要导出数据的条件
这里写图片描述

2、点击确认进行数据查询并导出操作

这里写图片描述

这里写图片描述

0 0
原创粉丝点击