jxl 导出excel

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

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

java

@RequestMapping(value = "queryPostOrderForExcel", produces = "text/html; charset=UTF-8")
@ResponseBody

public 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值