2019独角兽企业重金招聘Python工程师标准>>>

excel文件内容:

项目目录:

D:\code\smvc\phone-poi>tree /f
卷 软件 的文件夹 PATH 列表
卷序列号为 0000-CD08
D:.
│  .classpath
│  .project
│  pom.xml
│
├─.settings
│      org.eclipse.core.resources.prefs
│      org.eclipse.jdt.core.prefs
│      org.eclipse.m2e.core.prefs
│
├─m
│      student.xls
│      student.xlsx
│
├─src
│  ├─main
│  │  └─java
│  │      └─com
│  │          └─laolang
│  │              ├─officeutil
│  │              │      StudentExcelUtil.java
│  │              │
│  │              └─pojo
│  │                      Student.java
│  │
│  └─test
│      └─java
│          └─com
│              └─laolang
│                  └─officeutil
│                          StudentExcelUtilTest.java
│
└─wpsstudent.xlsstudent.xlsxD:\code\smvc\phone-poi>

这里共有四个excel文件,m 文件夹下的是我用microsoft excel 2010 编辑的,wps下的是我用 wps编辑的,每个文件夹下都有 97-03、07两种版本,内容基本上是一样的

代码:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.laolang.phone-study</groupId><artifactId>phone-poi</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>phone-poi</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.9</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.9</version></dependency></dependencies>
</project>

com.laolang.pojo.Student

package com.laolang.pojo;public class Student {public Student() {super();}public Student(String name, int age, String sex) {super();this.name = name;this.age = age;this.sex = sex;}public Student(int id, String name, int age, String sex) {super();this.id = id;this.name = name;this.age = age;this.sex = sex;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", age=" + age+ ", sex=" + sex + "]";}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}private int id;private String name;private int age;private String sex;
}

com.laolang.officeutil.StudentExcelUtil

