• poi导出excel


    1,用SXSSFWorkbook会快很多,HSSFWorkbook比较慢,2000条数据要跑130s。

    2,maven导入jar包:

    <dependency>
    			<groupId>org.apache.poi</groupId>
    			<artifactId>poi</artifactId>
    			<version>3.10.1</version>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.poi</groupId>
    			<artifactId>poi-ooxml</artifactId>
    			<version>3.10.1</version>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.poi</groupId>
    			<artifactId>poi-ooxml-schemas</artifactId>
    			<version>3.10.1</version>
    		</dependency>
    

      3,代码如下:

    // 创建SXSSFWorkbook对象
                SXSSFWorkbook wb = new SXSSFWorkbook();
                // 创建HSSFWorkbook对象
                // 建立新的sheet对象(excel的表单)
                Sheet sheet = wb.createSheet("报表");
                // 在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
                Row row1 = sheet.createRow(0);
                // 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
    //            HSSFCell cell = row1.createCell(0);
                row1.createCell(0).setCellValue("报表");
    
                // 1.生成字体对象
                Font font = wb.createFont();
                font.setFontHeightInPoints((short) 12);
                font.setFontName("新宋体");
                font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 宽度
                // 2.生成样式对象,这里的设置居中样式和版本有关,我用的poi用HSSFCellStyle.ALIGN_CENTER会报错,所以用下面的
                CellStyle style = wb.createCellStyle();
                style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 设置居中样式
                style.setFont(font); // 调用字体样式对象
                style.setWrapText(true);
                // 3.单元格应用样式
                row1.getCell(0).setCellStyle(style);
                // 设置单元格内容
                // 合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
                sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));
                // 在sheet里创建第二行 创建单元格并设置单元格内容及样式
                Row row2 = sheet.createRow(1);
                HashMap<Integer, String> headMap = new HashMap<Integer, String>();
                headMap.put(0, "交易时间");
                headMap.put(1, "交易类型");
                headMap.put(2, "收入");
                headMap.put(3, "支出");
                headMap.put(4, "备注");
                headMap.put(5, "会员名");
                for(int i =0;i < headMap.size();i++){
                    row2.createCell(i).setCellValue(headMap.get(i));
                }
                List<PersonTransactionDetailVO> listPersonDetail = personService.findPersonDetailAll(vo);
                long startTime = System.currentTimeMillis(); // 获取开始时间
                for (int s = 0; s < listPersonDetail.size(); s++) {
                    PersonTransactionDetailVO voCell = listPersonDetail.get(s);
                    HashMap<Integer, String> propertiesMap = new HashMap<Integer, String>();
                    propertiesMap.put(0, DateUtils.getDateToString(voCell.getUpdateDate()));
                    Integer typeInteger = voCell.getType();
                    String type = null;
                    switch (typeInteger) {
                        case 1:
                            type = "充值";
                            break;
                        case 2:
                            type = "提现";
                            break;
                        case 4:
                            type = "投标";
                            break;
                        case 5:
                            type = "收回本金";
                            break;
                        case 6:
                            type = "收回利息";
                            break;
                        case 7:
                            type = "提现手续费";
                            break;
                    }
                    propertiesMap.put(1, type);
                    if(typeInteger == 1 || typeInteger == 5 || typeInteger == 6){
                        propertiesMap.put(2, voCell.getMoney().toString());
                    }else{
                        propertiesMap.put(2, "");
                    }
                    if(typeInteger == 2 || typeInteger == 4 || typeInteger == 7){
                        propertiesMap.put(3, voCell.getMoney().toString());
                    }else{
                        propertiesMap.put(3, "");
                    }
                    if(null == voCell.getRemark()){
                        propertiesMap.put(4, "");
                    }else{
                        propertiesMap.put(4, voCell.getRemark());
                    }
                    propertiesMap.put(5, voCell.getLname());
                    // 从sheet第三行开始填充数据
                    Row rowx = sheet.createRow(s + 2);
                    for(int k =0; k < headMap.size();k++){
                        rowx.createCell(k).setCellValue(propertiesMap.get(k));
                        rowx.getCell(k).setCellStyle(style);
    
                    }
                }
                long endTime = System.currentTimeMillis(); // 获取结束时间
    
                System.out.println("程序运行时间: " + (endTime - startTime) + "ms");
                // 最后做统计,获取前台传过来的数据
                // 在sheet里创建最后一行
                Row rowCount = sheet.createRow(2 + listPersonDetail.size());
                // 创建单元格并设置单元格内容及样式
                rowCount.createCell(0).setCellValue("收入合计");
                rowCount.createCell(1).setCellValue(allIncome);
                rowCount.createCell(2).setCellValue("支出合计");
                rowCount.createCell(3).setCellValue(allOutcome);
                // 单元格宽度自适应
                for(int j = 0;j < headMap.size();j++){
                    sheet.autoSizeColumn((j));
                }
                Date dt = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmm");
                response.setContentType("octets/stream");
                String excelName = "交易明细表" + sdf.format(dt);
                // 转码防止乱码
                response.addHeader("Content-Disposition", "attachment;filename=" + new String(excelName.getBytes("gb2312"),
                        "ISO8859-1") + ".xls");
                OutputStream out = response.getOutputStream();
                wb.write(out);
                out.close();
    

      这样导出2万多条,只要300ms左右。

    4,另外,jxl和POI类似,也是java操作excel的类库,操作使用和poi一样。

  • 相关阅读:
    leetcode[145]Binary Tree Postorder Traversal
    leetcode[146]LRU Cache
    leetcode[147]Insertion Sort List
    leetcode[148]Sort List
    Intro to WebGL with Three.js
    Demo: Camera and Video Control with HTML5
    js ar
    Jingwei Huang
    Tinghui Zhou
    MODS: Fast and Robust Method for Two-View Matching
  • 原文地址:https://www.cnblogs.com/luoa/p/10511925.html
Copyright © 2020-2023  润新知