zoukankan      html  css  js  c++  java
  • java中使用jxl导出Excel表格详细通用步骤

    该方法一般接收两个参数,response和要导出的表格内容的list.

    一般我们将数据库的数据查询出来在页面进行展示,根据用户需求,可能需要对页面数据进行导出.

    此时只要将展示之前查询所得的数据放入session中备份一份,在调用导出方法时,从session中获取即可,

    如果为后台直接导出,直接查询数据库后将结果传入即可,当然也可以在导出Excel方法中查询.

    查询方法: 

      // 获取查询结果存入session中
            Object resultList = request.getAttribute("listItems");//传入前台展示的查询结果集
            request.getSession().setAttribute("resultList", resultList);

    导出Excel方法:

       // 从session中获取查询结果集
            List<TTmoasCoNorecordInfo> resultList = (List<TTmoasCoNorecordInfo>) request.getSession().getAttribute("resultList");

            // 调用导出excel方法,传入response,和resultList
            xsdwglService.downloadExcel(response, resultList);

    downloadExcel如下:

        @Override
        public void downloadExcel(HttpServletResponse response, List<TTmoasCoNorecordInfo> list) {
    
            // 创建工作表
            WritableWorkbook book = null;
            response.reset();
    
            response.setCharacterEncoding("UTF-8");// 设置字符集
    
            // 创建工作流
            OutputStream os = null;
            try {
    
                // 设置弹出对话框
                response.setContentType("application/DOWLOAD");
                response.setCharacterEncoding("UTF-8");
    
                // 设置工作表的标题
                response.setHeader("Content-Disposition", "attachment; filename=Norecord_Social_Credit_Code.xls");// 设置生成的文件名字
                os = response.getOutputStream();
    
                // 初始化工作表
                book = Workbook.createWorkbook(os);
    
            } catch (IOException e1) {
    
                logger.error("导出excel出现IO异常", e1);
                throw new ServiceException("导出失败", e1);
            }
            try {
    
                // 以下为excel表格内容
                // int nCount = list.size();
                WritableSheet sheet = book.createSheet("社信代无关联及名称不一致保存结果", 0);
    
                // 生成名工作表,参数0表示这是第一页
                // int nI = 1;
    
                // 表字段名
                sheet.addCell(new jxl.write.Label(0, 0, "序号"));
                sheet.addCell(new jxl.write.Label(1, 0, "名称(查询)"));
                sheet.addCell(new jxl.write.Label(2, 0, "经营状态"));
                sheet.addCell(new jxl.write.Label(3, 0, "名称(所填企业名)"));
                sheet.addCell(new jxl.write.Label(4, 0, "统一社会信用代码"));
                sheet.addCell(new jxl.write.Label(5, 0, "申请日期"));
    
                // 将数据追加
                for (int i = 1; i < list.size() + 1; i++) {
    
                    sheet.addCell(new jxl.write.Label(0, i, String.valueOf(i)));// 序号从1开始
                    sheet.addCell(new jxl.write.Label(1, i, list.get(i - 1).getEntname()));
                    sheet.addCell(new jxl.write.Label(2, i, list.get(i - 1).getEntstatus()));
                    sheet.addCell(new jxl.write.Label(3, i, list.get(i - 1).getUserName()));
                    sheet.addCell(new jxl.write.Label(4, i, list.get(i - 1).getAppCertificateNum()));
    
                    // 设置日期格式
                    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
                    Date appDate = list.get(i - 1).getAppDate();
                    String appDateStr = sf.format(appDate);
                    sheet.addCell(new jxl.write.Label(5, i, appDateStr));// 申请日期
                }
                book.write();
                book.close();
            } catch (Exception e) {
                logger.error("导出excel出现异常", e);
                throw new ServiceException("导出失败", e);
            } finally {
                if (null != os) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        logger.error("关流出现异常", e);
                        e.printStackTrace();
                    }
                }
            }
        }

    这样就可以导出Excel表格了.

    需要设置多个Sheet/合并单元格等,根据相应API设置即可.

  • 相关阅读:
    log4j 配置文件详解
    Java 发送Get和Post请求
    java 基于百度地图API GPS经纬度解析地址
    Spring MVC 注解json 配置
    web.xml中classpath 解释
    【错误信息】springMVC No mapping found for HTTP request with URI
    栈和堆
    结构体和类的区别,联系
    Delegate,Block,Notification, KVC,KVO,Target-Action
    Protocol, Delegate
  • 原文地址:https://www.cnblogs.com/MyOceansWeb/p/6123966.html
Copyright © 2011-2022 走看看