maven依赖:
<dependency> <groupId>org.apache.xmlbeans</groupId> <artifactId>xmlbeans</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.9</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.xml.stream/stax-api --> <dependency> <groupId>javax.xml.stream</groupId> <artifactId>stax-api</artifactId> <version>1.0-2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-examples --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-examples</artifactId> <version>3.8</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-excelant --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-excelant</artifactId> <version>3.8-beta1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.8-beta4</version> </dependency>
代码:
public class Try2 { public static void getXlsxExcelData(File file) { InputStream is; try { is = new FileInputStream(file); XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is); for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) { XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet); if (xssfSheet == null) { continue; } // 获取当前工作薄的每一行 for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) { XSSFRow xssfRow = xssfSheet.getRow(rowNum); if (xssfRow != null) { //这里需要注意 虽然是第一列数据 但是用这个输出的话得不到表格的数据而是返回 1.0 2.0 这样是数字 XSSFCell one = xssfRow.getCell(0); System.out.println("第"+rowNum+"行"+"第1列"+xssfRow.getCell(0));//这样才能输出表格内的内容 //第二列数据 XSSFCell two = xssfRow.getCell(1); System.out.println("第"+rowNum+"行"+"第2列"+xssfRow.getCell(1)); //第三列数据 XSSFCell three = xssfRow.getCell(2); System.out.println("第"+rowNum+"行"+"第3列"+xssfRow.getCell(2)); } } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void getXlsExcelData(File file) { InputStream is; try { is = new FileInputStream(file); HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) { HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); if (hssfSheet == null) { continue; } // 获取当前工作薄的每一行 for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { HSSFRow hssfRow = hssfSheet.getRow(rowNum); if (hssfRow != null) { //第一列数据 HSSFCell one = hssfRow.getCell(0); System.out.println("第"+rowNum+"行"+"第1列"+hssfRow.getCell(0)); //第二列数据 HSSFCell two = hssfRow.getCell(1); System.out.println("第"+rowNum+"行"+"第2列"+hssfRow.getCell(1)); //第三列数据 HSSFCell three = hssfRow.getCell(2); System.out.println("第"+rowNum+"行"+"第3列"+hssfRow.getCell(2)); } } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String args[]) { Try2.getXlsxExcelData(new File("D:\\test.xlsx")); // Try2.getXlsExcelData(new File("D:\\test.xls")); } }
注:一定要注意依赖的版本,网上有很多实例,但是大多因为没有对上依赖版本或jar包不全,会导致异常