欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java中通过POI读取Excel 2003 - 2007的xls,xlsx格式

程序员文章站 2022-06-03 17:44:24
...
	@Test
	public void testRead() throws IOException, InvalidFormatException {
		// String fileName = "e:/workbook.xls";
		String fileName = "e:/workbook.xlsx";
		Workbook wb = WorkbookFactory.create(new FileInputStream(fileName));

		int sheetNum = wb.getNumberOfSheets();

		for (int i = 0; i < sheetNum; i++) {
			Sheet childSheet = wb.getSheetAt(i);
			int rowNum = childSheet.getLastRowNum();

			for (int j = 0; j < rowNum; j++) {
				Row row = childSheet.getRow(j);
				int cellNum = row.getLastCellNum();

				for (int k = 0; k < cellNum; k++) {
					System.out.print(row.getCell(k).toString() + " ");
				}
				System.out.println();
			}

		}
	}