import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ReadExcle {
public static void main(String[] args) throws Exception{
File f=new File("F:\\excle文件名.xls");
readExcel(f);
}
public static void readExcel(File file)throws Exception {
Workbook wb = null;
try {
// 构造Workbook(工作薄)对象
wb = Workbook.getWorkbook(file);
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (wb == null)
return ;
// 获得了Workbook对象之后,就可以通过它得到Sheet(工作表)对象了
Sheet[] sheet = wb.getSheets();
Sheet sheet1 = sheet[0];// 第一页
int a=0;
// 对第一个工作表进行循环
for (int i = 1; i < sheet1.getRows(); i++) {// 得到当前工作表的行数
int rowNum = sheet1.getRows();
Cell[] cells = sheet1.getRow(i);
if (cells != null && cells.length > 0) {
String c_num= cells[1].getContents()==null?"": cells[1].getContents();
System.out.println(c_num);
}
}
}
}
本文提供了一个使用Java读取Excel文件的示例程序。该程序利用jxl库来打开并读取Excel表格中的数据。具体实现了从指定路径加载Excel文件,并遍历第一张工作表的所有行和列,获取单元格内容。
1206

被折叠的 条评论
为什么被折叠?



