读取execl文件的方法很多,现在介绍一种最简单的方法,用jxl来读取,下面是测试代码:
package File;
import java.io.File;
import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
public class ExeclReader
{
public static void main(String[] args)
{
Workbook workbook = null;
Sheet sheet =null;
Cell cell = null;
try {
workbook = Workbook.getWorkbook(new File("D:/Backup/我的文档/12.xls"));
} catch (Exception e) {
try {
throw new Exception("file to import not found!");
} catch (Exception e1) {
e1.printStackTrace();
}
}
sheet= workbook.getSheet(0);
int columnCount=sheet.getColumns();
int rowCount=sheet.getRows();
for (int i = 0; i <rowCount; i++) {
for (int j = 0; j <columnCount; j++) {//注意,这里的两个参数,第一个是表示列的,第二才表示行
cell=sheet.getCell(j, i); //要根据单元格的类型分别做处理,否则格式化过的内容可能会不正确
if(cell.getType()==CellType.NUMBER){
System.out.print(((NumberCell)cell).getValue());
}
else if(cell.getType()==CellType.DATE){
System.out.print(((DateCell)cell).getDate());
}
else{
System.out.print(cell.getContents());
}
//System.out.print(cell.getContents());
System.out.print("/t");
}
System.out.print("/n");
}
//关闭它,否则会有内存泄露
workbook.close();
}
}