一、该方法发现只能读取xls格式的Excel,不能读取xlsx格式的Excel;
首先导入jxl.jar包
package com;
import java.io.File;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class ReadExcel {
public static void main(String[] args) {
readExcels();
}
public static void readExcels(){
try {
Workbook book = Workbook
.getWorkbook(new File("C:/Users/物业.xls"));
book.getNumberOfSheets();
// 获得第一个工作表对象
Sheet sheet = book.getSheet(0);
int Rows = sheet.getRows();
int Cols = sheet.getColumns();
System.out.println("当前工作表的名字:" + sheet.getName());
System.out.println("总行数:" + Rows);
System.out.println("总列数:" + Cols);
String str = "";
for (int i = 0; i < Cols; ++i) {
for (int j = 1; j < Rows; ++j) {
str += (sheet.getCell(i, j)).getContents();
// getCell(Col,Row)获得单元格的值
System.out.print((sheet.getCell(i, j)).getContents() + "\t");
}
System.out.print("\n");
}
// 得到第一列第一行的单元格
Cell cell1 = sheet.getCell(0, 0);
String result = cell1.getContents();
book.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}