在寫代碼之前需要先給項目引入包jxl.jar
test.xml
java讀取效果如下:
代碼如下:
1 public static void main(String[] args) { 2 readFilexls("E:\\test.xls"); 3 } 4 //xls文件 5 public static void readFilexls(String filePath) { 6 System.out.println("readFilexls獲取文件路徑:"+filePath); 7 File f = new File(filePath); 8 try { 9 Workbook book = Workbook.getWorkbook(f);// 10 Sheet sheet = book.getSheet(0); // 獲得第一個工作表對象 11 for (int i = 0; i < sheet.getRows(); i++) { 12 for (int j = 0; j < sheet.getColumns(); j++) { 13 Cell cell = sheet.getCell(j, i); // 獲得單元格 14 System.out.print(cell.getContents() + " "); 15 } 16 System.out.print("\n"); 17 } 18 } catch (BiffException e) { 19 // TODO Auto-generated catch block 20 e.printStackTrace(); 21 } catch (IOException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } 25 }