这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx。

读取excel和MySQL相关: java的poi技术读取Excel数据到MySQL

你也可以在 : java的poi技术读取和导入Excel了解到写入Excel的方法信息

使用JXL技术 :java的jxl技术导入Excel 

下面是本文的项目结构:

项目中所需要的jar文件:

所用的Excel数据(2003-2007,2010都是一样的数据

运行效果:

=================================================

源码部分:

=================================================

/Excel2010/src/com/b510/common/Common.java

 1 /**
2 *
3 */
4 package com.b510.common;
5
6 /**
7 * @author Hongten
8 * @created 2014-5-21
9 */
10 public class Common {
11
12 public static final String OFFICE_EXCEL_2003_POSTFIX = "xls";
13 public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx";
14
15 public static final String EMPTY = "";
16 public static final String POINT = ".";
17 public static final String LIB_PATH = "lib";
18 public static final String STUDENT_INFO_XLS_PATH = LIB_PATH + "/student_info" + POINT + OFFICE_EXCEL_2003_POSTFIX;
19 public static final String STUDENT_INFO_XLSX_PATH = LIB_PATH + "/student_info" + POINT + OFFICE_EXCEL_2010_POSTFIX;
20 public static final String NOT_EXCEL_FILE = " : Not the Excel file!";
21 public static final String PROCESSING = "Processing...";
22
23 }

/Excel2010/src/com/b510/excel/ReadExcel.java

  1 /**
2 *
3 */
4 package com.b510.excel;
5
6 import java.io.FileInputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.util.ArrayList;
10 import java.util.List;
11
12 import org.apache.poi.hssf.usermodel.HSSFCell;
13 import org.apache.poi.hssf.usermodel.HSSFRow;
14 import org.apache.poi.hssf.usermodel.HSSFSheet;
15 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
16 import org.apache.poi.xssf.usermodel.XSSFCell;
17 import org.apache.poi.xssf.usermodel.XSSFRow;
18 import org.apache.poi.xssf.usermodel.XSSFSheet;
19 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
20
21 import com.b510.common.Common;
22 import com.b510.excel.util.Util;
23 import com.b510.excel.vo.Student;
24
25 /**
26 * @author Hongten
27 * @created 2014-5-20
28 */
29 public class ReadExcel {
30
31 /**
32 * read the Excel file
33 * @param path the path of the Excel file
34 * @return
35 * @throws IOException
36 */
37 public List<Student> readExcel(String path) throws IOException {
38 if (path == null || Common.EMPTY.equals(path)) {
39 return null;
40 } else {
41 String postfix = Util.getPostfix(path);
42 if (!Common.EMPTY.equals(postfix)) {
43 if (Common.OFFICE_EXCEL_2003_POSTFIX.equals(postfix)) {
44 return readXls(path);
45 } else if (Common.OFFICE_EXCEL_2010_POSTFIX.equals(postfix)) {
46 return readXlsx(path);
47 }
48 } else {
49 System.out.println(path + Common.NOT_EXCEL_FILE);
50 }
51 }
52 return null;
53 }
54
55 /**
56 * Read the Excel 2010
57 * @param path the path of the excel file
58 * @return
59 * @throws IOException
60 */
61 public List<Student> readXlsx(String path) throws IOException {
62 System.out.println(Common.PROCESSING + path);
63 InputStream is = new FileInputStream(path);
64 XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
65 Student student = null;
66 List<Student> list = new ArrayList<Student>();
67 // Read the Sheet
68 for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
69 XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
70 if (xssfSheet == null) {
71 continue;
72 }
73 // Read the Row
74 for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
75 XSSFRow xssfRow = xssfSheet.getRow(rowNum);
76 if (xssfRow != null) {
77 student = new Student();
78 XSSFCell no = xssfRow.getCell(0);
79 XSSFCell name = xssfRow.getCell(1);
80 XSSFCell age = xssfRow.getCell(2);
81 XSSFCell score = xssfRow.getCell(3);
82 student.setNo(getValue(no));
83 student.setName(getValue(name));
84 student.setAge(getValue(age));
85 student.setScore(Float.valueOf(getValue(score)));
86 list.add(student);
87 }
88 }
89 }
90 return list;
91 }
92
93 /**
94 * Read the Excel 2003-2007
95 * @param path the path of the Excel
96 * @return
97 * @throws IOException
98 */
99 public List<Student> readXls(String path) throws IOException {
100 System.out.println(Common.PROCESSING + path);
101 InputStream is = new FileInputStream(path);
102 HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
103 Student student = null;
104 List<Student> list = new ArrayList<Student>();
105 // Read the Sheet
106 for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
107 HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
108 if (hssfSheet == null) {
109 continue;
110 }
111 // Read the Row
112 for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
113 HSSFRow hssfRow = hssfSheet.getRow(rowNum);
114 if (hssfRow != null) {
115 student = new Student();
116 HSSFCell no = hssfRow.getCell(0);
117 HSSFCell name = hssfRow.getCell(1);
118 HSSFCell age = hssfRow.getCell(2);
119 HSSFCell score = hssfRow.getCell(3);
120 student.setNo(getValue(no));
121 student.setName(getValue(name));
122 student.setAge(getValue(age));
123 student.setScore(Float.valueOf(getValue(score)));
124 list.add(student);
125 }
126 }
127 }
128 return list;
129 }
130
131 @SuppressWarnings("static-access")
132 private String getValue(XSSFCell xssfRow) {
133 if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {
134 return String.valueOf(xssfRow.getBooleanCellValue());
135 } else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {
136 return String.valueOf(xssfRow.getNumericCellValue());
137 } else {
138 return String.valueOf(xssfRow.getStringCellValue());
139 }
140 }
141
142 @SuppressWarnings("static-access")
143 private String getValue(HSSFCell hssfCell) {
144 if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
145 return String.valueOf(hssfCell.getBooleanCellValue());
146 } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
147 return String.valueOf(hssfCell.getNumericCellValue());
148 } else {
149 return String.valueOf(hssfCell.getStringCellValue());
150 }
151 }
152 }

