直接上代码

pom文件需要导入这些依赖

org.apache.poi
poi-ooxml
3.9

org.apache.poi
poi-ooxml-schemas
3.9

org.apache.poi
poi
3.9

org.apache.poi
poi
3.10-FINAL
jar
compile

org.apache.poi
poi-ooxml
3.10-FINAL
jar

org.apache.poi
poi-ooxml-schemas
3.10-FINAL
jar
compile

org.apache.poi
poi-scratchpad
3.10-FINAL
jar
compile

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
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.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;public class ReadExcel {//读取excel表标头数据public static List<String> readTitleExcel(String path) {String fileType = path.substring(path.lastIndexOf(".") + 1);// return a list contains many listList<List<String>> lists = new ArrayList<List<String>>();//读取excel文件InputStream is = null;try {is = new FileInputStream(path);//获取工作薄Workbook wb = null;if (fileType.equals("xls")) {wb = new HSSFWorkbook(is);} else if (fileType.equals("xlsx")) {wb = new XSSFWorkbook(is);} else {return null;}//读取第一个工作页sheetSheet sheet = wb.getSheetAt(0);//第一行为标题for (Row row : sheet) {ArrayList<String> list = new ArrayList<String>();for (Cell cell : row) {//根据不同类型转化成字符串cell.setCellType(Cell.CELL_TYPE_STRING);list.add(cell.getStringCellValue());}lists.add(list);}} catch (IOException e) {e.printStackTrace();} finally {try {if (is != null) {is.close();}} catch (IOException e) {e.printStackTrace();}}List<String> list = lists.get(0);return list;}//报错的时候走这个接口public static String readXlsxCount(String path) {List<List<String>> list = new ArrayList<>();String fileType = path.substring(path.lastIndexOf(".") + 1);// return a list contains many list//读取excel文件InputStream is = null;try {is = new FileInputStream(path);//获取工作薄Workbook wb = null;if (fileType.equals("xls")) {wb = new HSSFWorkbook(is);} else if (fileType.equals("xlsx")) {wb = new XSSFWorkbook(is);} else {return null;}//读取第一个工作页sheetSheet sheet = wb.getSheetAt(0);//获取总行数int rowTotalCount = sheet.getLastRowNum();//获取标题的总行数int columnCount = sheet.getRow(0).getPhysicalNumberOfCells();String count = rowTotalCount+"-"+columnCount ;System.out.println("行数为:"+rowTotalCount+"列数为:"+columnCount);return count;} catch (IOException e) {e.printStackTrace();} finally {try {if (is != null){ is.close();}} catch (IOException e) {e.printStackTrace();}}return null;}//xls和xlsx都可以使用(除标头,不包括第一行)public static List<List<String>> readXlsxExcel(String path) {List<List<String>> list = new ArrayList<>();String fileType = path.substring(path.lastIndexOf(".") + 1);// return a list contains many list//读取excel文件InputStream is = null;try {is = new FileInputStream(path);//获取工作薄Workbook wb = null;if (fileType.equals("xls")) {wb = new HSSFWorkbook(is);} else if (fileType.equals("xlsx")) {wb = new XSSFWorkbook(is);} else {return null;}//读取第一个工作页sheetSheet sheet = wb.getSheetAt(0);//获取总行数int rowTotalCount = sheet.getLastRowNum();//获取标题的总行数int columnCount = sheet.getRow(0).getPhysicalNumberOfCells();System.out.println("行数为:"+rowTotalCount+"列数为:"+columnCount);if (fileType.equals("xlsx")) {for (int i = 1; i <= rowTotalCount; i++) {ArrayList<String> listRow = new ArrayList<String>();// 获取第i列的row对象XSSFRow row = (XSSFRow) sheet.getRow(i);for (int j = 0; j < columnCount; j++) {//下列步骤为判断cell读取到的数据是否为null 如果不做处理 程序会报错String cell = null;//如果未null则加上""组装成非null的字符串if (row.getCell(j) == null) {listRow.add("");//如果读取到cell不为null 则直接加入 listRow集合} else {cell = row.getCell(j).toString();listRow.add(cell);}// 在第i列 依次获取第i列的第j个位置上的值 %15s表示前后间隔15个字节输出}list.add(listRow);}}else {for (int i = 1; i <= rowTotalCount; i++) {ArrayList<String> listRow = new ArrayList<String>();// 获取第i列的row对象HSSFRow row = (HSSFRow) sheet.getRow(i);for (int j = 0; j < columnCount; j++) {//下列步骤为判断cell读取到的数据是否为null 如果不做处理 程序会报错String cell = null;//如果未null则加上""组装成非null的字符串if (row.getCell(j) == null) {listRow.add("");//如果读取到cell不为null 则直接加入    listRow集合} else {cell = row.getCell(j).toString();listRow.add(cell);}// 在第i列 依次获取第i列的第j个位置上的值 %15s表示前后间隔15个字节输出}list.add(listRow);}}return list;} catch (IOException e) {e.printStackTrace();} finally {try {if (is != null) {is.close();}} catch (IOException e) {e.printStackTrace();}}return list;}//只能适用于xlspublic static List<List<String>> ReadExcel(String filePath){List<List> list = new ArrayList<>();try {// 建需要读取的excel文件写入streamHSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filePath));// 指向sheet下标为0的sheet 即第一个sheet 也可以按在sheet的名称来寻找HSSFSheet sheet = workbook.getSheetAt(0);// 获取sheet1中的总行数int rowTotalCount = sheet.getLastRowNum();//获取总列数int columnCount = sheet.getRow(0).getPhysicalNumberOfCells();System.out.println("行数为:"+rowTotalCount+"列数为:"+columnCount);for (int i = 0; i <= rowTotalCount; i++) {// 获取第i列的row对象HSSFRow row = sheet.getRow(i);ArrayList<String> listRow = new ArrayList<String>();for (int j = 0; j < columnCount; j++) {//下列步骤为判断cell读取到的数据是否为null 如果不做处理 程序会报错String cell = null;//如果未null则加上""组装成非null的字符串if(row.getCell(j) == null){cell = row.getCell(j)+"";listRow.add(cell);//如果读取到cell不为null 则直接加入   listRow集合}else{cell = row.getCell(j).toString();listRow.add(cell);}// 在第i列 依次获取第i列的第j个位置上的值 %15s表示前后间隔15个字节输出}list.add(listRow);}List<List<String>> listList = new ArrayList<>();for (int i=1;i<list.size();i++){listList.add(list.get(i));}return listList;} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return null;}//读取管线表public static List<List<String>> readLineExcel(String path) {List<List<String>> list = new ArrayList<>();String fileType = path.substring(path.lastIndexOf(".") + 1);// return a list contains many list//读取excel文件InputStream is = null;try {is = new FileInputStream(path);//获取工作薄Workbook wb = null;if (fileType.equals("xls")) {wb = new HSSFWorkbook(is);} else if (fileType.equals("xlsx")) {wb = new XSSFWorkbook(is);} else {return null;}//读取第一个工作页sheetSheet sheet = wb.getSheetAt(0);//获取总行数int rowTotalCount = sheet.getLastRowNum();//获取标题的总行数int columnCount = sheet.getRow(0).getPhysicalNumberOfCells();System.out.println("行数为:"+rowTotalCount+"列数为:"+columnCount);if (columnCount != 20){return null;}if (fileType.equals("xlsx")) {for (int i = 3; i <= rowTotalCount; i++) {ArrayList<String> listRow = new ArrayList<String>();// 获取第i列的row对象XSSFRow row = (XSSFRow) sheet.getRow(i);for (int j = 0; j < columnCount; j++) {//下列步骤为判断cell读取到的数据是否为null 如果不做处理 程序会报错String cell = null;//如果未null则加上""组装成非null的字符串if (row.getCell(j) == null) {cell = row.getCell(j) + "";listRow.add("");//如果读取到cell不为null 则直接加入    listRow集合} else {cell = row.getCell(j).toString();listRow.add(cell);}// 在第i列 依次获取第i列的第j个位置上的值 %15s表示前后间隔15个字节输出}list.add(listRow);}}else {for (int i = 3; i <= rowTotalCount; i++) {ArrayList<String> listRow = new ArrayList<String>();// 获取第i列的row对象HSSFRow row = (HSSFRow) sheet.getRow(i);for (int j = 0; j < columnCount; j++) {//下列步骤为判断cell读取到的数据是否为null 如果不做处理 程序会报错String cell = null;//如果未null则加上""组装成非null的字符串if (row.getCell(j) == null) {cell = row.getCell(j) + "";listRow.add("");//如果读取到cell不为null 则直接加入 listRow集合} else {cell = row.getCell(j).toString();listRow.add(cell);}// 在第i列 依次获取第i列的第j个位置上的值 %15s表示前后间隔15个字节输出}list.add(listRow);}}return list;} catch (IOException e) {e.printStackTrace();} finally {try {if (is != null) is.close();} catch (IOException e) {e.printStackTrace();}}return list;}
}

