Java读取Excel内容
借助于apathe的poi.jar,由于上传文件不支持.jar所以请下载后将文件改为.jar,在应用程序中添加poi.jar包,并将需要读取的excel文件放入根目录即可
本例使用Java来读取excel的内容并展出出结果,代码如下:
[java] view plain copy
01.import java.io.BufferedInputStream;
02.
03.import java.io.File;
04.
05.import java.io.FileInputStream;
06.
07.import java.io.FileNotFoundException;
08.
09.import java.io.IOException;
10.
11.import java.text.DecimalFormat;
12.
13.import java.text.SimpleDateFormat;
14.
15.import java.util.ArrayList;
16.
17.import java.util.Arrays;
18.
19.import java.util.Date;
20.
21.import java.util.List;
22.
23.
24.
25.import org.apache.poi.hssf.usermodel.HSSFCell;
26.
27.import org.apache.poi.hssf.usermodel.HSSFDateUtil;
28.
29.import org.apache.poi.hssf.usermodel.HSSFRow;
30.
31.import org.apache.poi.hssf.usermodel.HSSFSheet;
32.
33.import org.apache.poi.hssf.usermodel.HSSFWorkbook;
34.
35.import org.apache.poi.poifs.filesystem.POIFSFileSystem;
36.
37.
38.
39.public class ExcelOperate {
40.
41.
42.
43. public static void main(String[] args) throws Exception {
44.
45. File file = new File("ExcelDemo.xls");
46.
47. String[][] result = getData(file, 1);
48.
49. int rowLength = result.length;
50.
51. for(int i=0;i result = new ArrayList();
88.
89. int rowSize = 0;
90.
91. BufferedInputStream in = new BufferedInputStream(new FileInputStream(
92.
93. file));
94.
95. // 打开HSSFWorkbook
96.
97. POIFSFileSystem fs = new POIFSFileSystem(in);
98.
99. HSSFWorkbook wb = new HSSFWorkbook(fs);
100.
101. HSSFCell cell = null;
102.
103. for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
104.
105. HSSFSheet st = wb.getSheetAt(sheetIndex);
106.
107. // 第一行为标题,不取
108.
109. for (int rowIndex = ignoreRows; rowIndex <= st.getLastRowNum(); rowIndex++) {
110.
111. HSSFRow row = st.getRow(rowIndex);
112.
113. if (row == null) {
114.
115. continue;
116.
117. }
118.
119. int tempRowSize = row.getLastCellNum() + 1;
120.
121. if (tempRowSize > rowSize) {
122.
123. rowSize = tempRowSize;
124.
125. }
126.
127. String[] values = new String[rowSize];
128.
129. Arrays.fill(values, "");
130.
131. boolean hasValue = false;
132.
133. for (short columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex++) {
134.
135. String value = "";
136.
137. cell = row.getCell(columnIndex);
138.
139. if (cell != null) {
140.
141. // 注意:一定要设成这个,否则可能会出现乱码
142.
143. cell.setEncoding(HSSFCell.ENCODING_UTF_16);
144.
145. switch (cell.getCellType()) {
146.
147. case HSSFCell.CELL_TYPE_STRING:
148.
149. value = cell.getStringCellValue();
150.
151. break;
152.
153. case HSSFCell.CELL_TYPE_NUMERIC:
154.
155. if (HSSFDateUtil.isCellDateFormatted(cell)) {
156.
157. Date date = cell.getDateCellValue();
158.
159. if (date != null) {
160.
161. value = new SimpleDateFormat("yyyy-MM-dd")
162.
163. .format(date);
164.
165. } else {
166.
167. value = "";
168.
169. }
170.
171. } else {
172.
173. value = new DecimalFormat("0").format(cell
174.
175. .getNumericCellValue());
176.
177. }
178.
179. break;
180.
181. case HSSFCell.CELL_TYPE_FORMULA:
182.
183. // 导入时如果为公式生成的数据则无值
184.
185. if (!cell.getStringCellValue().equals("")) {
186.
187. value = cell.getStringCellValue();
188.
189. } else {
190.
191. value = cell.getNumericCellValue() + "";
192.
193. }
194.
195. break;
196.
197. case HSSFCell.CELL_TYPE_BLANK:
198.
199. break;
200.
201. case HSSFCell.CELL_TYPE_ERROR:
202.
203. value = "";
204.
205. break;
206.
207. case HSSFCell.CELL_TYPE_BOOLEAN:
208.
209. value = (cell.getBooleanCellValue() == true ? "Y"
210.
211. : "N");
212.
213. break;
214.
215. default:
216.
217. value = "";
218.
219. }
220.
221. }
222.
223. if (columnIndex == 0 && value.trim().equals("")) {
224.
225. break;
226.
227. }
228.
229. values[columnIndex] = rightTrim(value);
230.
231. hasValue = true;
232.
233. }
234.
235.
236.
237. if (hasValue) {
238.
239. result.add(values);
240.
241. }
242.
243. }
244.
245. }
246.
247. in.close();
248.
249. String[][] returnArray = new String[result.size()][rowSize];
250.
251. for (int i = 0; i < returnArray.length; i++) {
252.
253. returnArray[i] = (String[]) result.get(i);
254.
255. }
256.
257. return returnArray;
258.
259. }
260.
261.
262.
263. /**
264.
265. * 去掉字符串右边的空格
266.
267. * @param str 要处理的字符串
268.
269. * @return 处理后的字符串
270.
271. */
272.
273. public static String rightTrim(String str) {
274.
275. if (str == null) {
276.
277. return "";
278.
279. }
280.
281. int length = str.length();
282.
283. for (int i = length - 1; i >= 0; i--) {
284.
285. if (str.charAt(i) != 0x20) {
286.
287. break;
288.
289. }
290.
291. length--;
292.
293. }
294.
295. return str.substring(0, length);
296.
297. }
298.
299.}