這里是在springboot項(xiàng)目下使用EasyExcel實(shí)現(xiàn)模板下載、數(shù)據(jù)導(dǎo)入功能。 順便記錄下自己遇到的一些坑。
一.模板下載
1.在你的工程下添加模板文件
2.編寫代碼實(shí)現(xiàn)下載功能
controller
//下載模板
@PostMapping(value="/downloadWorkplacedetail")
public void downloadWorkplacedetail(HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException {
jobManageService.downloadWorkplacedetail(response);
}
serviceImpl
public void downloadWorkplacedetail(HttpServletResponse response) {
//方法一:直接下載路徑下的文件模板(這種方式貌似在SpringCloud和Springboot中,打包成JAR包時,無法讀取到指定路徑下面的文件,不知道記錯沒,你們可以自己嘗試下?。。。?/span>
try {
//文件名稱
String fileName = "jobWorkplace.xlsx";
//設(shè)置要下載的文件的名稱
setExcelRespProp(response,"jobWorkplace");
InputStream input = JobManageServiceImpl.class.getClassLoader().getResourceAsStream("/excel/" + fileName);
OutputStream out = response.getOutputStream();
byte[] b = new byte[2048];
int len;
while ((len = input.read(b)) != -1) {
out.write(b, 0, len);
}
input.close();
if (out != null) {
out.flush();
out.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
好了,到這里,一個簡單的下載模板功能就實(shí)現(xiàn)了。不過我在項(xiàng)目運(yùn)行中遇到了一些坑,下面記錄一下。
3.項(xiàng)目中遇到的坑:excel文件在springboot的maven項(xiàng)目下打了jar包后損壞
4.解決辦法:試了很多種,主要有三種方式,首推薦第一種
(1)該問題主要是maven打包時過濾導(dǎo)致該文件損壞,所以解決方案就是去掉過濾即可,在pom.xml文件中加入如下配置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions><!--不加這一行,xlsx文件會被過濾,然后在maven build的時候,去target下看對應(yīng)的xlsx就是損壞的-->
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension></nonFilteredFileExtensions>
</configuration>
</plugin>
</plugins>
</build>
(2).直接關(guān)閉所有過濾,resource–filtering直接設(shè)置為false:不過這樣做有可能讀取不到其他配置而出現(xiàn)錯誤
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>false</filtering>
</resource>
</resources>
(3).如果前兩種都不怎么起效果的話…那就只能用比較笨以及麻煩的法子了:
在install命令打了jar包后,打開文件發(fā)現(xiàn)是損壞的,那么把jar包復(fù)制出來,用解壓器打開,在文件目標(biāo)位置替換一個好的進(jìn)去。。。這樣雖然也可以,但需要每次打jar包時都記得手動維護(hù),所以并不推薦
二.模板導(dǎo)入
好了,接下來講簡單實(shí)現(xiàn)模板導(dǎo)入功能
1.添加EasyExcel依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.1</version>
</dependency>
2.實(shí)體類
public class WorkplacedetailEntity implements Serializable {
/**
* 主鍵
*/
@ExcelIgnore
private Long id;
/**
* 姓名
*/
@ExcelProperty("姓名")
@ColumnWidth(20)
private String name;
/**
* 電話
*/
@ExcelProperty("電話")
@ColumnWidth(20)
private String phone;
}
3.controller
//導(dǎo)入職場明細(xì)
@RequestMapping(value="/importWorkplacedetail")
public ResponseData importWorkplacedetail(MultipartHttpServletRequest request) throws Exception {
jobManageService.importWorkplacedetail(request);
return this.successResonse(null);
}
4.serviceImpl文章來源:http://www.zghlxwxcb.cn/news/detail-641202.html
public void importWorkplacedetail(MultipartHttpServletRequest request) throws IOException {
MultipartFile file = request.getFile("file");
System.out.println("導(dǎo)入職場明細(xì)功能參數(shù)file="+file);
String unicomcode = request.getParameter("unicomcode");
System.out.println("導(dǎo)入職場明細(xì)功能參數(shù)unicomcode="+unicomcode);
DManagecomEntity query = new DManagecomEntity();
query.setUnicomcode(unicomcode);
Map<String, Object> data = this.queryDManagecom(query);
List<DManagecomEntity> dManagecomEntityList = (List<DManagecomEntity>) data.get("list");
//excel讀取數(shù)據(jù)
List<WorkplacedetailEntity> workplacedetailEntityList = EasyExcel.read(new BufferedInputStream(file.getInputStream())).head(WorkplacedetailEntity.class).sheet().doReadSync();
System.out.println("解析到workplacedetailEntityList--------------------------------------------------------");
System.out.println("size="+workplacedetailEntityList.size());
System.out.println("workplacedetailEntityList="+workplacedetailEntityList);
workplacedetailEntityList.remove(0);
workplacedetailEntityList.stream().forEach(workplacedetailEntity -> {
workplacedetailEntity.setStatus("0");
if(null != dManagecomEntityList && dManagecomEntityList.size()>0){
workplacedetailEntity.setSecondarycomcode(dManagecomEntityList.get(0).getProvincecomcode());
workplacedetailEntity.setSecondarycomname(dManagecomEntityList.get(0).getProvincecomname());
workplacedetailEntity.setTertiarycomcode(dManagecomEntityList.get(0).getBranchcode());
workplacedetailEntity.setTertiarycomname(dManagecomEntityList.get(0).getBranchname());
}
this.insertOrUpdateWorkplacedetail(workplacedetailEntity);
});
}
好了,到這里就完成了。注意,本次是實(shí)現(xiàn)了后臺接入字段名為file的file類型的參數(shù)和字段名為unicomcode的字符串參數(shù)。 如果僅僅需要一個file的話,那直接用file參數(shù)來接即可。文章來源地址http://www.zghlxwxcb.cn/news/detail-641202.html
到了這里,關(guān)于使用EasyExcel實(shí)現(xiàn)模板下載、數(shù)據(jù)導(dǎo)入功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!