• java实现导出excel的时候,每页规定数据量,分页,按数据命名页名


    最近任务是完成要求:把需要导出的数据导出到excel中,要求每页按传入的参数规定数据量,便于后期更改。同时分页要实现页名是当前页数据的开始到结束。
    话不多说,实现代码:
    首先是参数的传入

    @Value("${records:40}")
    private int records; //导出时每页数据最大数量
    然后下面是生成excel文件

    private void DeviceExportXlsx(String exportType, List<JSONObject> exportData,List<String>titleList) {
    /**
    * 生成excel文件
    */
    int record = 1; //当前的数据行
    int pages = (exportData.size()%records==0?exportData.size()/records-1:exportData.size()/records); //总页数-1
    String fileName="INMS_COREEQUIPMENT_LIST_"+format.format(new Date())+"." + exportType; //".xlsx"
    try {
    //创建工作簿
    XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
    //创建工作表
    ArrayList<XSSFSheet> sheets = new ArrayList<>();
    for(int page= 0;page<=pages;page++){
    XSSFSheet sheet = xssfWorkbook.createSheet();
    //设置表名
    if (page!=pages){
    xssfWorkbook.setSheetName(page,record+"-"+(record+records-1));
    }else {
    xssfWorkbook.setSheetName(page,record+"-"+(record+exportData.size()%records-1));
    }
    //创建表头
    XSSFRow head = sheet.createRow(0);
    for(int i = 0;i < titleList.size();i++){
    XSSFCell cell = head.createCell(i);
    cell.setCellValue(titleList.get(i));
    }
    for (int i = 1;i <= records;i++,record++) {

    if(record==exportData.size()+1){
    break;
    }
    JSONObject json=exportData.get(record - 1);
    //创建行,从第二行开始,所以for循环的i从1开始取
    XSSFRow row = sheet.createRow(i);
    //创建单元格,并填充数据
    XSSFCell cell=null;
    for(int j = 0;j < titleList.size();j++){
    cell = row.createCell(j);
    cell.setCellValue(json.containsKey(titleList.get(j))?json.getString(titleList.get(j)):"");
    }
    }
    sheets.add(sheet);
    System.out.println(sheets);
    }

    //创建临时文件的目录
    File file = new File(homePath);
    if(!file.exists()){
    file.mkdirs();
    }
    //临时文件路径/文件名
    String path = homePath +File.separator+ fileName;
    OutputStream outputStream = new FileOutputStream(path);
    xssfWorkbook.write(outputStream);
    outputStream.flush();
    outputStream.close();
    export(path,fileName);
    }catch (Exception e){
    e.printStackTrace();;
    }
    }
     
    附:导出的代码

    public void export (String path,String fileName) throws Exception{
    FileInputStream fileIn = new FileInputStream(path);
    response.setContentType("application/force-download");
    response.addHeader("Content-disposition", "attachment;fileName="+fileName);
    OutputStream outPut = response.getOutputStream();
    byte[] bytes = new byte[fileIn.available()];
    fileIn.read(bytes);
    outPut.write(bytes);
    fileIn.close();
    }

    ————————————————
    版权声明:本文为CSDN博主「如我一般的人」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/z710757293/article/details/107537696

  • 相关阅读:
    (转载)Linux定时任务cron配置
    (转载)PHP源代码分析- tick(s)
    (转载)PCNTL函数族--PHP多进程编程
    org.apache.hadoop.conf-Configured
    org.apache.hadoop.conf-Configuration
    org.apache.hadoop.conf-Configurable
    源码-hadoop1.1.0-core-org.apache.hadoop.classification
    源码-hadoop1.1.0-core-org.apache.hadoop
    作为开始
    测试一下
  • 原文地址:https://www.cnblogs.com/javalinux/p/15696979.html
Copyright © 2020-2023  润新知