Weitere ähnliche Inhalte
Ähnlich wie Java 操作 Excel (读) (20)
Mehr von wensheng wei (20)
Java 操作 Excel (读)
- 1. 一个项目要求 读 Excel 表格,于是 Google、baidu 查了查,看到不少关于 Java 读 Excel 到文章,讲解的
比较详细。今天开博,第一篇文章,对 Java 读 Excel 进行一下总结,内容全都是来自网络,我只是做了一些修
改、总结。经过挑选决定采用 poi 和 fastexcel。
1、poi 读取 Excel (poi-3.2-FINAL-20081019)
Java 代码
1. // 构造 HSSFWorkbook 对象,strPath 传入文件路径
2. HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(strPath));
3. // 读取文件中的第一张表格
4. HSSFSheet sheet = wb.getSheetAt(0);
5. // 定义 row、cell
6. HSSFRow row;
7. // HSSFCell cell;
8. String cell;
9. // 循环输出表格中的内容
10. for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
11. row = sheet.getRow(i);
12. for (int j = row.getFirstCellNum(); j < row.getPhysicalNumberOfCells(); j++) {
13. // 推荐通过 row.getCell(j).toString() 获取单元格内容,
14. cell = row.getCell(j).toString();
15. System.out.print(cell + "t");
16. }
17. System.out.println("");
18.} // 构造 HSSFWorkbook 对象,strPath 传入文件路径
19. HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(strPath));
20. // 读取文件中的第一张表格
21. HSSFSheet sheet = wb.getSheetAt(0);
22. // 定义 row、cell
23. HSSFRow row;
24. // HSSFCell cell;
25. String cell;
26. // 循环输出表格中的内容
27. for (int i = sheet.getFirstRowNum(); i <
sheet.getPhysicalNumberOfRows(); i++) {
28. row = sheet.getRow(i);
29. for (int j = row.getFirstCellNum(); j <
row.getPhysicalNumberOfCells(); j++) {
30. // 推荐通过 row.getCell(j).toString() 获取单元格内容,
31. cell = row.getCell(j).toString();
32. System.out.print(cell + "t");
33. }
34. System.out.println("");
35. }
2、fastexcel 读 Excel (fastexcel-0.2-2009-01-16)
- 2. Java 代码
1. // 打开表格文件,strPath 设置文件所在路径
2. Workbook workBook = FastExcel.createReadableWorkbook(new File(strPath));
3. workBook.open();
4.
5. // 基于事件的读取
6. // workBook.getSheet(0, new SheetReadAdapter() {
7. // public void onCell(int row, int col, String content) {
8. // System.out.println(row + "," + col + "," + content);
9. // }
10. // });
11.
12. // 基本的读取
13. // 操作第一张表格
14. Sheet s = workBook.getSheet(0);
15. // 设置 row、cell
16. String[] row;
17. String cell;
18. // 循环输出表格内容
19. for (int i = s.getFirstRow(); i < s.getLastRow(); i++) {
20. row = s.getRow(i);
21. for (int j = s.getFirstColumn(); j < s.getLastColumn(); j++) {
22. cell = s.getCell(i, j);
23. System.out.print(cell + "t");
24. }
25. System.out.println("");
26. }
27. // 关闭连接
28. workBook.close(); // 打开表格文件,strPath 设置文件所在路径
29. Workbook workBook = FastExcel.createReadableWorkbook(new
File(strPath));
30. workBook.open();
31.
32. // 基于事件的读取
33.// workBook.getSheet(0, new SheetReadAdapter() {
34.// public void onCell(int row, int col, String content) {
35.// System.out.println(row + "," + col + "," + content);
36.// }
37.// });
38.
39. // 基本的读取
40. // 操作第一张表格
41. Sheet s = workBook.getSheet(0);
42. // 设置 row、cell
43. String[] row;
44. String cell;
45. // 循环输出表格内容
46. for (int i = s.getFirstRow(); i < s.getLastRow(); i++) {
47. row = s.getRow(i);
48. for (int j = s.getFirstColumn(); j < s.getLastColumn(); j++) {
49. cell = s.getCell(i, j);
50. System.out.print(cell + "t");
51. }
52. System.out.println("");
53. }
54. // 关闭连接
- 3. 55. workBook.close();
3、简单测试读取速度 (Intel(R) Pentium(R) Dual T2330 @ 1.60GHz 内存:2G)
Java 代码
1. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS");
2. TimeZone t = sdf.getTimeZone();
3. t.setRawOffset(0);
4. sdf.setTimeZone(t);
5. Long startTime = System.currentTimeMillis();
6. // 检测代码
7. String fileName = "F:我的文档专业实训07 信管缴费代码.xls";
8. try {
9. // 性能更好,读取更快
10. FastexcelReadExcel fre = new FastexcelReadExcel();
11. fre.testFastExcel(fileName);
12. } catch (Exception ex) {
13. Logger.getLogger(FastexcelReadExcel.class.getName()).log(Level.SEVERE, null, ex);
14. }
15. Long endTime = System.currentTimeMillis();
16. System.out.println("用时:" + sdf.format(new Date(endTime - startTime)));
17.
18. startTime = System.currentTimeMillis();
19. // 检测代码
20. try {
21. PoiReadExcel er = new PoiReadExcel();
22. er.testPoiExcel(fileName);
23. } catch (Exception ex) {
24. Logger.getLogger(FastexcelReadExcel.class.getName()).log(Level.SEVERE, null, ex);
25. }
26. endTime = System.currentTimeMillis();
27.System.out.println("用时:" + sdf.format(new Date(endTime - startTime)));
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS");
28. TimeZone t = sdf.getTimeZone();
29. t.setRawOffset(0);
30. sdf.setTimeZone(t);
31. Long startTime = System.currentTimeMillis();
32. // 检测代码
33. String fileName = "F:我的文档专业实训07 信管缴费代码.xls";
34. try {
35. // 性能更好,读取更快
36. FastexcelReadExcel fre = new FastexcelReadExcel();
37. fre.testFastExcel(fileName);
38. } catch (Exception ex) {
39. Logger.getLogger(FastexcelReadExcel.class.getName()).log(Level.SE
VERE, null, ex);
40. }
41. Long endTime = System.currentTimeMillis();
42. System.out.println("用时:" + sdf.format(new Date(endTime -
startTime)));
43.
- 4. 44. startTime = System.currentTimeMillis();
45. // 检测代码
46. try {
47. PoiReadExcel er = new PoiReadExcel();
48. er.testPoiExcel(fileName);
49. } catch (Exception ex) {
50. Logger.getLogger(FastexcelReadExcel.class.getName()).log(Level.SE
VERE, null, ex);
51. }
52. endTime = System.currentTimeMillis();
53. System.out.println("用时:" + sdf.format(new Date(endTime -
startTime)));
fastexcel 读取用时:656ms,poi 读取用时:735ms。
Excel 表格 为 80 行 8 列。
总结:poi 是非常强大的,可以完成设置各种格式,但是相对的也可能更慢一些(猜测),fastexcel 则更适合读取数
据,不需要控制太多的格式,只是简单的读取。
两者的具体实现没有过深入的了解过。
对于读取 Excel 内容,导入数据库中,优先选用 fasteexcel。
对于要求更高的格式控制,优先选用 poi。
另外还有 JXL 等其他操作 Excel 的开源项目,没有使用过,感觉有以上两者已经足够。
对于 office2007 格式还没有研究,等待……