hutool工具
hutool工具
1. pom依賴
<!-- hutool依賴-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.20</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
瀏覽器格式
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
String fileName = URLEncoder.encode("用戶信息", "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
2. 導(dǎo)出接口
/**
* 導(dǎo)出接口
*/
@GetMapping("/export")
public void export(HttpServletResponse response) throws Exception {
// 從數(shù)據(jù)庫查詢出所有的數(shù)據(jù)
List<User> list = userService.list();
// 通過工具類創(chuàng)建writer 寫出到磁盤路徑
// ExcelWriter writer = ExcelUtil.getWriter(filesUploadPath + "/用戶信息.xlsx");
// 在內(nèi)存操作,寫出到瀏覽器
ExcelWriter writer = ExcelUtil.getWriter(true);
//自定義標(biāo)題別名
writer.addHeaderAlias("username", "用戶名");
writer.addHeaderAlias("password", "密碼");
writer.addHeaderAlias("nickname", "昵稱");
writer.addHeaderAlias("email", "郵箱");
writer.addHeaderAlias("phone", "電話");
writer.addHeaderAlias("address", "地址");
writer.addHeaderAlias("createTime", "創(chuàng)建時(shí)間");
writer.addHeaderAlias("avatarUrl", "頭像");
// 一次性寫出list內(nèi)的對象到excel,使用默認(rèn)樣式,強(qiáng)制輸出標(biāo)題
writer.write(list, true);
// 設(shè)置瀏覽器響應(yīng)的格式
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
String fileName = URLEncoder.encode("用戶信息", "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
ServletOutputStream out = response.getOutputStream();
writer.flush(out, true);
out.close();
writer.close();
}
打開瀏覽器能下載Excel即成功。文章來源:http://www.zghlxwxcb.cn/news/detail-840877.html
3. 導(dǎo)入接口
/**
* excel 導(dǎo)入
* @param file
* @throws Exception
*/
@PostMapping("/import")
public Boolean imp(MultipartFile file) throws Exception {
InputStream inputStream = file.getInputStream();
ExcelReader reader = ExcelUtil.getReader(inputStream);
// 方式1:(推薦) 通過 javabean的方式讀取Excel內(nèi)的對象,但是要求表頭必須是英文,跟javabean的屬性要對應(yīng)起來
// List<User> list = reader.readAll(User.class);
// 方式2:忽略表頭的中文,直接讀取表的內(nèi)容
List<List<Object>> list = reader.read(1);
List<User> users = CollUtil.newArrayList();
for (List<Object> row : list) {
User user = new User();
user.setUsername(row.get(0).toString());
user.setPassword(row.get(1).toString());
user.setNickname(row.get(2).toString());
user.setEmail(row.get(3).toString());
user.setPhone(row.get(4).toString());
user.setAddress(row.get(5).toString());
user.setAvatarUrl(row.get(6).toString());
users.add(user);
}
userService.saveBatch(users);
return true;
}
3.1 測試:
通過postman測試,send之后返回true測試成功。
在Navicat刷新,可以看到新寫入的數(shù)據(jù)。文章來源地址http://www.zghlxwxcb.cn/news/detail-840877.html
4. Vue導(dǎo)入
<el-upload action="http://localhost:9090/user/import" :show-file-list="false" accept="xlsx" :on-success="handleExcelImportSuccess" style="display: inline-block">
<el-button type="primary" class="ml-5">導(dǎo)入 <i class="el-icon-bottom"></i></el-button>
</el-upload>
handleExcelImportSuccess() {
this.$message.success("導(dǎo)入成功")
this.load()
}
5. Vue導(dǎo)出
<el-button type="primary" @click="exp" class="ml-5">導(dǎo)出 <i class="el-icon-top"></i></el-button>
exp() {
window.open("http://localhost:9090/user/export")
}
到了這里,關(guān)于SpringBoot和Vue實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!