利用poi可以實現excel檔案匯入和匯出功能 本例子結合了springboot做demo
pom檔案
1.8乙個demo的controller類org.springframework.boot
spring-boot-starter
1.5.4.release
org.springframework.boot
spring-boot-starter-freemarker
1.5.4.release
org.apache.poi
poi3.16
org.apache.poi
poi-ooxml
3.16
org.apache.poi
poi-ooxml-schemas
3.16
@restcontrollerpublic class democontroller
if (file.getsize() > 5 * 1024 * 1024)
filetypeutil.filetype filetype = filetypeutil.getfiletype(file.getinputstream());
//xls_doc --》2007之前的excel型別 xlsx_docx--》 2007之後的excel型別
if (filetype == filetypeutil.filetype.xls_doc)
logger.info("資料解析成功");
file.getinputstream().close();
return getdata;
} else if (filetype == filetypeutil.filetype.xlsx_docx)
logger.info("資料解析成功");
//測試資料
listusers = new arraylist();
for (int i = 0; i < 10; i++)
//獲取模板的工作薄
xssfworkbook workbook = new xssfworkbook(inputstream);
xssfsheet sheetat = workbook.getsheetat(0);
xssfrow row = sheetat.getrow(0);
short rowheight = row.getheight();
xssfrow row1 = null;
xssfcell cell = null;
for (int j = 0; j < users.size(); j++)
string filename = new string("檔案".getbytes("utf-8"),"iso8859-1");
logger.info("檔名"+filename);
response.setheader("content-disposition", "attachment;filename="+filename+".xlsx");
workbook.write(response.getoutputstream());
response.getoutputstream().flush();
response.getoutputstream().close();
workbook.close();
}//處理2007之前的excel
private listreadoldexcel(hssfworkbook hssfworkbook)
object objects = new object[row.getlastcellnum()];
for (int j = row.getfirstcellnum(); j < row.getlastcellnum(); j++)
}//處理資料
if (objects != null)
}return users;
}//處理2007之後的excel
private listreadexcel(xssfworkbook xssfworkbook)
object objects = new object[row.getlastcellnum()];
for (int j = row.getfirstcellnum(); j < row.getlastcellnum(); j++)
//第一行資料
switch (cell.getcelltypeenum())
}//處理資料
if (objects != null)
}return users;
}//處理excel中的 2007之後 ---》獲取 string裡面儲存著 這個的座標
private mapgetexcelpic(xssfworkbook xssfworkbook, xssfsheet xssfsheet) }}
}return map;
}//處理excel中的 2007之前
private mapgetoldexcelpic(hssfworkbook hssfworkbook, hssfsheet hssfsheet)
}return map;
} else
}}
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是傳過來的...
poi匯入匯出excel
org.apache.poigroupid poiartifactid 3.14 beta1version dependency public private string headtextname 列頭 標題 名 private string propertyname 對應欄位名 private ...
POI的匯入匯出
匯入測試 匯入的模板一定要按照格式準備,固定每列的內容 created by hp on 2018 9 18.public class poiimport 匯出測試 created by hp on 2018 9 18.public class poiexport poi匯入用的是jquery的oc...