導語:如何實現控制檯匯入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();
}
}
}