/Excel2010/src/com/b510/excel/client/Client.java

 1 /**
2 *
3 */
4 package com.b510.excel.client;
5
6 import java.io.IOException;
7 import java.util.List;
8
9 import com.b510.common.Common;
10 import com.b510.excel.ReadExcel;
11 import com.b510.excel.vo.Student;
12
13 /**
14 * @author Hongten
15 * @created 2014-5-21
16 */
17 public class Client {
18
19 public static void main(String[] args) throws IOException {
20 String excel2003_2007 = Common.STUDENT_INFO_XLS_PATH;
21 String excel2010 = Common.STUDENT_INFO_XLSX_PATH;
22 // read the 2003-2007 excel
23 List<Student> list = new ReadExcel().readExcel(excel2003_2007);
24 if (list != null) {
25 for (Student student : list) {
26 System.out.println("No. : " + student.getNo() + ", name : " + student.getName() + ", age : " + student.getAge() + ", score : " + student.getScore());
27 }
28 }
29 System.out.println("======================================");
30 // read the 2010 excel
31 List<Student> list1 = new ReadExcel().readExcel(excel2010);
32 if (list1 != null) {
33 for (Student student : list1) {
34 System.out.println("No. : " + student.getNo() + ", name : " + student.getName() + ", age : " + student.getAge() + ", score : " + student.getScore());
35 }
36 }
37 }
38 }

/Excel2010/src/com/b510/excel/util/Util.java

 1 /**
2 *
3 */
4 package com.b510.excel.util;
5
6 import com.b510.common.Common;
7
8 /**
9 * @author Hongten
10 * @created 2014-5-21
11 */
12 public class Util {
13
14 /**
15 * get postfix of the path
16 * @param path
17 * @return
18 */
19 public static String getPostfix(String path) {
20 if (path == null || Common.EMPTY.equals(path.trim())) {
21 return Common.EMPTY;
22 }
23 if (path.contains(Common.POINT)) {
24 return path.substring(path.lastIndexOf(Common.POINT) + 1, path.length());
25 }
26 return Common.EMPTY;
27 }
28 }

/Excel2010/src/com/b510/excel/vo/Student.java

 1 /**
2 *
3 */
4 package com.b510.excel.vo;
5
6 /**
7 * Student
8 *
9 * @author Hongten
10 * @created 2014-5-18
11 */
12 public class Student {
13 /**
14 * id
15 */
16 private Integer id;
17 /**
18 * 学号
19 */
20 private String no;
21 /**
22 * 姓名
23 */
24 private String name;
25 /**
26 * 学院
27 */
28 private String age;
29 /**
30 * 成绩
31 */
32 private float score;
33
34 public Integer getId() {
35 return id;
36 }
37
38 public void setId(Integer id) {
39 this.id = id;
40 }
41
42 public String getNo() {
43 return no;
44 }
45
46 public void setNo(String no) {
47 this.no = no;
48 }
49
50 public String getName() {
51 return name;
52 }
53
54 public void setName(String name) {
55 this.name = name;
56 }
57
58 public String getAge() {
59 return age;
60 }
61
62 public void setAge(String age) {
63 this.age = age;
64 }
65
66 public float getScore() {
67 return score;
68 }
69
70 public void setScore(float score) {
71 this.score = score;
72 }
73
74 }

