首页  编辑  

读取Excel文件全部内容为List

Tags: /Java/   Date Created:
public static List<List<String>> getExcelAllRowData(String excelFile, String sheetName,
			int rowStart, int rowEnd,int colStart, int colEnd) {
	XSSFWorkbook workBook;
	Sheet sheet;
	List<List<String>> excelRowDatas;
	int getRowStart = 0;
	int getRowEnd = 0;
	int getColEnd = 0;
	int getColStart = 0;
	try {
		FileInputStream fis = new FileInputStream(new File(excelFile));
		workBook = new XSSFWorkbook(fis);
		sheet = workBook.getSheet(sheetName);
		if (rowStart >= 0)
			getRowStart = rowStart;
		getRowEnd = sheet.getPhysicalNumberOfRows();
		if (rowEnd >= 0)
			getRowEnd = rowEnd;
		if (rowStart >= 0)
			getColStart = rowStart;
		getColEnd = sheet.getRow(0).getLastCellNum();
		if (colEnd >= 0)
			getColEnd = colEnd;
	} catch (Exception e) {
		System.err.println("getExcelAllData: load file [" + excelFile + " - " + sheetName + "] failed, details:"+ e.toString());
		return null;
	}
	excelRowDatas = new ArrayList<>();
	List<String> excelColDatas = new ArrayList<>();
	for (int currentRow = getRowStart; currentRow <= getRowEnd; currentRow++) {
		Row row = sheet.getRow(currentRow);
		excelColDatas = new ArrayList<>();
		for (int currentColumn = getColStart; currentColumn < getColEnd; currentColumn++) {
			excelColDatas.add(getCellValue(sheet, row, currentColumn));
		}
		excelRowDatas.add(excelColDatas);
	}
	return excelRowDatas;
}