使用workbook读取Excel
1、导入jar包
jxl.jar
2、创建输入流(inputStream):读文件
3、得到工作薄,进行操作
4、关闭工作薄
传入存在的路径测试。代码如下
package com.cgsoft.kyxl.oldxm;
import java.io.FileInputStream;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class ExcelTest {
public static void main(String[] args) {
readExcel("e:/文件.xls");
}
public static void readExcel(String filePath){
FileInputStream inputStream =null;
Workbook wb = null;
try {
//创建输入流对象
inputStream=new FileInputStream(filePath);
wb=Workbook.getWorkbook(inputStream);//得到工作薄
Sheet sheet = wb.getSheet(0);//得到表格对象
//得到行和列
int rows=sheet.getRows();
int columns = sheet.getColumns();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
Cell cell = sheet.getCell(j,i);//得到单元格对象
System.out.print(cell.getContents()+"");//得到单元格内容并输出
}
System.out.println();
}
wb.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(inputStream != null){
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

浙公网安备 33010602011771号