需要jxl的包
//打开文件
Workbook book = Workbook.getWorkbook(new File(path)) ;
//取得第一个sheet
Sheet sheet = book.getSheet(0);
int rows = sheet.getRows();
//i和j是你要的行和列
Cell [] cell = sheet.getRow(i);
Cell cloumn_cell = sheet.getCell(j, i);
//str是你的内容
String str = cloumn_cell.getContents();
方法有点老了 不知道能不能帮到你
package rw_excel;
import static org.junit.Assert.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class test_poi {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Test
public void test() throws IOException {
// fail("Not yet implemented");
String file_dir = "test.xlsx";
Workbook book = null;
book = getExcelWorkbook(file_dir);
Sheet sheet = getSheetByNum(book,0);
int lastRowNum = sheet.getLastRowNum();
System.out.println("last number is "+ lastRowNum);
for(int i = 0 ; i <= lastRowNum ; i++){
Row row = null;
row = sheet.getRow(i);
if( row != null ){
System.out.println("reading line is " + i);
int lastCellNum = row.getLastCellNum();
System.out.println("lastCellNum is " + lastCellNum );
Cell cell = null;
for( int j = 0 ; j <= lastCellNum ; j++ ){
cell = row.getCell(j);
if( cell != null ){
String cellValue = cell.getStringCellValue();
System.out.println("cell value is \n" + cellValue);
}
}
}
}
}
public static Sheet getSheetByNum(Workbook book,int number){
Sheet sheet = null;
try {
sheet = book.getSheetAt(number);
// if(sheet == null){
// sheet = book.createSheet("Sheet"+number);
// }
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return sheet;
}
public static Workbook getExcelWorkbook(String filePath) throws IOException{
Workbook book = null;
File file = null;
FileInputStream fis = null;
try {
file = new File(filePath);
if(!file.exists()){
throw new RuntimeException("文件不存在");
}else{
fis = new FileInputStream(file);
book = WorkbookFactory.create(fis);
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
} finally {
if(fis != null){
fis.close();
}
}
return book;
}
//
}
高版本的jxl好像是可以的 老版本的jar包只支持2003
解析Excel还可以用Apache POI ,这个支持新老版本的Excel,非常好用。
其实这个很简单的啊,jxl同样可以读取2007的,
可以apache 的poi框架,他的官网上有api,操作还挺简单的