java的poi技术读取Excel数据的更多相关文章

  1. java的poi技术读取Excel数据到MySQL

    这篇blog是介绍java中的poi技术读取Excel数据,然后保存到MySQL数据中. 你也可以在 : java的poi技术读取和导入Excel了解到写入Excel的方法信息 使用JXL技术可以在 ...

  2. java的poi技术读取Excel[2003-2007,2010]

    这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...

  3. java的poi技术读取Excel[2003-2007,2010]

    这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...

  4. java的poi技术写Excel的Sheet

    在这之前写过关于java读,写Excel的blog如下: Excel转Html java的poi技术读,写Excel[2003-2007,2010] java的poi技术读取Excel[2003-20 ...

  5. java的poi技术读取和导入Excel实例

    本篇文章主要介绍了java的poi技术读取和导入Excel实例,报表输出是Java应用开发中经常涉及的内容,有需要的可以了解一下. 报表输出是Java应用开发中经常涉及的内容,而一般的报表往往缺乏通用 ...

  6. java的poi技术读取和导入Excel

    项目结构: http://www.cnblogs.com/hongten/gallery/image/111987.html  用到的Excel文件: http://www.cnblogs.com/h ...

  7. java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)

    使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...

  8. java 使用POI批量导入excel数据

    一.定义 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 二.所需jar包: 三.简单的一个读取e ...

  9. Java使用poi包读取Excel文档

    项目需要解析Excel文档获取数据,就在网上找了一些资料,结合自己这次使用,写下心得: 1.maven项目需加入如下依赖: <dependency> <groupId>org. ...

随机推荐

  1. 深入学习Make命令和Makefile(上)

    https://www.zybuluo.com/lishuhuakai/note/209302 深入学习Make命令和Makefile(上) make是Linux下的一款程序自动维护工具,配合make ...

  2. java(3) 面向对象

    1.super关键字 * 使用super关键字调用父类的成员变量和成员方法.具体格式: super.成员变量 super.成员方法([参数1,参数2...]) * 使用super关键字调用父类的构造方 ...

  3. [转]F5负载均衡算法及基本原理

    原文:Intro to Load Balancing for Developers – The Algorithms 转载:http://blog.gesha.net/archives/205/  p ...

  4. tcp连接出现close_wait状态?可能是代码不够健壮

    一.问题概述 今天遇到个小问题. 我们的程序依赖了大数据那边的服务,大数据那边提供了restful接口供我们调用. 测试反映接口有问题,我在本地重现了. 我这边感觉抓包可能对分析问题有用,就用wire ...

  5. 禁用ngen版本的.NET Framework dll加载

    在调试时会发现出于性能考虑.NET Framework dll加载的都是ngen版本,比如:System.dll,实际加载System.ni.dll. 如果希望加载非ngen版本,可以设置系统环境变量 ...

  6. spring boot 部署

    指定运行的内存 java -Xms10m -Xmx200m -jar xxx.jar spring boot 打包成war包: 让 SpringbootApplication 类继承 SpringBo ...

  7. express运行原理

    一.express底层:http模块 Express框架建立在node.js内置的http模块上.http模块生成服务器的原始代码如下. var http = require("http&q ...

  8. C++类继承示例

    C++的子类与孙子类都实现了虚函数时,孙子类的实现会覆盖掉子类的实现. 继承的最主要的应用就是把不同的类放到一个数组中,然后遍历调用同名函数. 实例如下: #include <iostream& ...

  9. iOS - 网址、链接、网页地址、下载链接等正则表达式匹配(解决url包含中文不能编码的问题)

    DNS规定,域名中的标号都由英文字母和数字组成,每一个标号不超过63个字符,也不区分大小写字母.标号中除连字符(-)外不能使用其他的标点符号.级别最低的域名写在最左边,而级别最高的域名写在最右边.由多 ...

  10. Elasticsearch 与 Mongodb 数据同步问题

    1.mongo-connector工具 首先安装python环境 wget http://www.python.org/ftp/python/3.0.1/Python-3.0.1.tgz tar -z ...