POI版本3.15

pom文件

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

Excel文件

Java代码实现:

/*** */
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;/**** Description: Excel操作* * CreateTime: 2017年12月11日  下午3:08:09** Change History:**        Date             CR Number              Name              Description of change**/
public class ExcelUtil {private static final String EXCEL_XLS = "xls";  private static final String EXCEL_XLSX = "xlsx";  /** * 判断Excel的版本,获取Workbook * @param in * @param filename * @return * @throws IOException */  public static Workbook getWorkbok(InputStream in,File file) throws IOException{  Workbook wb = null;  if(file.getName().endsWith(EXCEL_XLS)){  //Excel 2003  wb = new HSSFWorkbook(in);  }else if(file.getName().endsWith(EXCEL_XLSX)){  // Excel 2007/2010  wb = new XSSFWorkbook(in);  }  return wb;  }  /** * 判断文件是否是excel * @throws Exception  */  public static void checkExcelVaild(File file) throws Exception{  if(!file.exists()){  throw new Exception("文件不存在");  }  if(!(file.isFile() && (file.getName().endsWith(EXCEL_XLS) || file.getName().endsWith(EXCEL_XLSX)))){  throw new Exception("文件不是Excel");  }  }  /** * 读取Excel测试,兼容 Excel 2003/2007/2010 * @throws Exception  */  public static void main(String[] args) throws Exception {  SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");  try {  // 同时支持Excel 2003、2007  File excelFile = new File("d:/product.xlsx"); // 创建文件对象  FileInputStream in = new FileInputStream(excelFile); // 文件流  checkExcelVaild(excelFile);  Workbook workbook = getWorkbok(in,excelFile);  //Workbook workbook = WorkbookFactory.create(is); // 这种方式 Excel2003/2007/2010都是可以处理的  int sheetCount = workbook.getNumberOfSheets(); // Sheet的数量  /** * 设置当前excel中sheet的下标:0开始 */
//            Sheet sheet = workbook.getSheetAt(0);   // 遍历第一个Sheet  Sheet sheet = workbook.getSheetAt(2);   // 遍历第三个Sheet  //获取总行数
//          System.out.println(sheet.getLastRowNum());// 为跳过第一行目录设置count  int count = 0;for (Row row : sheet) {try {// 跳过第一和第二行的目录  if(count < 2 ) {count++;  continue;  }//如果当前行没有数据,跳出循环  if(row.getCell(0).toString().equals("")){  return;}//获取总列数(空格的不计算)int columnTotalNum = row.getPhysicalNumberOfCells();System.out.println("总列数:" + columnTotalNum);System.out.println("最大列数:" + row.getLastCellNum());//for循环的,不扫描空格的列
//                    for (Cell cell : row) {
//                      System.out.println(cell);
//                    }int end = row.getLastCellNum();for (int i = 0; i < end; i++) {Cell cell = row.getCell(i);if(cell == null) {System.out.print("null" + "\t");continue;}Object obj = getValue(cell);System.out.print(obj + "\t");}} catch (Exception e) {e.printStackTrace();}}  } catch (Exception e) {  e.printStackTrace();  }}private static Object getValue(Cell cell) {Object obj = null;switch (cell.getCellTypeEnum()) {case BOOLEAN:obj = cell.getBooleanCellValue(); break;case ERROR:obj = cell.getErrorCellValue(); break;case NUMERIC:obj = cell.getNumericCellValue(); break;case STRING:obj = cell.getStringCellValue(); break;default:break;}return obj;}
}

若是导入文件太大出现内存溢出,参考https://blog.csdn.net/wwd0501/article/details/91626652实现

Java POI Excel读取相关推荐

  1. java excel读取操作,Java 操作 Excel (读取Excel2003 2007,Poi兑现)

