zoukankan      html  css  js  c++  java
  • 添加POI导出excel通用工具类

            /**
         * 文件写入excel
         * @param file 文件
         * @param list 数据源
         * @param sheetname 工作簿
         * @throws IOException
         */
        @SuppressWarnings("resource")
        public void exportExcel(File file,List<LinkedHashMap<String,Object>> list,String sheetname) throws IOException{
            
            Workbook workbook = new XSSFWorkbook(FileUtils.openInputStream(file));
            Sheet sheet = workbook.getSheet(sheetname);
            
            Row row = sheet.getRow(1);
            if (row == null) {
                row = sheet.createRow(1);
            }
            LinkedHashMap<String, Object> m =list.get(0);
                    String [] title = new String[m.size()];
                    int v = 0;
            for(String key : m.keySet()){
                title[v] = key;
                v++;
            }
            fos = new FileOutputStream(file);
            for (int i = 0; i < list.size(); i++) {
                row = sheet.createRow(i + 1);
                LinkedHashMap<String, Object> map = list.get(i);
                for (int j = 0; j < title.length; j++) {
                    row.createCell((short) j).setCellValue(map.get(title[j]) + "");
                }
            }
            workbook.write(fos);
            fos.flush();
            fos.close();
        }

  • 相关阅读:
    hashlib 库
    包--json 与 pickle 模块
    模块
    叠加多个装饰器,列表生成式,字典生成式,匿名函数
    函数的递归调用和二分法
    Redis之哨兵模式
    Redis之集群
    Redis之主从复制
    Django之redis-session
    Python操作redis
  • 原文地址:https://www.cnblogs.com/ymj2018/p/11907266.html
Copyright © 2011-2022 走看看