一、導入 jxl-2.6.12.jar
二、直接看代碼
package com.excel.mysj;
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class TestExcelJXL {
public static void main(String args[]) {
try {
String fileName = "f:\\data.xls"; // Excel檔案所在路徑
File file = new File(fileName); // 建立檔案對象
Workbook wb = Workbook.getWorkbook(file); // 從檔案流中擷取Excel工作區對象(WorkBook)
Sheet sheet = wb.getSheet(0); // 從工作區中取得頁(Sheet)
for (int i = 0; i < sheet.getRows(); i++) { // 循環列印Excel表中的内容
Cell cell = sheet.getCell(0, i);
Cell cell2 = sheet.getCell(1, i);
String name = cell.getContents();
String idcard = cell2.getContents();
System.out.println(name);
System.out.println(idcard);
}
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}