先上代码,刚开始用的几个包由于版本过高一直编译失败,后来换成了低版本的包运行通过。
public class Demo {
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\11399\\Desktop\\test.xlsx");
FileInputStream in=new FileInputStream(file);
XSSFWorkbook wb=new XSSFWorkbook(in);
Sheet sheets=wb.getSheetAt(0);
int firstrow=sheets.getFirstRowNum();//第一行
int lastrow=sheets.getLastRowNum();//最后一行
System.out.println("一共有"+lastrow+"行");
int columns=sheets.getRow(0).getPhysicalNumberOfCells();//判断有多少列数据
System.out.println("一共有"+columns+"列");
for (int i = 0; i < lastrow; i++) {
for (int j = 0; j < columns; j++) {
XSSFRow row1=((XSSFSheet) sheets).getRow(i);
XSSFCell cell1=row1.getCell(j);
cell1.setCellType(CellType.STRING);
System.out.print(cell1.getStringCellValue()+" ");
}
System.out.println();
}
}
}