第一步:[引入jar包] [ 版本選擇 ]
org.apache.poi
poi3.17
org.apache.poi
poi-ooxml
3.17
第二步:[ 工具類 ]
第三步:[ controller層 ]
}通過以上的三步走,基本的匯出功能實現。
但實際中可能不會這麼簡單,也會有很多負責的業務邏輯。
例如:第①步: [ 更改工具類 ]
// 輸出流
outputstream out = response.getoutputstream();
// 建立工作簿物件
hssfworkbook workbook = new hssfworkbook();
// sheet樣式定義【getcolumntopstyle()/getstyle()均為自定義方法 - 在下面 - 可擴充套件】
hssfcellstyle columntopstyle = this.getcolumntopstyle(workbook);// 獲取列頭樣式物件
hssfcellstyle style = this.getstyle(workbook); // 單元格樣式物件
int rowscount = datalist.size();// 總的資料條數
int sheetmaxrowcount = 50000;// 每個sheet的最大資料條數
int sheetindex = rowscount / sheetmaxrowcount;
// 遍歷每個sheet,並賦值
for (int a = 0; a <= sheetindex; a++)
// 每個sheet的初始行號
int sheetrownum = a * sheetmaxrowcount;
// 最後乙個sheet的資料條數
int lastrowcount = rowscount % sheetmaxrowcount;
if (a == sheetindex)
// 將查詢出的資料設定到sheet對應的單元格中
for (int i = 0; i < sheetmaxrowcount; i++) else else
//}cell.setcellstyle(style); // 設定單元格樣式}}
}workbook.write(out);
out.close();
}第②步:[ controller層 ]
}實現的效果如下:
總結:poi不僅能做匯出,也可以做匯入。poi的這樣寫法優點在於可以不使用模板,處理匯出。
更多請看下篇博文...
poi百萬級資料匯出excel
通過傳入實體類陣列和指定匯出列來即可,匯出excel,讀者可直接複製到專案直接使用,下面只是個簡單的示例提供參考 一 匯出excel工具類 匯出實體類 param head 表頭 param exportcolumn 匯出字段 param exportlist 實體陣列 param clazz 實體...
POI匯出Excel檔案
workbook workbook exportservice.exportfile request,student outputstream out null try catch ioexception e finally catch exception e 實現類 設定各列寬度 short st...
利用poi匯出excel
最近接了乙個任務,要求把資料庫內容提取成excel,乙個excel動態建立多個sheet,乙個sheet裡面顯示一條資訊,然後再根據此條資訊的結果,再去子表獲取結果,如一條學生資訊,對應顯示該學生的所有詳細資訊。如圖所示 工程中匯入 public static void writeexcelnew ...