Fastexcel

package com.file.monitor;

//用fastexcel读取xls文件
import java.io.File;

import edu.npu.fastexcel.FastExcel;
import edu.npu.fastexcel.Sheet;
import edu.npu.fastexcel.Workbook;

public class ReadExcel {

/**
* @param args
*/
public static void main(String[] args) {
readExcel("D:/mfqueue.xls");
}

// 用fastexcel读取xls文件
public static void readExcel(String strPath) {
// 打开表格文件,strPath设置文件所在路径
Workbook workBook = FastExcel.createReadableWorkbook(new File(strPath));
try {
workBook.open();
// 基于事件的读取
// workBook.getSheet(0, new SheetReadAdapter() {
// public void onCell(int row, int col, String content) {
// System.out.println(row + "," + col + "," + content);
// }
// });
System.out.println("xls文件包含的表数量: " + workBook.sheetCount());
for (String tableName : workBook.sheetNames()) {
System.out.println("所有表的名字: " + tableName);
}
// 基本的读取
// 操作第一张表格
// Sheet s = workBook.getSheet(0);
// 设置 row、cell
// String[] row;
String cell;
// 循环输出每张表的内容
for (int loop = 0; loop < workBook.sheetCount(); loop++) {
System.out.println("表:" + (loop + 1) + "  表名:" + workBook.sheetNames()[loop]);
Sheet sheet = workBook.getSheet(loop);
// 循环输出表格内容
for (int i = sheet.getFirstRow(); i < sheet.getLastRow(); i++) {
// row = sheet.getRow(i);
for (int j = sheet.getFirstColumn(); j < sheet.getLastColumn(); j++) {
cell = sheet.getCell(i, j);
if (null == cell) {
cell = "";
}
System.out.print(cell + "\t");
}
System.out.println("");
}
}
// 关闭连接
workBook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

你可能感兴趣的:(J#)