范文健康探索娱乐情感热点
投稿投诉
热点动态
科技财经
情感日志
励志美文
娱乐时尚
游戏搞笑
探索旅游
历史星座
健康养生
美丽育儿
范文作文
教案论文
国学影视

Excel文件操作工具类

  直接上代码package com.sccin.spboot.utils;  import com.sccin.spboot.pojo.exception.RunMsgException; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory;  import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map;  /**  * Excel文件操作  *  * @author zx  */ public final class ExcelUtil {      /**    * 结束标记    */   public static final String END_OF_TABLE = "$end_of_table";    /**    * 日志    */   private static final Logger LOGGER = LoggerFactory.getLogger(ExcelUtil.class);    private ExcelUtil() {   }    /**    * 读取Excel文件中的数据    *    * @param excelFile excel文件    * @param startLine 起始行(基于0)    * @return map数据    */   public static Map getData(File excelFile, int startLine) {     try (Workbook workbook = WorkbookFactory.create(excelFile)) {       Sheet sheet = workbook.getSheetAt(0);       int endLine = sheet.getLastRowNum();       return getData(excelFile, startLine, endLine);     } catch (IOException | InvalidFormatException e) {       LOGGER.error(e.getMessage(), e);     }     return Collections.emptyMap();   }    /**    * 读取Excel文件中的数据    *    * @param excelFile excel文件    * @param startLine 起始行(基于0)    * @param endLine   结束行(基于0)    * @return map数据    */   public static Map getData(File excelFile, int startLine, int endLine) {     Map map = new HashMap<>();     try (Workbook workbook = WorkbookFactory.create(excelFile)) {       Sheet sheet = workbook.getSheetAt(0);       for (int i = startLine; i <= endLine; i++) {         Row row = sheet.getRow(i);         if (row != null) {           rowToMap(row, map);         }       }     } catch (IOException | InvalidFormatException e) {       LOGGER.error(e.getMessage(), e);     }     return map;   }    /**    * 读取Excel文件中表格的数据    *    * @param excelFile excel文件    * @param titleLine 表格标题所在的行(基于0)    * @return 数据列表    */   public static List> getTableData(File excelFile, int titleLine) {     List> list = new ArrayList<>();     try (Workbook workbook = WorkbookFactory.create(excelFile)) {       Sheet sheet = workbook.getSheetAt(0);       // 表格标题所在的行       Row row = sheet.getRow(titleLine);       if (row != null) {         // 获取标题对应的索引         Map indexMap = getIndexMap(row);         // 获取表格中的数据         for (int i = titleLine + 1; i <= sheet.getLastRowNum(); i++) {           row = sheet.getRow(i);           if (row != null) {             Map map = new HashMap<>();             rowToMap(row, map, indexMap);             // 跳过空行             if (map.isEmpty()) {               continue;             }             list.add(map);             // 内容结束             if (map.get(END_OF_TABLE) != null) {               break;             }           }         }       }     } catch (IOException | InvalidFormatException e) {       LOGGER.error(e.getMessage(), e);       throw new RunMsgException("读取数据错误", e);     }     return list;   }    /**    * 读取Excel文件中表格的数据    *    * @param inputStream excel文件    * @param titleLine   表格标题所在的行(基于0)    * @return 数据列表    */   public static List> getTableData(InputStream inputStream, int titleLine) {     List> list = new ArrayList<>();     try (Workbook workbook = WorkbookFactory.create(inputStream)) {       Sheet sheet = workbook.getSheetAt(0);       // 表格标题所在的行       Row row = sheet.getRow(titleLine);       if (row != null) {         // 获取标题对应的索引         Map indexMap = getIndexMap(row);         // 获取表格中的数据         for (int i = titleLine + 1; i <= sheet.getLastRowNum(); i++) {           row = sheet.getRow(i);           if (row != null) {             Map map = new HashMap<>();             rowToMap(row, map, indexMap);             // 跳过空行             if (map.isEmpty()) {               continue;             }             list.add(map);             // 内容结束             if (map.get(END_OF_TABLE) != null) {               break;             }           }         }       }     } catch (IOException | InvalidFormatException e) {       LOGGER.error(e.getMessage(), e);       throw new RunMsgException("读取数据错误", e);     }     return list;   }    /**    * 返回Excel文件数据字段名称的索引    *    * @param row 行    * @return map    */   private static Map getIndexMap(Row row) {     Map map = new HashMap<>();     int max = row.getLastCellNum();     for (int i = 0; i <= max; i++) {       Cell cell = row.getCell(i);       if (cell == null) {         continue;       }       Comment comment = cell.getCellComment();       if (comment == null) {         continue;       }       map.put(i, comment.getString().getString().trim());     }     return map;   }    /**    * 行数据转存入map中,key为单元格索引在indexMap中的值,value为单元格的值    *    * @param row      行    * @param map      map    * @param indexMap index map    */   private static void rowToMap(Row row, Map map, Map indexMap) {     int max = row.getLastCellNum();     for (int i = 0; i <= max; i++) {       String key = indexMap.get(i);       if (key == null) {         continue;       }       Cell cell = row.getCell(i);       if (cell == null) {         continue;       }       Comment comment = cell.getCellComment();       // 如果表格数据结束,返回结束所在的行数       if ((comment != null) && END_OF_TABLE.equals(comment.getString().getString())) {         map.clear();         map.put(END_OF_TABLE, row.getRowNum());         return;       }       cell.setCellType(CellType.STRING);       String value = cell.getStringCellValue();       if (!value.isEmpty()) {         map.put(key, value);       }     }   }    /**    * 行数据转存入map中,key为单元格的批注,value为单元格的值    *    * @param row 行    * @param map map    */   private static void rowToMap(Row row, Map map) {     int max = row.getLastCellNum();     for (int i = 0; i <= max; i++) {       Cell cell = row.getCell(i);       if (cell == null) {         continue;       }       Comment comment = cell.getCellComment();       if (comment == null) {         continue;       }       String key = comment.getString().getString().trim();       cell.setCellType(CellType.STRING);       String value = cell.getStringCellValue();       map.putIfAbsent(key, value);     }   }   /**    * 读取Excel文件中的数据    *    * @param excelFile excel文件    * @param startLine 起始行(基于0)    * @param endLine   结束行(基于0)    * @return map数据    */   public static Map getDataOfIntegerMap(File excelFile, int startLine, int endLine) {     Map map = new HashMap<>();     try (Workbook workbook = WorkbookFactory.create(excelFile)) {       Sheet sheet = workbook.getSheetAt(0);       for (int i = startLine; i <= endLine; i++) {         Row row = sheet.getRow(i);         if (row != null) {           rowToMap2(row, map);         }       }     } catch (IOException | InvalidFormatException e) {       LOGGER.error(e.getMessage(), e);       System.out.println(e);     }     return map;   }   /**    * 读取Excel文件中表格的数据    *    * @param excelFile excel文件    * @param titleLine 表格批注所在的行(基于0)    * @return 数据列表    */   public static List> getTableData1(File excelFile, int titleLine) {     List> list = new ArrayList<>();     try (Workbook workbook = WorkbookFactory.create(excelFile)) {       Sheet sheet = workbook.getSheetAt(0);       // 表格标题所在的行       Row row = sheet.getRow(titleLine);       if (row != null) {         // 获取标题对应的索引         Map indexMap = getIndexMap(row);         // 获取表格中的数据         for (int i = titleLine + 1; i <= sheet.getLastRowNum(); i++) {           row = sheet.getRow(i);           if (row != null) {             Map map = new HashMap<>();             rowToMap1(row, map, indexMap);             // 跳过空行             if (map.isEmpty()) {               continue;             }             list.add(map);             // 内容结束             if (map.get(END_OF_TABLE) != null) {               break;             }           }         }       }     } catch (IOException | InvalidFormatException e) {       LOGGER.error(e.getMessage(), e);       throw new RunMsgException(e.getMessage(),e);     }     return list;   }   /**    * 行数据转存入map中,key为单元格索引在indexMap中的值,value为单元格的值    *    * @param row      行    * @param map      map    * @param indexMap index map    */   private static void rowToMap1(Row row, Map map, Map indexMap) {     int max = row.getLastCellNum();     for (int i = 0; i <= max; i++) {       String key = indexMap.get(i);       if (key == null) {         continue;       }       Cell cell = row.getCell(i);       if (cell == null) {         continue;       }       Comment comment = cell.getCellComment();       // 如果表格数据结束,返回结束所在的行数       if ((comment != null) && END_OF_TABLE.equals(comment.getString().getString())) {         map.clear();         map.put(END_OF_TABLE, StringUtil.getString(row.getRowNum()));         return;       }       cell.setCellType(CellType.STRING);       String value = cell.getStringCellValue();       if (!value.isEmpty()) {         map.put(key, value.trim());       }     }   }   /**    * 行数据转存入map中,key为单元格的批注,value为单元格的值    *    * @param row 行    * @param map map    */   private static void rowToMapString(Row row, Map map) {     int max = row.getLastCellNum();     for (int i = 0; i <= max; i++) {       Cell cell = row.getCell(i);       if (cell == null) {         continue;       }       Comment comment = cell.getCellComment();       if (comment == null) {         continue;       }       String key = comment.getString().getString().trim();       cell.setCellType(CellType.STRING);       String value = cell.getStringCellValue();       map.putIfAbsent(key, value);     }   }   /**    * 行数据转存入map中,key为单元格的批注,value为单元格的值    *    * @param row 行    * @param map map    */   private static void rowToMap2(Row row, Map map) {     int max = row.getLastCellNum();     for (int i = 0; i <= max; i++) {       Cell cell = row.getCell(i);       if (cell == null) {         continue;       }       Comment comment = cell.getCellComment();       if (comment == null) {         continue;       }       String key = comment.getString().getString().trim();       cell.setCellType(CellType.STRING);       String value = cell.getStringCellValue();       Map sonMap = new HashMap<>();       map.putIfAbsent(String.valueOf(i),new String[]{key,value});     }   }   /**    * 行数据转存入map中,key为单元格的批注,value为单元格的值    *    * @param row 行    * @param list 数组    */   private static void rowToMapOfList(Row row, List> list) {     int max = row.getLastCellNum();     HashMap cellMap = new HashMap<>();     for (int i = 0; i <= max; i++) {       Cell cell = row.getCell(i);       if (cell == null) {         continue;       }       cell.setCellType(CellType.STRING);       String value = StringUtil.getString(cell.getStringCellValue());       cellMap.put(String.valueOf(i),value);     }     list.add(cellMap);   }       /**    *    * 根据流读取Excel数据    *    *    * @param inputStream    * @param startLine 开始行    * @param isExcel2003    * @return    * @see [类、类#方法、类#成员]    */   public static Map read(InputStream inputStream, int startLine,boolean isExcel2003) {     Workbook wb = getWorkbook(inputStream, isExcel2003);     return getData(wb,startLine);   }    /**    * 读取Excel文件中的数据    *    * @param startLine 起始行(基于0)    * @param endLine   结束行(基于0)    * @return map数据    */   public static Map readOfStringMap(Workbook wb, int startLine, int endLine) {     Map map = new HashMap<>();     Sheet sheet = wb.getSheetAt(0);     for (int i = startLine; i <= endLine; i++) {       Row row = sheet.getRow(i);       if (row != null) {         rowToMap2(row, map);       }     }     return map;   }     /**    *    * @param workbook Excel对象    * @param startLine    * @return    */   public static Map getData(Workbook workbook, int startLine) {     Sheet sheet = workbook.getSheetAt(0);     int endLine = sheet.getLastRowNum();     return getWorkbookData(workbook, startLine, endLine);   }    public static Map getWorkbookData(Workbook workbook, int startLine, int endLine) {     Map map = new HashMap<>();     Sheet sheet = workbook.getSheetAt(0);     for (int i = startLine; i <= endLine; i++) {       Row row = sheet.getRow(i);       if (row != null) {         rowToMapString(row, map);       }     }     return map;   }    /**    * 读取Excel文件中的数据    *    * @param excelFile excel文件    * @param startLine 起始行(基于0)    * @param endLine   结束行(基于0)    * @return map数据    */   public static List> getDataWithList(File excelFile, int startLine, int endLine) {     List> result = new ArrayList<>();     try (Workbook workbook = WorkbookFactory.create(excelFile)) {       Sheet sheet = workbook.getSheetAt(0);       for (int i = startLine; i <= endLine; i++) {         Row row = sheet.getRow(i);         if (row != null) {           rowToMapOfList(row, result);         }       }     } catch (IOException | InvalidFormatException e) {       LOGGER.error(e.getMessage(), e);     }     return result;   }   /**    * 读取Excel文件中表格的数据    *    * @param titleLine 表格标题所在的行(基于0)    * @return 数据列表    */   public static List> getTableDataByInputStream(Workbook wb, int titleLine ) { //    Workbook wb = getWorkbook(inputStream, isExcel2003);     List> list = new ArrayList<>();       Sheet sheet = wb.getSheetAt(0);       // 表格标题所在的行       Row row = sheet.getRow(titleLine);       if (row != null) {         // 获取标题对应的索引         Map indexMap = getIndexMap(row);         // 获取表格中的数据         for (int i = titleLine + 1; i <= sheet.getLastRowNum(); i++) {           row = sheet.getRow(i);           if (row != null) {             Map map = new HashMap<>();             rowToMap1(row, map, indexMap);             // 跳过空行             if (map.isEmpty()) {               continue;             }             list.add(map);             // 内容结束             if (map.get(END_OF_TABLE) != null) {               break;             }           }         }       }     return list;   }    public static Workbook getWorkbook(InputStream inputStream, boolean isExcel2003) {     /** 根据版本选择创建Workbook的方式 */     Workbook wb = null;     try {       if (isExcel2003)       {         wb = new HSSFWorkbook(inputStream);       }       else       {         wb = new XSSFWorkbook(inputStream);       }     }catch (IOException e){       throw new RunMsgException(e.getMessage(),e);     }     return wb;   }    /**    * 给excel文件设置表格行数据    *    * @param sheet     excel工作表    * @param titleRows 标题所在的行    * @param datas     数据    */   public static void setTableData(Sheet sheet, List titleRows, List> datas) {     if (datas.isEmpty()) {       return;     }     Map indexMap = new HashMap<>();     for (Integer titleRow : titleRows) {       Row row = sheet.getRow(titleRow);       Map map = getIndexMap(row);       indexMap.putAll(map);     }     Integer start = titleRows.stream().max(Comparator.naturalOrder()).orElse(null);     if (start == null) {       return;     }     start += 1;     sheet.shiftRows(start, sheet.getLastRowNum(), datas.size(), true, false);     CellStyle cellStyle = sheet.getWorkbook().createCellStyle();     cellStyle.setBorderBottom(BorderStyle.THIN);     cellStyle.setBorderLeft(BorderStyle.THIN);     cellStyle.setBorderRight(BorderStyle.THIN);     cellStyle.setBorderTop(BorderStyle.THIN);     cellStyle.setWrapText(true);     cellStyle.setAlignment(HorizontalAlignment.CENTER);     cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);     for (int i = 0; i < datas.size(); i++) {       int rowNum = start + i;       Row row = sheet.createRow(rowNum);       Map map = datas.get(i);       for (Map.Entry entry : indexMap.entrySet()) {         Object value = map.get(entry.getValue());         Cell cell = row.createCell(entry.getKey());         cell.setCellStyle(cellStyle);         if (value != null) {           cell.setCellValue(value.toString());         }       }     }   }  }

模玩资讯SUPER7ULTIMATES!2001太空漫游角色7吋可动人偶美系玩具厂牌SUPER7旗下可动人偶系列ULTIMATES!Figures,以最有影响力科幻电影作品之一的2001太空漫游主题,推出全新的四弹作品!分别是戴维鲍曼博士法兰克普尔博士模玩资讯新设计的蛇夫座!HG亚斯古利奥高达发布文章转自模玩地带万代PB限定HG亚斯古利奥高达,或者叫蛇夫座高达,今日发布。售价3600日元,2022年1月。机体登场于W外传新漫画GUNITRE,机体由阿久津润一进行了新设计,和模玩资讯合金弹头也出兵人了文章转自模魂志国内模玩品牌吞时工作室近日公布了旗下获得了SNK授权合作的产品合金弹头112布衣人形的初次公布就有马克塔马敌方士兵3款模型,预计2022年第二三季度发售。(PS这两年模玩资讯火焰冥王哈里哈拉羯谛14比例完成品由SUMART(末那末匠)与日本原型师毒岛孝牧合作的原创雕塑作品火焰冥王哈里哈拉羯谛14比例完成品即将于2021年11月发售,限量99体(日本国内流通版,日本国外预计2022年第3模玩资讯变形金刚第三方极致小比例!藏玩阁CT03B小鹏文章转自模魂志在早前就收到了藏玩阁推出的首个小比例变形成品CT03B小鹏,今天咱们就来补个档评测看看吧!在藏玩阁的大比例合体即将齐人的时候,突然剑走偏锋推出一个小比例版本的蚩尤冲云模玩资讯财团90年代老模再版万代HG1144OZ10VMSX亚斯古利奥高达再贩卖880日元,2022年1月再贩万代HG1144OZ19MASX古历培高达再贩卖1100日元,2022年1月再版万代HG1144OWiFi芯片供不应求,联发科或再调涨20WiFi芯片供不应求,市场传出联发科10月产品价格调涨20。联发科表示,不评论价格。射频芯片厂立积第3季营运大受WiFi芯片缺货冲击,单季营收滑落至新台币10。26亿元,季减40。台积电建新厂遭英特尔高价抢工,工期将延后2个月百能云芯晶圆代工龙头台积电昨日发公告,称美国新厂的子公司TSMCArizonaCorporation完成45亿美元无担保主顺位公司债定价,预计10月25日完成交割台积电千亿资金到位,但是,电池废料变肥料,废旧动力电池的出路找到了吗?目前我国动力电池主要以磷铁酸锂三元锂为主废旧动力电池中含有大量的锰钴镍重金属元素电解液中也含有六氟磷酸锂等高毒性物质处理不当会对环境造成巨大的破坏最近瑞士电池制造商Northvol特斯拉宣布将全球范围使用磷酸铁锂电池,比亚迪乐了今年电池原材料上涨磷酸铁锂因为不需要镍或者钴所以造价要更便宜对三元锂电池制造成本影响相对较高行业意识到一味追求高能量密度不一定正确带来的安全性风险也不可小觑而磷酸铁锂电池化学性质稳奔驰为控制成本,旗下EQA和EQB将使用磷酸铁锂电池10月27日戴姆勒集团表示公司旗下豪华汽车部门梅赛德斯奔驰将在入门级新能源汽车中使用价格更便宜且功率较低磷酸铁锂电池今年电池原材料上涨磷酸铁锂因为不需要镍或者钴所以造价要更便宜对三
游戏就应该是纯粹的快乐体验,纸片马力欧折纸国王评测纸片马力欧折纸国王可能是款比较慢热的游戏,在前四小时的游戏体验中它只会一如既往地展示一个马力欧遇到新伙伴的冒险一个不算特别有趣的战斗玩法,外加一个水管工再次拯救碧琪公主的老套戏码。2021春节必备Switch派对游戏盘点(一定有你没玩过)去年春节假期前夕,我们曾经整理过一期Switch派对游戏盘点,尽管由于疫情很多小伙伴没能如愿返乡,即使成功回家也都只能在家隔离无法与亲朋好友相聚,多少让上一期盘点显得有点尴尬。今年比亚迪在挪威市场交付1500辆新车日前,我们从外媒获悉,比亚迪已经通过当地经销商开始向挪威首批客户交付七座版唐(参数询价)EV车型。新车在挪威的起售价格为59。99万挪威克朗,约合人民币44。1万元,相比国内市场补债券市场收益率持续上涨,中国平安股价有望再次翻倍近期国债利率持续上行上图是10月15日的国债收益率表,收益率最高的是30年期国债,目前收益率为3。53,其次是10年期国债2。97。30年期国债收益率较前日增长了2。19BP,10企业有必要做微信商场么,微信商场开发需要哪些步骤?微信公众号在这几年发展的可是越来越好,也是受到了众多企业的青睐,纷纷在平台中搭建了属于自己的领域。通过这一操作,可以让企业带来直接的收益,也能企业在社交平台中,拥有自己的私域流量。抖音作者视频采集全部视频,批量采集批量下载在这个短视频爆火的时代,我们想要在这个自媒体时代获取一席之位,我们除了坚持之外,还需要各种方法和技巧,今天我就教大家做自媒体的第一步,素材视频的获取,我们首选需要选好想要做自媒体的短暂的养猪股反弹即将结束,猪周期反转从来不是个把月就能实现的近期养猪股明显反弹到了高位猪肉概念从低位反弹到现在的高位也持续了2个多月了,反弹的高度也高达20,整体的反弹还是相当高的,毕竟市场一直充斥着养猪逆转的消息,特别是昨天生猪期货涨停的惊喜福利到,呼伦贝尔宋ProDM限时特惠1。0万元,期待您的光临观望很久的宋ProDM终于降价了,比亚迪王朝呼伦贝尔益丰祥泰店即日起到11月03日,店铺活动购车直降5。89,真是迫不及待的想要到店一睹宋ProDM的风采啊促销时间2021年11月惊喜福利到,呼伦贝尔比亚迪F3最高优惠6。28,期待您的光临谈及理想生活,多少都会带有未来的憧憬,舒适科技感的生活,而现实中,你需要一台比亚迪F3,为你的现实生活增添舒适与便利,当前比亚迪王朝呼伦贝尔益丰祥泰店降价6。28,活动时间截止到1珍惜中概互联网股的低价筹码,未来将翻倍中概互联网ETF已经从底部上涨了13。41本轮中概互联网ETF调整到最低的价格是1。372,目前的价格是1。556,反弹幅度已经高达13。41。之前连续对中概互联网ETF进行了连续立案调查!全部带走!徽州宴老板几千万的资产就交了几千块钱税?徽州宴老板几千万的资产就交了几千块钱税?老板娘这次遛狗代价有点大啦。徽州宴事件7月2日,安徽蚌埠。因遛狗纠纷,徽州宴老板娘与小区居民发生争执冲突。事情的起因是徽州宴老板娘遛狗没拴绳