在写代码之前需要先给项目引入包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 }