package com.laolang.officeutil;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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.usermodel.WorkbookFactory;import com.laolang.pojo.Student;public class StudentExcelUtil {public static List<Student> execelToStudent(String filename) {List<Student> stuList = new ArrayList<Student>();File excelFile = new File(filename);try {FileInputStream is = new FileInputStream(excelFile);Workbook workbook = WorkbookFactory.create(is);//这种方式 Excel 2003/2007/2010 都是可以处理的int sheetCount = workbook.getNumberOfSheets();//Sheet的数量 //遍历每个Sheet  for (int s = 0; s < sheetCount; s++) {Sheet sheet = workbook.getSheetAt(s);//取得当前sheetint rowCount = sheet.getPhysicalNumberOfRows();//获取总行数  //遍历第一行,因为第一行,也就是索引为0的那一行是标题,所以这里从第二行也就是索引为1的行开始遍历for (int r = 1; r < rowCount; r++) {Student stu = new Student();Row row = sheet.getRow(r);int cellCount = row.getPhysicalNumberOfCells();//获取总列数 for (int c = 0; c < cellCount; c++) {Cell cell = row.getCell(c);switch (c) {case 0: {//我没有发现直接攻取int的方法,又不想先取其内容类型再取值,//所以我这里的方法是取出double型数据,然后再强转为intstu.setId((int)cell.getNumericCellValue());break;}case 1: {stu.setName(cell.getStringCellValue());break;}case 2: {stu.setAge((int)cell.getNumericCellValue());break;}case 3: {stu.setSex(cell.getStringCellValue());break;}}}stuList.add(stu);}}} catch (FileNotFoundException e) {e.printStackTrace();} catch (InvalidFormatException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return stuList;}
}

com.laolang.officeutil.StudentExcelUtilTest

package com.laolang.officeutil;import java.util.List;import org.junit.Test;import com.laolang.pojo.Student;public class StudentExcelUtilTest {@Testpublic void testReadM(){System.out.println("microsoft 97-03:");List<Student> stuList = StudentExcelUtil.execelToStudent("m/student.xlsx");for( Student stu : stuList ){System.out.println(stu);}System.out.println("microsoft 97-03:");stuList = StudentExcelUtil.execelToStudent("m/student.xlsx");for( Student stu : stuList ){System.out.println(stu);}}@Testpublic void testReadWps(){System.out.println("wpd 97-03:");List<Student> stuList = StudentExcelUtil.execelToStudent("wps/student.xlsx");for( Student stu : stuList ){System.out.println(stu);}System.out.println("wps:");stuList = StudentExcelUtil.execelToStudent("m/student.xlsx");for( Student stu : stuList ){System.out.println(stu);}}
}

运行结果:

D:\code\smvc\phone-poi>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building phone-poi 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ phone-poi ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\code\smvc\phone-poi\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ phone-poi ---
[INFO] Compiling 2 source files to D:\code\smvc\phone-poi\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ phone-poi ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\code\smvc\phone-poi\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ phone-poi ---
[INFO] Compiling 1 source file to D:\code\smvc\phone-poi\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.7.2:test (default-test) @ phone-poi ---
[INFO] Surefire report directory: D:\code\smvc\phone-poi\target\surefire-reports-------------------------------------------------------T E S T S
-------------------------------------------------------
Running com.laolang.officeutil.StudentExcelUtilTest
microsoft 97-03:
Student [id=1001, name=小代码, age=23, sex=男]
Student [id=1002, name=老狼, age=34, sex=男]
Student [id=1003, name=天涯, age=35, sex=男]
Student [id=1004, name=行者, age=23, sex=男]
microsoft 97-03:
Student [id=1001, name=小代码, age=23, sex=男]
Student [id=1002, name=老狼, age=34, sex=男]
Student [id=1003, name=天涯, age=35, sex=男]
Student [id=1004, name=行者, age=23, sex=男]
wpd 97-03:
Student [id=1001, name=小代码, age=23, sex=男]
Student [id=1002, name=老狼, age=34, sex=男]
Student [id=1003, name=行者, age=35, sex=男]
Student [id=1004, name=天涯, age=23, sex=男]
wps:
Student [id=1001, name=小代码, age=23, sex=男]
Student [id=1002, name=老狼, age=34, sex=男]
Student [id=1003, name=天涯, age=35, sex=男]
Student [id=1004, name=行者, age=23, sex=男]
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.82 secResults :Tests run: 2, Failures: 0, Errors: 0, Skipped: 0[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.330s
[INFO] Finished at: Wed Mar 16 14:17:58 CST 2016
[INFO] Final Memory: 11M/19M
[INFO] ------------------------------------------------------------------------
D:\code\smvc\phone-poi>

转载于:https://my.oschina.net/iamhere/blog/638481

poi 读取excel相关推荐

  1. java利用poi读取excel_java利用POI 读取EXCEL

    /* * 使用POI读取EXCEL文件 */ import java.io.File; import java.io.FileInputStream; import java.util.ArrayLi ...

  2. Java开发小技巧(六):使用Apache POI读取Excel

    前言 在数据仓库中,ETL最基础的步骤就是从数据源抽取所需的数据,这里所说的数据源并非仅仅是指数据库,还包括excel.csv.xml等各种类型的数据接口文件,而这些文件中的数据不一定是结构化存储的, ...

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

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

  4. POI读取Excel内容格式化

    在用POI读取Excel内容时,经常会遇到数据格式化的问题. 比如:数字12365会变为12365.0;字符串数字123也会变为123.0,甚至会被变为科学计数法.另外日期格式化也是一个头疼的问题.其 ...

  5. 使用POI 读取 Excel 文件,读取手机号码 变成 1.3471022771E10

    使用POI 读取 Excel 文件,读取手机号码 变成 1.3471022771E10 [问题点数:40分,结帖人xieyongqiu] 不显示删除回复             显示所有回复     ...

  6. POI读取Excel 各种特殊数字和类型的转换

    POI读取Excel 各种特殊数字和类型的转换.取值带一个E http://blog.csdn.net/johnstrive/article/details/8393541 POI解析Excel[po ...

  7. Java教程:使用POI读取excel文档(根据BV1bJ411G7Aw整理)

    Java教程:使用POI读取excel文档(根据BV1bJ411G7Aw整理) 最近公司需要我做一个导出Excel表格的功能,为此来学习一下POI,在这里记录一下学习笔记.B站直接搜BV1bJ411G ...

  8. poi读取excel多层表头模板写入数据并导出

    poi读取excel多层表头模板写入数据并导出 这两天刚好写excel,写了一份自定义表头的,写了一份模板的,这里展示一份读取excel模板写入数据并导出的 //title excel的名称 head ...

  9. java不用poi怎么读取excel,java-无法使用Apache POI读取Excel

    您必须包括poi jar文件.它的版本将是4.1.0.如果使用的是Maven pom.xml,请包括以下依赖项. org.apache.poi poi-ooxml 4.1.0 org.apache.p ...

  10. poi读取Excel日期为数字的解决方法

    这个问题虽然也比较常见,解决办法也比较简单,但是网上有一些代码不全,思路混乱,乱七八糟的办法,容易误导大家,特地来为大家开路 这里分享一下我的一个思路 Maven依赖 <!--POI--> ...

最新文章

  1. ODP.NET调用存储需要使用事务
  2. Java中的随机数生成器:Random,ThreadLocalRandom,SecureRandom
  3. django性能优化缓存view详解
  4. ​最新综述!基于图神经网络的关系抽取技术进展
  5. Thymeleaf——在不覆盖现有class属性的情况下动态添加CSS class解决方案
  6. windows下apache报错The requested operation has failed解决方法
  7. 基于eclipse RCP的文件夹管理工具
  8. 【Python】Python3.7.3 源代码编译安装 CentOS
  9. cmake同时生成动态库与静态库的方法
  10. 【渝粤教育】国家开放大学2018年秋季 1301T病理生理学 参考试题
  11. SHELL中如何获得指定字符的位置及正确的截取动作
  12. python逻辑运算符例子_python运算符-实战中常用的三个逻辑运算符使用实例
  13. 深度学习教程(14) | 序列模型与RNN网络(吴恩达·完整版)
  14. 各种绩效考核方法的区别
  15. python必考题_干货|Python经典面试考题(下)
  16. 系统介绍python魔法方法
  17. 开放计算何处觅?JDM安天下
  18. D2D加载图片资源(2)
  19. 80核处理器_最受欢迎的处理器 酷睿i5-9400F果然霸榜了
  20. eoLinker-AMS接口管理系统 项目管理教程

热门文章

  1. Python 按行读取文件内按分隔符分割字符串(去除空格和换行、字符串分割)
  2. 语法分析与中间代码生成
  3. Ajax提交打开新窗口,浏览器拦截处理;以及跨域问题
  4. phoneGap工程的创建
  5. Ubuntu 12.04 Server OpenStack Havana多节点(OVS+GRE)安装
  6. 数据中台必备的4个核心能力,你让数据创造价值了吗?
  7. python接口测试第二期_Python接口测试实战2 - 使用Python发送请求
  8. 大学生利用漏洞薅肯德基羊毛,获刑两年半
  9. GitHub被“中介”攻击了?中间人攻击?
  10. 中台唯一的胜利果实:大数据中台架构详解