注意

excel读取的行列数,都是从零开始的

需要引入的包

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.BooleanUtils;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

获取单元格的值

获取这个单元格的值

/*** 获取单元格的值** @param cell 需要判断的单元格* @return     单元格的值*/
public static String getCellValue(Cell cell) {if (cell != null) {//判断cell类型switch (cell.getCellType()) {case NUMERIC: {//数字return String.valueOf(cell.getNumericCellValue());}case FORMULA: {//公式//判断cell是否为日期格式if (DateUtil.isCellDateFormatted(cell)) {//转换为日期格式YYYY-mm-ddreturn String.valueOf(cell.getDateCellValue());} else {//数字return String.valueOf(cell.getNumericCellValue());}}case STRING: {//字符串型return cell.getRichStringCellValue().getString();}default:return  "";}} else {return  "";}
}

获取所有合并行的集合

 /*** 合并单元格处理,获取合并行** @param sheet sheet* @return List<CellRangeAddress>*/public static List<CellRangeAddress> getCombineCell(Sheet sheet) {List<CellRangeAddress> list = new ArrayList<>();//获得一个 sheet 中合并单元格的数量int sheetMergerCount = sheet.getNumMergedRegions();//遍历所有的合并单元格for (int i = 0; i < sheetMergerCount; i++) {//获得合并单元格保存进list中CellRangeAddress ca = sheet.getMergedRegion(i);list.add(ca);}return list;}

获取合并行所占的行数

获取到的是合并行在表格中最后所占的行数是第几行

这个获取到的便是8

/**
*  获取合并行所占的行数
* @param listCombineCell 存放合并单元格的list
* @param cell            需要判断的单元格
* @return                合并的行数
*/
public static int getRowNum(List<CellRangeAddress> listCombineCell, Cell cell) {int xr = 0, firstC, lastC, firstR, lastR;for (CellRangeAddress ca : listCombineCell) {//获得合并单元格的起始行, 结束行, 起始列, 结束列firstC = ca.getFirstColumn();lastC = ca.getLastColumn();firstR = ca.getFirstRow();lastR = ca.getLastRow();if (cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR) {if (cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC) {xr = lastR;}}}return xr;}

获取合并单元格的值(通过所有合并行集合、单元格、sheet判断)

这个只需要知道是哪一个单元格即可

/*** 判断单元格是否为合并单元格,是的话则将单元格的值返回** @param listCombineCell 存放合并单元格的list* @param cell            需要判断的单元格* @param sheet           sheet* @return                单元格的值*/
public static String isCombineCell(List<CellRangeAddress> listCombineCell, Cell cell, Sheet sheet) {int firstC, lastC, firstR, lastR;String cellValue = null;for (CellRangeAddress ca : listCombineCell) {//获得合并单元格的起始行, 结束行, 起始列, 结束列firstC = ca.getFirstColumn();lastC = ca.getLastColumn();firstR = ca.getFirstRow();lastR = ca.getLastRow();if (cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR) {if (cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC) {Row fRow = sheet.getRow(firstR);Cell fCell = fRow.getCell(firstC);cellValue = getCellValue(fCell);break;}} else {cellValue = "";}}return cellValue;
}

获取合并单元格的值(通过行数、列数、sheet判断)

这个需要清楚具体是哪一行哪一列,具体的数字

/*** 获取合并单元格的值, 如果不是合并单元格则输出为null** @param row    行下标* @param column 列下标* @return 合并单元格的值*/
public static String getMergedRegionValue(Sheet sheet, int row, int column) {int sheetMergeCount = sheet.getNumMergedRegions();for (int i = 0; i < sheetMergeCount; i++) {CellRangeAddress ca = sheet.getMergedRegion(i);int firstColumn = ca.getFirstColumn();int lastColumn = ca.getLastColumn();int firstRow = ca.getFirstRow();int lastRow = ca.getLastRow();if (row >= firstRow && row <= lastRow) {if (column >= firstColumn && column <= lastColumn) {Row fRow = sheet.getRow(firstRow);Cell fCell = fRow.getCell(firstColumn);return getCellValue(fCell);}}}return null;
}

判断指定的单元格是否是合并单元格

 /*** 判断指定的单元格是否是合并单元格** @param row    行下标* @param column 列下标* @return       单元格是否是合并单元格*/public static boolean isMergedRegion(Sheet sheet, int row, int column) {int sheetMergeCount = sheet.getNumMergedRegions();for (int i = 0; i < sheetMergeCount; i++) {CellRangeAddress range = sheet.getMergedRegion(i);int firstColumn = range.getFirstColumn();int lastColumn = range.getLastColumn();int firstRow = range.getFirstRow();int lastRow = range.getLastRow();if (row >= firstRow && row <= lastRow) {if (column >= firstColumn && column <= lastColumn) {return true;}}}return false;}

读取Excel文件(传入String类型的excel地址)

//读取excel
public static Workbook readExcel(String filePath) {if (filePath == null) {return null;}String extString;extString = filePath.substring(filePath.lastIndexOf("."));InputStream is;try {is = new FileInputStream(filePath);if (".xls".equals(extString)) {return new HSSFWorkbook(is);} else if (".xlsx".equals(extString)) {return new XSSFWorkbook(is);} else {return null;}} catch (IOException e) {e.printStackTrace();}return null;
}

读取Excel文件(传入MultipartFile类型数据)

//读取excel
public static Workbook readExcel(MultipartFile excelFile) throws IOException {if (excelFile == null) {return null;}InputStream excel = excelFile.getInputStream();String excelName = excelFile.getOriginalFilename();if (excelName == null){return null;}String excelSuffix = excelName.substring(excelName.lastIndexOf("."));try {if (".xls".equals(excelSuffix)) {return new HSSFWorkbook(excel);} else if (".xlsx".equals(excelSuffix)) {return new XSSFWorkbook(excel);} else {return null;}} catch (IOException e) {e.printStackTrace();}return null;
}

对Excel表格操作的常用工具类相关推荐

  1. Java Excel导出复杂excel表格样式之ExcelUtil工具类

    Java Excel导出包括普通导出及复杂表格样式,主要是对于需要进行行列合并的列进行特殊处理,计算清楚起始行,结束行,起始列,结束列. 普通导出可以是所有列,也可以是包含某些列,或者排除某些列: 1 ...

  2. JAVA常用工具类(实用高效)

    JAVA常用工具类(根据GITHUB代码统计) 从Google你能搜索到大量的关于Struts,Spring,Hibernate,iBatis等比较大的框架的资料,但是很少有人去关注一些小的工具包,但 ...

  3. commons-lang3-3.2.jar中的常用工具类的使用

    这个包中的很多工具类可以简化我们的操作,在这里简单的研究其中的几个工具类的使用. 1.StringUtils工具类 可以判断是否是空串,是否为null,默认值设置等操作: /*** StringUti ...

  4. java中集合类的转换_Java中的两个常用工具类及集合数组的相互转换

    为了编程人员的方便及处理数据的安全性,Java特别提供了两个非常有用的工具类: 一.Collections 1.Collections类的特点: 集合框架的工具类.里面定义的都是静态方法. 2.Col ...

  5. javascript 总结(常用工具类的封装)(转)

    转载地址:http://dzblog.cn/article/5a6f48afad4db304be1e7a5f javascript 总结(常用工具类的封装) JavaScript 1. type 类型 ...

  6. java excel 取值_java实现Excel 单元格取值工具类

    在工作中经常遇到通过excel获取数据的需求,比如通过excel将数据提交到数据库等.现针对excel单元格的取值方法提取出来作为一个工具类. 具体代码如下: import org.apache.po ...

  7. Java常用工具类StringUtils的常用方法

    Java常用工具类StringUtils的常用方法 1.该工具类是用于操作Java.lang.String类的. 2.StringUtils类在操作字符串是安全的,不会报空指针异常,也正因此,在操作字 ...

  8. Java 常用工具类 Collections 源码分析

    文章出处 文章出自:安卓进阶学习指南 作者:shixinzhang 完稿日期:2017.10.25 Collections 和 Arrays 是 JDK 为我们提供的常用工具类,方便我们操作集合和数组 ...

  9. 【Java 代码实例 13】Java操作pdf的工具类itext

    目录 一.什么是iText? 二.引入jar 1.项目要使用iText,必须引入jar包 2.输出中文,还要引入下面```itext-asian.jar```包 3.设置pdf文件密码,还要引入下面` ...

最新文章

  1. CTF(pwn) Fastbin Attack
  2. mysql8.0.17压缩包安装教程_超详细的MySQL8.0.17版本安装教程
  3. Cocos Creator导出场景和预制的问题
  4. mysql dba证书挂靠_这7种情况都叫“证书挂靠”!看看你是否在挂证的边缘试探?...
  5. MySQL 5.7 忘记密码
  6. spring 配置版本问题
  7. Java_Arrays.fill() 初始化二维数组一个指定值
  8. cimoc 最新版_Cimoc1.49版下载
  9. 程序员该怎么创业才比较靠谱?
  10. class6 图(左程云左神算法 初级笔记 2018)
  11. 浏览器标签中显示京东logo
  12. [腾讯校招] 微信红包
  13. Android运行项目时提示:No signature of method: build_*.android() is applicable for argument types
  14. 教你如何用计算机玩游戏,想要在电脑设备上玩手机游戏,教你如何使用tcgames软件进行操作...
  15. ZABBIX 监控基本报警故障
  16. “鲜花插在牛粪上” 好域名嫁错郎?
  17. 从零开始的计算机学习
  18. 用“网文快捕”破解不能复制文字的网站
  19. 转载随机数的两种操作方法
  20. 【原创】iOS开发入门教程

热门文章

  1. 采用邻接表存储有向图,设计算法判断任意两个顶点间是否存在路径。设计算法,将一个无向图的邻接矩阵转换为邻接表。
  2. date-fns日期格式化_使用date-fns轻松实现JS中的日期操作
  3. 买房上瘾!甲骨文CEO埃里森4800万美元再添一豪宅
  4. b站计算机考研大师兄数据结构代码打卡第2天
  5. 中国性文化史 读后感得2021-07-03
  6. 读《对中国文化的布罗代尔式考证》再感
  7. Oracle数据库中的序列、索引和同义词,详细笔记。
  8. 突击蓝桥杯嵌入式(四)——滴答定时器、按键的三行代码消抖、LCD与ADC
  9. 正确释放WORD对象(COM组件) COMException: 被调用的对象已与其客户端断开连接
  10. Firbase开启调试模式及常见问题记录