标签:
这篇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