利用Java Excel API ,下载地址:jexcelapi.rar
下面给出一段读取数据的例子代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package excel;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
/**
*
* @author Wei.Liu
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
InputStream is = new FileInputStream("d:\\test.xls");
jxl.Workbook rwb = Workbook.getWorkbook(is);
Sheet rs = rwb.getSheet(0);
//取第一行,第一列的元素
Cell c00 = rs.getCell(0, 0);
String strc00 = c00.getContents();
//取第一行,第二列的元素
Cell c10 = rs.getCell(1,0);
String strc10= c10.getContents();
System.out.println(strc00+" "+c00.getType().toString());
System.out.println(strc10+" "+c10.getType().toString());
//获取sheet的个数
System.out.println(rwb.getNumberOfSheets());
Sheet [] sheets =rwb.getSheets();
for(int i=0;i
System.out.println(rwb.getSheet(i).getName());
}
//获得列数
System.out.println(rwb.getSheet(0).getColumns());
//获得每列的元素
Cell [] cell = rwb.getSheet(0).getColumn(0);
//获得总行数
System.out.println(rwb.getSheet(0).getRows());
//获得行元素
Cell [] cell2 = rwb.getSheet(0).getRow(0);
//关闭对象
rwb.close();
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}