這里用一個(gè)簡(jiǎn)單的例子來(lái)演示 java操作excel文件
用maven可以把包導(dǎo)入進(jìn)來(lái):
1
2
3
4
5
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.example.demo.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
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;
/**
讀取Excel
-
@author zengwendong
*/
public class ReadExcelUtils {private Workbook wb;
private Sheet sheet;
private Row row;public ReadExcelUtils(String filepath) {
if (filepath == null) {
return;
}try { InputStream is = new FileInputStream(filepath); String ext = filepath.substring(filepath.lastIndexOf(".")); if (".xls".equals(ext)) { wb = new HSSFWorkbook(is); } else if (".xlsx".equals(ext)) { wb = new XSSFWorkbook(is); } else { wb = null; } } catch (FileNotFoundException e) { System.out.println(e.getLocalizedMessage() + "找不到文件"); } catch (IOException e) { System.out.println(e.getMessage() + "io的異常"); }
}
/**
- 讀取Excel表格表頭的內(nèi)容
- @param InputStream
- @return String 表頭內(nèi)容的數(shù)組
- @author zengwendong
*/
public String[] readExcelTitle() throws Exception {
if (wb == null) {
throw new Exception("Workbook對(duì)象為空!");
}
sheet = wb.getSheetAt(0);
row = sheet.getRow(0);
// 標(biāo)題總列數(shù)
int colNum = row.getPhysicalNumberOfCells();
System.out.println("colNum:" + colNum);
String[] title = new String[colNum];
for (int i = 0; i < colNum; i++) {
// title[i] = getStringCellValue(row.getCell((short) i));
title[i] = row.getCell(i).getCellFormula();
}
return title;
}
/**
讀取Excel數(shù)據(jù)內(nèi)容
@param InputStream
@return Map 包含單元格數(shù)據(jù)內(nèi)容的Map對(duì)象
-
@author zengwendong
*/
public Map<Integer, Map<Integer, Object>> readExcelContent() throws Exception {
if (wb == null) {
throw new Exception("Workbook對(duì)象為空绞铃!");
}
Map<Integer, Map<Integer, Object>> content = new HashMap<Integer, Map<Integer, Object>>();sheet = wb.getSheetAt(0);
// 得到總行數(shù)
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
// 正文內(nèi)容應(yīng)該從第二行開(kāi)始,第一行為表頭的標(biāo)題
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 0;
Map<Integer, Object> cellValue = new HashMap<Integer, Object>();
while (j < colNum) {
Object obj = getCellFormatValue(row.getCell(j));
cellValue.put(j, obj);
j++;
}
content.put(i, cellValue);
}
return content;
}
/**
根據(jù)Cell類型設(shè)置數(shù)據(jù)
@param cell
@return
-
@author zengwendong
*/
private Object getCellFormatValue(Cell cell) {
Object cellvalue = "";
if (cell != null) {
// 判斷當(dāng)前Cell的Type
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:// 如果當(dāng)前Cell的Type為NUMERIC
case Cell.CELL_TYPE_FORMULA: {
// 判斷當(dāng)前的cell是否為Date
if (DateUtil.isCellDateFormatted(cell)) {
// 如果是Date類型則儿捧,轉(zhuǎn)化為Data格式
// data格式是帶時(shí)分秒的:2013-7-10 0:00:00
// cellvalue = cell.getDateCellValue().toLocaleString();
// data格式是不帶帶時(shí)分秒的:2013-7-10
Date date = cell.getDateCellValue();
cellvalue = date;
} else {// 如果是純數(shù)字// 取得當(dāng)前Cell的數(shù)值 cellvalue = String.valueOf(cell.getNumericCellValue()); } break; } case Cell.CELL_TYPE_STRING:// 如果當(dāng)前Cell的Type為STRING // 取得當(dāng)前的Cell字符串 cellvalue = cell.getRichStringCellValue().getString(); break; default:// 默認(rèn)的Cell值 cellvalue = ""; }
} else {
cellvalue = "";
}
return cellvalue;
}
public static void main(String[] args) {
try {
String filepath = "F:user.xls";
ReadExcelUtils excelReader = new ReadExcelUtils(filepath);
// 對(duì)讀取Excel表格標(biāo)題測(cè)試
// String[] title = excelReader.readExcelTitle();
// System.out.println("獲得Excel表格的標(biāo)題:");
// for (String s : title) {
// System.out.print(s + " ");
// }
// 對(duì)讀取Excel表格內(nèi)容測(cè)試
Map<Integer, Map<Integer, Object>> map = excelReader.readExcelContent();
System.out.println("獲得Excel表格的內(nèi)容:");
for (int i = 1; i <= map.size(); i++) {
System.out.println(map.get(i));
}
} catch (FileNotFoundException e) {
System.out.println("未找到指定路徑的文件!");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}