接口:

 /*** 缓冲区*/@PostMapping("/test")@ApiOperation("test")public R test(){String path = "C:\\Users\\70490\\Documents\\WeChat Files\\wxid_yxfik7e0yef322\\FileStorage\\File\\2020-08\\410000河南省收购站信息.xlsx";List<List<String>> lists = ReadExcel.readXlsxExcel(path);List<RanchEntity> entityList = new ArrayList<>();for (List<String> list : lists) {RanchEntity entity = new RanchEntity();entity.setRanchId(list.get(1));entity.setRanchName(list.get(2));entity.setRanchAddress(list.get(3));entity.setAreaCode(list.get(10));entity.setAreaName(list.get(7)+list.get(9)+list.get(11));entity.setMobile(list.get(6));entity.setXCoordinate(new BigDecimal(list.get(14).split(",")[0]));entity.setYCoordinate(new BigDecimal(list.get(14).split(",")[1]));ranchService.save(entity);}return R.ok();}

RanchEntity是实体 ,将Excel表格中的数据导入到数据库表中,list就是导入的表 下标是excel中的列 从零开始,想赋值给哪一个字段就选择对应的下标

Java中 POI读取Excel工具类相关推荐

  1. Java基于POI读取Excel工具类

    为什么为封装此工具类? 由于公司供应链部门业务需要,对Excel处理这块有较为严苛的要求.为了提高开发效率,从实际项目出发封装了通用自定义读取Excel工具类. 功能概述 支持读取全部excel数据 ...

  2. JAVA使用POI写入excel 工具类【通用】

    使用写入excel工具类的例子 说明:改工具类是网上公开的,来源现在找不着了,我只是在原基础上修改了下,抽离了泛型.如有侵权,请通知博主 工具类代码: package com.system.util; ...

  3. poi读取excel工具类

    1.添加依赖 <dependency><groupId>org.apache.poi</groupId><artifactId>poi</arti ...

  4. java通过poi读取excel中的日期类型数据或自定义类型日期

    java通过poi读取excel中的日期类型数据或自定义类型日期 Java 读取Excel表格日期类型数据的时候,读出来的是这样的  12-十月-2019,而Excel中输入的是 2019/10/12 ...

  5. java中poi导出Excel表格(前台流文件接收)

    java中poi导出Excel表格,前端以流的方式接收,而非直接生成文件再下载,解决多台服务器部署后,路径地址不统一导致的下载问题. 生成Excel示例图: 2.代码说明 ① 在上次的基础上增加了底部 ...

  6. 自定义POI的excel工具类-xls-xlsx

    自定义POI的excel工具类-xls-xlsx 使用jdk8(java8)实现Excel导出,随意切换 xls和xlsx.自己只是封装了比较常用方法,07版生成还有bug.ε=(´ο`*)))唉 j ...

  7. java poi 模板填数据库,java使用POI读取excel模版并向固定表格里填写数据详解

    java使用POI读取excel模版并向固定表格里填写数据详解:public class ExportExcelDemo { private HSSFWorkbook workbook = null; ...

  8. java中常用的日期工具类

    java中常用的日期工具类 日期相关的类: package net.yto.ofclacct.core.util;import java.text.ParseException; import jav ...

  9. java 兼容excel_Java解析Excel工具类(兼容xls和xlsx)

    依赖jar org.apache.poi poi-ooxml 4.0.1 ExcelUtils.java package javax.utils; import java.io.File; impor ...

最新文章

  1. 2021-07-08执行Linux的命令分析
  2. 我是大富豪php源码,最全大富豪3.4源码【自用可运营】含23款子游戏+可控制输赢工具...
  3. 双目估计方法_教你提高双目立体视觉系统的精度!
  4. 查看硬件配置的Linux命令,LINUX 查看硬件配置命令的教程
  5. DSP5509项目之用FFT识别钢琴音调(5)之开始傅里叶变换
  6. 【通信原理 入坑之路】—— 深入理解奈奎斯特第一准则与码间串扰
  7. 仿QQ和飞秋并支持语音视频白板屏幕共享的即时聊天软件
  8. 纯C语言实现动态爱心(详解,初学者也能看懂)
  9. 4.19 数椟科技远程面试 + 粉笔
  10. zcmu之水题来一波~
  11. 14个程序员常去的外国网站
  12. 三类考试新练习试题附答案
  13. 利用思维导图,快速整理小学语文复习重点,建议为孩子收藏!
  14. BugKu-图穷匕见
  15. Cadence Allegro 17.4学习记录开始05-制作封装插件2.54间距排针为例
  16. 3D打印机喷头堵塞维修
  17. sql查询需要转换时间戳
  18. [python]写一个含蓄表白器加密神器,拿去试试?
  19. 电视剧场节目管理系统 JAVA MySQL
  20. Java三大技术平台是什么?

热门文章

  1. 实现Linux与Windows双系统共存下修复磁盘分区表
  2. 关于JAVA实现二维码以及添加二维码LOGO
  3. 360断网急救箱显示为dns配置问题解决
  4. AS400(系列)第一章 入门简介
  5. linux中ls-f的用法,ls命令--Linux命令应用大词典729个命令解读
  6. Win11找不到本地用户和组怎么办?
  7. Junction.exe 命令应用详解
  8. 将迅雷集成进XP安装光盘(下)(转)
  9. 怎么测试t470p性能软件,【ThinkPadT470p(20J6002YCD)评测】高性能商务本的新选择 ThinkPad T470p评测-中关村在线...
  10. Blender Rigify版Beefy绑定下载