    Java 操作 Excel (读取Excel2003 2007,Poi实现) 一. Apache POI 简介( http://poi.apache.org/) 使用Java程序读写Microsoft ...

  2. java读取word文档的复杂表格_poi读取word表格 java POI 如何读取word的表格中的表格...

    poi 操作word 2007 (如何删除word中的某一个表格)小编忘了哪年哪月的哪日小编在哪面墙上刻下张脸张微笑着忧伤着凝望小编的脸. public static void changeTable ...

  3. Java POI Excel导入导出

    Java POI Excel导入导出 1.maven引入依赖 2.导入Excel 3.导出Excel 1.maven引入依赖 <!-- POI Excel 操作 --> <depen ...

  4. Java POI Excel移动行和复制行的处理

    目录 Java POI Excel移动行和复制行的处理 坑点: 实现的代码 Java POI Excel移动行和复制行的处理 POI操作Excel时,不支持移动行的操作,因此在需要通过复制行+删除行+ ...

  5. java poi excel 导入数据库_java POI 处理excel表格数据并导入数据库示例

    java操作Excel最常用的开源组件有poi与jxl.jxl是韩国人开发的,发行较早,但是更新的很慢,目前似乎还不支持excel2007. poi是apache下的一个子项目,poi应该是处理ms的 ...

  6. Java POI SXSSFWorkbook 读取模板,输出

    Java POI 用 SXSSFWorkbook 读取模板,输出 公司的项目,用户投诉,说只要点击模板下载功能,就会导致整个服务全部停掉. 之前这个功能由于数据量并没有那么多,所以一直都没有发生过这种 ...

  7. Java POI——Excel导入导出的列英文字母与数字的互转方法记录

    因项目需要,写了递归实现POI--excel英文字母列转列编号,记录. 规则1:excel英文字母列转数字编号:A→0,B→1,Z→25,AA→26,ZZ→701,AAA→702 - public i ...

  8. java excel poi 包_用java poi包读取Excel单元格

    content = new hashmap(); string str = ""; try { fs = new poifsfilesystem(is); wb = new hss ...

  9. java poi方式读取Excel的图片

    POM ​​​​ <!-- poi --> <dependency><groupId>cn.afterturn</groupId><artifac ...

最新文章

  1. MySQL导数据工具对比
  2. 《柳叶刀》:群体免疫不靠谱!欧洲迄今最大新冠血清学调查显示,西班牙抗体阳性率仅5%...
  3. android siri 源码,Android的SIRI 。
  4. 将excel的数据导入到mysql数据表
  5. 倒腾了一天的笔记-centos 部署jar包
  6. 补充小知识:文件句柄与文件标识符
  7. 公路多孔箱涵设计_涵洞设计,设计师可参考~
  8. 如何快速打好java基础_学习Java课程时如何才能打好基础呢?
  9. Server object instance creation failed on all SOC machines
  10. ArcGIS水文分析实战教程(7)细说流域提取
  11. iOS中分段控制器与UIScrollView结合使用
  12. postgresql 客户端登录配置
  13. linux拷贝文件命令
  14. turtle库——绘制八边形、八角图形以及叠边形图形
  15. Tensorflow XLA
  16. css-reset样式重置
  17. 软件测试的艺术 读书笔记完整版
  18. c++重写卷积网络的前向计算过程,完美复现theano的测试结果
  19. 从SAP的核心优势看SAP四大战略成功的可能
  20. 点晴oa办公系统 18.0119B

热门文章

  1. 卸载WPS后,安装OFFICE2013问题
  2. 可视化工具Navicat的使用/pymysql模块的使用
  3. java 体检套餐_体检套餐管理系统的综合版
  4. SONM月报_2018年8月
  5. HTML自动暂停按钮,css播放暂停按钮实现_html/css_WEB-ITnose
  6. 【IdeaVR2019快捷操作(一)】虚拟仿真软件介绍
  7. jsp中文件上床的enctype=multipart/form-data用法
  8. sourcetree clone很大的项目
  9. Windows10的系统文件windows.edb文件过大的两种解决办法
  10. linux进程查看的一些命令