記錄一下讀取Excel表格裏面的內容xls版本的代碼,所使用的jar包以及讀取xlsx的jar包都在下面給出:
public static void main(String[] args) throws FileNotFoundException, IOException {
POIFSFileSystem pfs = new POIFSFileSystem(new FileInputStream("F:/test.xls"));
HSSFWorkbook hwb = new HSSFWorkbook(pfs);
HSSFSheet sheet = hwb.getSheetAt(0);// 獲取excel中的某張表,這裏獲取第一張
int rowNum = sheet.getLastRowNum();// 獲取最後一行索引的值
for (int i = 0; i <= rowNum; i++) {
HSSFRow row = sheet.getRow(i);// 獲取某一行
int cellNum = row.getLastCellNum();// 獲取某一行有多少列
Person person = new Person();// 聲明一個實例,用來存儲數據
for (int j = 0; j < cellNum; j++) {// 遍歷某行的所有列
HSSFCell cell = row.getCell(j);// 獲取某個單元格
cell.setCellType(CellType.STRING);// 設置單元格的格式
// 獲取單元格的數據
if (j == 0) {
person.number = cell.getStringCellValue();
} else if (j == 1) {
person.name = cell.getStringCellValue();
} else if (j == 2) {
person.name2 = cell.getStringCellValue();
} else if (j == 3) {
person.age = cell.getStringCellValue();
} else {
person.constellation = cell.getStringCellValue();
}
}
System.out.println(person.toString());
}
hwb.close();// 關閉數據流
}