How to read excel files in java using Apache POI

绝地灬酷狼 2024-02-18 20:59 166阅读 0赞

摘要:在这篇文章中,我们将看到如何使用Apache POI示例在java中读取excel。Apache POI项目的任务是创建和维护Java api,用于根据Office Open XML标准(OOXML)和微软的OLE 2复合文档格式(OLE2)来操作各种文件格式。简而言之,您可以使用Java读写MS Excel文件。

关于Apache POI的一些基本知识:

在java中读/写excel时,会遇到两个前缀

HSSF:用于处理excel 2003或更早(.xls)的文件。一些具有HSSF前缀的类是HSSFWorkbook、HSSFSheet、HSSFRow和HSSFCell。

XSSF:用于处理excel 2007或稍后(.xlsx)的文件。一些带有XSSF前缀的类是XSSFWorkbook、XSSFSheet、XSSFRow和XSSFCell。

这里有一些你需要注意的类:

Workbook :这是代表excel工作簿的高级类
Sheet :这是代表excel表格的高级类
Row : 这是代表excel行的高级类。它有与行相关的方法

Cell:这是代表单个excel单元的高级类。它有与cell相关的方法,例如:getDataType()。

项目结构:

20180607153800189

Dependency:

如果您正在使用maven,那么您需要在pomxml中添加以下相依性。

  1. <!-- poi -->
  2. <dependency>
  3. <groupId>org.apache.poi</groupId>
  4. <artifactId>poi</artifactId>
  5. <version>3.14</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.poi</groupId>
  9. <artifactId>poi-ooxml</artifactId>
  10. <version>3.14</version>
  11. </dependency>

使用poi读取excel文件:

Java Program:

我们要读countries.xlsx。它的内容是:

20180607153515160

创建ReadWriteExcelMain.java如下

  1. package com.micai.poi;
  2. import org.apache.poi.ss.usermodel.Cell;
  3. import org.apache.poi.ss.usermodel.Row;
  4. import org.apache.poi.ss.usermodel.Sheet;
  5. import org.apache.poi.ss.usermodel.Workbook;
  6. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.IOException;
  10. import java.util.Iterator;
  11. /**
  12. * 描述:
  13. * <p>
  14. *
  15. * @author: 赵新国
  16. * @date: 2018/6/7 14:53
  17. */
  18. public class ReadWriteExcelMain {
  19. public static void main(String [] args) throws IOException {
  20. readFileUsingPOI();
  21. }
  22. public static void readFileUsingPOI() throws IOException {
  23. ClassLoader classLoader = ReadWriteExcelMain.class.getClassLoader();
  24. String excelFilePath = "Countries.xlsx";
  25. FileInputStream inputStream = new FileInputStream(new File(classLoader.getResource(excelFilePath).getFile()));
  26. Workbook workbook = new XSSFWorkbook(inputStream);
  27. Sheet sheet = workbook.getSheetAt(0);
  28. Iterator<Row> iterator = sheet.iterator();
  29. while (iterator.hasNext()) {
  30. Row nextRow = iterator.next();
  31. Iterator<Cell> cellIterator = nextRow.cellIterator();
  32. while (cellIterator.hasNext()) {
  33. Cell cell = cellIterator.next();
  34. switch (cell.getCellType()) {
  35. case Cell.CELL_TYPE_STRING:
  36. System.out.print(cell.getStringCellValue());
  37. break;
  38. case Cell.CELL_TYPE_NUMERIC:
  39. System.out.print(cell.getNumericCellValue());
  40. break;
  41. case Cell.CELL_TYPE_BOOLEAN:
  42. System.out.print(cell.getBooleanCellValue());
  43. break;
  44. }
  45. System.out.print(" | ");
  46. }
  47. System.out.println();
  48. }
  49. workbook.close();
  50. inputStream.close();
  51. }
  52. }

当您运行在程序之上时,您将得到以下输出:

20180607153712572

让我们通过面向对象的方式实现读取excel文件:

我们将读取每一行并创建国家对象。显然我们会跳过标题行。

创建一个名为Country的类。java在包com.micai.poi

  1. package com.micai.poi;
  2. /**
  3. * 描述:
  4. * <p>
  5. *
  6. * @author: 赵新国
  7. * @date: 2018/6/7 15:17
  8. */
  9. public class Country {
  10. String name;
  11. String capital;
  12. double population;
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public String getCapital() {
  20. return capital;
  21. }
  22. public void setCapital(String capital) {
  23. this.capital = capital;
  24. }
  25. public double getPopulation() {
  26. return population;
  27. }
  28. public void setPopulation(double population) {
  29. this.population = population;
  30. }
  31. @Override
  32. public String toString() {
  33. return "Country{" +
  34. "name='" + name + '\'' +
  35. ", capital='" + capital + '\'' +
  36. ", population=" + population +
  37. '}';
  38. }
  39. }
  40. ReadWriteExcelWithCountryMain.java
  41. package com.micai.poi;
  42. import org.apache.poi.ss.usermodel.Cell;
  43. import org.apache.poi.ss.usermodel.Row;
  44. import org.apache.poi.ss.usermodel.Sheet;
  45. import org.apache.poi.ss.usermodel.Workbook;
  46. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  47. import org.omg.PortableInterceptor.INACTIVE;
  48. import java.io.File;
  49. import java.io.FileInputStream;
  50. import java.io.IOException;
  51. import java.util.ArrayList;
  52. import java.util.Iterator;
  53. import java.util.List;
  54. /**
  55. * 描述:
  56. * <p>
  57. *
  58. * @author: 赵新国
  59. * @date: 2018/6/7 15:17
  60. */
  61. public class ReadWriteExcelWithCountryMain {
  62. public static void main(String [] args) throws IOException {
  63. List<Country> countryList = readFileUsingPOI();
  64. for (Country country : countryList) {
  65. System.out.println(country.toString());
  66. }
  67. }
  68. public static List<Country> readFileUsingPOI() throws IOException {
  69. List<Country> countryList = new ArrayList<Country>();
  70. ClassLoader classLoader = ReadWriteExcelWithCountryMain.class.getClassLoader();
  71. String excelFilePath = "Countries.xlsx";
  72. FileInputStream inputStream = new FileInputStream(new File(classLoader.getResource(excelFilePath).getFile()));
  73. Workbook workbook = new XSSFWorkbook(inputStream);
  74. Sheet sheet = workbook.getSheetAt(0);
  75. Iterator<Row> iterator = sheet.iterator();
  76. while (iterator.hasNext()) {
  77. Row nextRow = iterator.next();
  78. // Not creating country object for header
  79. if (nextRow.getRowNum() == 0) {
  80. continue;
  81. }
  82. Country country = new Country();
  83. Iterator<Cell> cellIterator = nextRow.cellIterator();
  84. while (cellIterator.hasNext()) {
  85. Cell cell = cellIterator.next();
  86. int columnIndex = cell.getColumnIndex();
  87. switch (columnIndex+1) {
  88. case 1:
  89. country.setName(cell.getStringCellValue());
  90. break;
  91. case 2:
  92. country.setCapital(cell.getStringCellValue());
  93. break;
  94. case 3:
  95. country.setPopulation(cell.getNumericCellValue());
  96. break;
  97. }
  98. }
  99. countryList.add(country);
  100. }
  101. workbook.close();
  102. inputStream.close();
  103. return countryList;
  104. }
  105. }

当您运行在程序之上时,您将得到以下输出:

20180607154210172

发表评论

表情:
评论列表 (有 0 条评论,166人围观)

还没有评论,来说两句吧...

相关阅读