读取excel文件,首先呢需要去读取excel的表单数量,然后根据去读取表单里面的内容,并且输出到控制台
//文件地址 也可以放在main方法里面
public static String PATH = "D:\\test\\1667194328379.xlsx";
private static void ReadExcel(String url) throws Exception {
InputStream inputStream = new FileInputStream(url);
Workbook workbook = WorkbookFactory.create(inputStream);
int numberOfSheets = workbook.getNumberOfSheets();
//获取表单数量
System.out.println("numberOfSheets="+numberOfSheets);
Sheet sheet = workbook.getSheetAt(0);
int i1 = readExcelValueRows(sheet);
System.out.println(i1);
//获取sheet的行数
int rows = sheet.getPhysicalNumberOfRows();
System.out.println(rows);
for (int i = 0; i < rows; i++) {
//过滤表头行
if (i == 0) {
continue;
}
//获取当前行的数据
Row row = sheet.getRow(i);
if (row==null){
break;
}
int index = 0;
for (Cell cell : row) {
System.out.println(cell);
}
}
}
/**
* 用来得到真实行数
* @param sheet 需要读取的Excel表格(excel文件的工作簿的名称)
* @return
*
*/
public static int readExcelValueRows(Sheet sheet) {
int realRow = 0;// 返回的真实行数
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
//i从1开始,不判断第一行标题行
Row row = sheet.getRow(i);
if (row == null){
continue;
}
for (Cell cell : row) {
if (cell == null){
continue;
}
String value = cell.getStringCellValue();
if (value == null || "".equals(value)){
continue;
} else{
realRow++;
break;
}
}
}
return realRow;
}
public static void main(String[] args) throws Exception {
ReadExcel(PATH);
}
输出