簡易使用POI匯入匯出excel檔案

2021-07-15 12:43:28 字數 1851 閱讀 1745

首先需要匯入5個jar包

poi-3.7-20101029.jar

poi-examples-3.7-20101029.jar

poi-ooxml-3.7-20101029.jar

poi-ooxml-schemas-3.7-20101029.jar

poi-scratchpad-3.7-20101029.jar

在poi中, hssf開頭的類是對字尾為.xls 檔案的支援,也就是07之前的excel檔案

以 xssf開頭的類是對字尾為 .xlsx檔案的支援,也就是07之後包括07 的excel檔案,如果在執行過程中 發現了一些諸如版本不對 之類的錯誤,極有可能是沒有使用正確的類來匯入excel檔案,或者是 excel檔案的字尾被修改了 

檔案的匯入,如果有需求可以直接建立資料庫的dao業務,來匯入進資料庫  匯出同理

public void importstudent(string filepath) 

// 如果不為空,那麼用迴圈來檢視該頁面有多少行

// 注意,預設行是從0開始的,如果有表頭,則需要剔除

for (int rows = 1; rows <= sheet.getlastrownum(); rows++) }}

system.out.println(list);

for (student bean : list)

} catch (exception e) finally catch (ioexception e)

}} }

其實大部分時候可以全部強制轉化為字串,在對字串進行操作,excel預設不支援15位以上的數字,超過15位後 多出來的無論怎麼修改都是0,如身份證 

// 用來判斷**中具體項值的型別,最終把他們全部返回為字串

private string getvalue(xssfcell xssfcell) else

}

匯出

* @param filepath

* 儲存的檔案路徑

*/public static void exportmanager(string filepath)

// 建立行

row = sheet.createrow(j + 2);

// 得到要輸出的物件 因為有分頁,所有需要用頁數誠意每頁顯示條數,如果不需要分頁則可以去掉

int x = j + i * 30;

if (x >= list.size())

manager bean = list.get(x);

id = row.createcell(0);

id.setcellvalue(bean.getid());

name = row.createcell(1);

name.setcellvalue(bean.getusername());

pwd = row.createcell(2);

pwd.setcelltype(pwd.cell_type_string);

pwd.setcellvalue(bean.getuserpwd());

email = row.createcell(3);

email.setcellvalue(bean.getemail());

}} outputstream file = null;

try catch (exception e) finally catch (ioexception e)

}} }

目前乙個小專案中,使用的是sql server 2008 ,大概對1萬多條資料,13個字段進行匯入匯出,不分頁也就是30秒內。

POI匯入匯出

利用poi可以實現excel檔案匯入和匯出功能 本例子結合了springboot做demo pom檔案 1.8 org.springframework.boot spring boot starter 1.5.4.release org.springframework.boot spring boo...

poi匯入匯出

org.apache.poi groupid poi artifactid 4.1.2 dependency org.apache.poi groupid poi ooxml artifactid 4.1.2 version dependency 匯入設定 import 資料匯入 file是傳過來的...

JSONCPP 簡易使用

1 編譯jsoncpp mkdir usr jsoncpp cp r include usr jsoncpp cp r libs usr jsoncpp 2 jsoncpp簡單例項 1 反序列化json物件 比如乙個json物件的字串序列如下,其中 array 表示json物件中的陣列 那怎麼分別取...