zoukankan      html  css  js  c++  java
  • POI导出数据以Excel的方式录入,下载

     简单描述:把数据导出到excel文件中。过程:获取要导出的数据列表(list),创建excel文件,数据放入。

    代码:

    //html代码
    <div class="btn-group">
    <button class="btn sbold green" id="export" onclick="">
        <span class="ladda-label">导出数据</span>
    </button>
    </div> 
    //js代码

    <script type="text/javascript">
    //后台查询到的list传过来的
    //通过modelmap的addAttribute()方法来传递
    //具体的省略
    var examList = [[examList]];

    $("#export").click("click",function () {
      //list转json
    var obj=JSON.stringify(examList);
    window.location.href = rootPath + "/vraxx/rightAxx/fileExport?rightsJson="+encodeURIComponent(obj);
    }) 
    //后台java代码
    @RequestMapping("fileExport")
    @ResponseBody
    public void exportFile(String rightsJson,HttpServletResponse response) throws IOException{
    try {
    List<VraxxTemporary> list=(List<VraxxTemporary>)JSONArray.toList(JSONArray.fromObject(rightsJson), VraxxTemporary.class);
    if(list.size()==0){
    response.sendRedirect("errornull.action");
    }else{
    // 在内存中创建一个Excel文件,通过输出流写到客户端提供下载
    // 内存中保留 10000 条数据,以免内存溢出,其余写入 硬盘
    SXSSFWorkbook workbook = new SXSSFWorkbook(10000);
    // 创建一个sheet页
    SXSSFSheet sheet = (SXSSFSheet) workbook.createSheet("XX模板");
    // 分别设置Excel列的宽度
    sheet.setColumnWidth(0, 150 * 40);
    sheet.setColumnWidth(1, 100 * 40);
    sheet.setColumnWidth(2, 100 * 40);
    sheet.setColumnWidth(3, 100 * 40);
    // 创建标题
    SXSSFRow headRow = (SXSSFRow) sheet.createRow(0);
    headRow.createCell(0).setCellValue("序号");
    headRow.createCell(1).setCellValue("编码");
    headRow.createCell(2).setCellValue("AA名称");
    headRow.createCell(3).setCellValue("AA编码");
    headRow.createCell(4).setCellValue("BB名称");
    headRow.createCell(5).setCellValue("BB编码");
    headRow.createCell(6).setCellValue("CC名称");
    headRow.createCell(7).setCellValue("CC编码");
    headRow.createCell(8).setCellValue("姓名");
    headRow.createCell(9).setCellValue("手机号");
    headRow.createCell(10).setCellValue("证件类型");
    headRow.createCell(11).setCellValue("证件号");

    for (VraxxTemporary temporary: list) {
    // 创建行
    SXSSFRow dataRow = (SXSSFRow) sheet.createRow(sheet.getLastRowNum() + 1);
    dataRow.createCell(0).setCellValue(temporary.getTemp_id());
    dataRow.createCell(1).setCellValue(temporary.getCont_number());
    dataRow.createCell(2).setCellValue(temporary.getAA_name());
    dataRow.createCell(3).setCellValue(temporary.getAA_code());
    dataRow.createCell(4).setCellValue(temporary.getBB_name());
    dataRow.createCell(5).setCellValue(temporary.getBB_code());
    dataRow.createCell(6).setCellValue(temporary.getCC_name());
    dataRow.createCell(7).setCellValue(temporary.getCC_code());
    dataRow.createCell(8).setCellValue(temporary.getUserName());
    dataRow.createCell(9).setCellValue(temporary.getUser_phone());
    dataRow.createCell(10).setCellValue(temporary.getIdtype());
    dataRow.createCell(11).setCellValue(temporary.getIdcode());
    }
    // 设置Excel文件名,并以中文进行编码
    Date day=new Date();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String name = "XX数据"+ df.format(day);
    String codedFileName = new String(name.getBytes("gbk"), "iso-8859-1");
    response.setHeader("Content-Disposition", "attachment;filename=" + codedFileName + ".xlsx");
    // 响应类型,编码
    response.setContentType("application/octet-stream;charset=UTF-8");
    // 形成输出流
    OutputStream osOut = response.getOutputStream();
    // 将指定的字节写入此输出流
    workbook.write(osOut);
    // 刷新此输出流并强制将所有缓冲的输出字节被写出
    osOut.flush();
    // 关闭流
    osOut.close();
    /*
    * dispose of temporary files backing this workbook on disk 处理在磁盘上备份此工作簿的临时文件
    * SXSSF分配临时文件,您必须始终清除显式,通过调用dispose方法
    */
    workbook.dispose();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    } 

     说明:encodeURIComponent()方法是对uri中的某一部分特殊字符进行编码,是浏览器能够接受和理解  (前台js传递的是json字符串)

    网上找excel导出,很多都不太好用,可能是自己太菜,没调试好,也可能本身就有问题,在这里,这个是我网上扒来的然后经过修改之后的,记录一下。

  • 相关阅读:
    10分钟教你Python+MySQL数据库操作
    10分钟教你用eclipse上传代码到GitHub
    分享几个有趣的程序代码(有些还是值得学习的哦)~
    10分钟教你生成超高逼格微信朋友圈
    1.maven中pom.xml文件中<exclusions>标签认不到问题
    2. springboot启动报错:Field userMapper in com.service.UserService required a bean of type 'com.dao.UserMapper' that could not be found.
    1. SpringBoot启动后,报异常:This application has no explicit mapping for /error, so you are seeing this as a fallback.
    9. 一个list拆分成多个list返回
    12. myeclipse遇到Initializing java tooling(1%)终极解决办法
    42. oracle通过两张表的一个字段对应,update其中一张表的某个字段
  • 原文地址:https://www.cnblogs.com/xuchao0506/p/10002024.html
Copyright © 2011-2022 走看看