java读取Excel数据,代码如下:
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ReadExcel {
public static void main(String[] args) {
try {
readExcel();
} catch (BiffException | IOException e) {
e.printStackTrace();
}
}
private static void readExcel() throws BiffException, IOException {
File xlsFile = new File("jxl.xls");
// 获得工作簿对象
Workbook workbook = Workbook.getWorkbook(xlsFile);
// 获得所有工作表
Sheet[] sheets = workbook.getSheets();
// 遍历工作表
if (sheets != null) {
for (Sheet sheet : sheets) {
// 获得行数
int rows = sheet.getRows();
// 获得列数
int cols = sheet.getColumns();
// 读取数据
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
Cell cell = sheet.getCell(col, row);
System.out.print(cell.getContents() + " ");
}
System.out.println();
}
}
}
workbook.close();
}
}
运行结果:
所使用的jar包为jxl-2.6.12.jar、jxl-2.6.12-sources.jar,下载地址:https://download.csdn.net/download/qq_42017767/12662474