Java 读取Excel文件

时间:2022-06-09 11:25:50


Java 读取Excel文件

操作步骤:

1.首先到网上载一下JExcelApi rar包,目前最新是:jexcelapi_2_6_12.tar,然后将其解压开,将 jxl.jar文件Copy到WEB-INF\lib目录下或直接导入到Java项目中
2.相应的操作代码如下:

package com.tengxincar.tengxincar.common;

import java.io.File;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;



public class ReadExcel {

public static void main(String[] args) {

try {
//excel文件所在路径
String fileName = "/Users/linshaofeng/Documents/document/123.xls";
//创建文件对象
File file = new File(fileName);

//从文件流中获取Excel工作区对象(WorkBook)
Workbook wb = Workbook.getWorkbook(file);

// 从工作区中取得页(Sheet)
Sheet sheet = wb.getSheet(0);

//得到sheet的总行数
int rows = sheet.getRows();

//得到sheet的总列数
int column = sheet.getColumns();

// 循环打印Excel表中的内容
for (int i = 0; i < rows; i++) {
for (int j = 0; j < column; j++) {
Cell cell = sheet.getCell(j, i);
System.out.println(cell.getContents());
}
}

} catch (Exception e) {
e.printStackTrace();
}
}
}