如何实现控制台导入Excel

来源:文书网 2.81W

导语:如何实现控制台导入Excel呢?下面是小编给大家提供的代码,大家可以参考练习,更多详情请关注应届毕业生考试网。

如何实现控制台导入Excel

package util;

import ;

import InputStream;

import ception;

import tStream;

import yList;

import ;

import ;

import t;

import book;

import Exception;

public class ExcelReader {

/**

*

* @param excelFile 读取文件对象

* @param rowNum 从第几行开始读,如果有一行表头则从第二行开始读

* @return

* @throws BiffException

* @throws IOException

*/

public static List<String[]> readExcel(File excelFile,int rowNum) throws BiffException,

IOException {

// 创建一个list 用来存储读取的内容

List<String[]> list = new ArrayList<String[]>();

Workbook rwb = null;

Cell cell = null;

// 创建输入流

InputStream stream = new FileInputStream(excelFile);

// 获取Excel文件对象

rwb = orkbook(stream);

// 获取文件的指定工作表 默认的第一个

Sheet sheet = heet(0);

// 行数(表头的目录不需要,从1开始)

for (int i = rowNum-1; i < ows(); i++) {

// 创建一个数组 用来存储每一列的`值

String[] str = new String[olumns()];

// 列数

for (int j = 0; j < olumns(); j++) {

// 获取第i行,第j列的值

cell = ell(j, i);

str[j] = ontents();

}

// 把刚获取的列存入list

(str);

}

// 返回值集合

return list;

}

public static void main(String[] args) {

String excelFileName = "D:";

try {

List<String[]> list = Excel(new File(excelFileName),1);

for (int i = 0; i < (); i++) {

String[] str = (String[])(i);

for (int j = 0; j < th; j++) {

tln(str[j]);

}

}

} catch (BiffException e) {

tStackTrace();

} catch (IOException e) {

tStackTrace();

}

}

}

热门标签