getservletcontext獲取什么對象 springMVC怎么把結(jié)果集寫入Excel并導出?
springMVC怎么把結(jié)果集寫入Excel并導出?首先,我必須導入spring相關(guān)的包、poi和fileupload包,這些包是我用maven構(gòu)建的。一、導入excel(1)使用spring上傳文件
springMVC怎么把結(jié)果集寫入Excel并導出?
首先,我必須導入spring相關(guān)的包、poi和fileupload包,這些包是我用maven構(gòu)建的。
一、導入excel
(1)使用spring上傳文件
A.首頁提交
form namexcelimportform action $ { }/brand/importBrandSort方法post onsubmitreturn check import path();enctypemultipart/form-data idexcelImportForm
輸入類型隱藏名稱idids
差異
差異
標簽輸入idexcel_file類型文件名文件名acceptxls//label
差異
輸入id excel _ buttontype提交值導入Excel/
/div
/div
/div
差異
按鈕類型button data-dismissmodal onClickuncheckBoxes();取消/按鈕
/div
B.spring的控制器在后臺執(zhí)行相關(guān)操作。這里主要說一下使用spring上傳文件和讀取文件信息。
在使用spring上傳文件之前,您需要配置bean。
bean idmultipartResolver /(值/importBrandSort,方法)
public modeland view importBrandSort(@ request param(文件名)MultipartFile文件,
HttpServletRequest請求,HttpServletResponse響應(yīng))引發(fā)異常{
字符串溫度()。getServletCont:為單位。
(4096);//設(shè)置內(nèi)存中最多只能存儲的數(shù)據(jù),以:位為單位。
(temp);//設(shè)置一旦文件大小超過getSizeThreshold()的值,數(shù)據(jù)將存儲在硬盤上的目錄。
//開始讀取上傳的信息
//
int索引0;
/*列表文件項為空;
嘗試{
fileItems(請求);
}
捕捉(異常e) {
();
}
迭代器ITER();//依次處理每個上傳的文件。
文件項文件項為空;
while (iter.hasNext()) {
FileItem item(文件項)();//忽略不是文件字段的所有其他表單信息。
如果(!()) {
fileItem項目;
//索引;
}
}
if(文件項為空)
返回null
*/
如果(文件為空)
返回null
(());
字符串名稱();//獲取上傳文件名,包括路徑。
//name(()1);//從完整路徑中提取文件名
長尺寸();
if((name null | | name . equals())size 0)
返回null
()中的InputStream
品牌移動信息實體品牌移動信息服務(wù)
(在);
//更改為手動刷新緩存(新
//period dimensions());//清除所有緩存
int count();
String strAlertMsg
如果(算!0){
StrAlertMsg成功導入計數(shù)條目!;
}否則{
StrAlertMsg導入失?。。?/p>
}
(strAlertMsg);
(brandPeriodSortList,BrandMobileInfos);
(strAlertMsg,strAlertMsg);
().setAttribute(msg,strAlertMsg);
return get(請求,響應(yīng));
//返回null
}
代碼的注釋部分是如何得到它,如果你不 不要用彈簧。交上來的文件名(需要一些apache工具包)其實和用spring的一樣,只是被封裝了,方便我們寫代碼。
后半部分代碼是讀取上傳的文件信息并更新數(shù)據(jù)庫后輸出到前臺頁面的信息。
在上面的代碼中:InputStream in();
品牌移動信息實體品牌移動信息服務(wù)
(在);讀取excel的信息。
(2)用poi讀取excel
A.更新數(shù)據(jù)庫
r
public ListBrandMobil: brandMobileInfos){
mapper . updateby conditions(brandMobileInfo);
}
返回brandMobileInfos
}
這部分是服務(wù)層的代碼,用于讀取excel信息后更新數(shù)據(jù)庫數(shù)據(jù)。我在這里用mybatis。定義一個類BrandMobileInfoEntity,該類使用并保存excel表格的每一行的信息,而List BrandMobileInfoEntity保存所有的信息,并用這些信息更新數(shù)據(jù)庫。
B.讀取excel信息
private ListBrandMobileInfoEntity readBrandPeriodSorXls(InputStream is)
引發(fā)IOException,ParseException {
HSSFWorkbook hssfWorkbook新建hssf workbook(is);
ListBrandMobileInfoEntity brandMobileInfos new ArrayListBrandMobileInfoEntity();
BrandMobileInfoEntity brandMobileInfo;
//循環(huán)工作表工作表
for (int num sheet 0;
numSheet();numSheet ) {
hssf sheet hssf sheet(num sheet);
if (hssfSheet null) {
繼續(xù);
}
//循環(huán)行
for(int rowNum 1;rowNum();rowNum ) {
brandMobileInfo new BrandMobileInfoEntity();
HSS flow HSS flow(rowNum);
for(int I 0;I();i ) {
hssf cell brandIdHSSFCell(I);
如果(i 0) {
(整數(shù)
(getCellValue(brandIdHSSFCell));
} else if (i 1) {
繼續(xù);
} else if(I ^ 2){
((getCellValue(brandIdHSSFCell)));
} else if (i 3) {
((getCellValue(brandIdHSSFCell)));
} else if (i 4) {
(getCellValue(brandIdHSSFCell));
} else if (i 5) {
(getCellValue(brandIdHSSFC
contextparam元素的父元素是?
context-param元素包含一對參數(shù)名和參數(shù)值,它們用作應(yīng)用程序的ServletContext上下文的初始化參數(shù)。參數(shù)名稱在整個Web應(yīng)用程序中必須是唯一的。