easyexcel使用教程
时间: 2023-10-20 13:36:38 浏览: 118
EXCEL使用教程
EasyExcel是一个方便导出Excel的Java库,可以在后端代码中引入对应的依赖。你可以使用以下代码来获取response对象,并将Excel内容写入response中进行下载:
```java
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("ExcelName", "UTF-8").replaceAll("\\ ", " ");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
response.setHeader("fileName", fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), HotelReportDto.class).sheet("sheetName").doWrite(data);
```
在前端,你可以使用以下代码来设置接收类型并进行下载:
```javascript
download() {
var params = {}
downloadReportByParams(params).then(res => {
let blob = new Blob([res], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
const $link = document.createElement("a");
$link.href = URL.createObjectURL(blob);
$link.download = '数据模板.xlsx'
$link.click();
document.body.appendChild($link);
document.body.removeChild($link);
window.URL.revokeObjectURL($link.href);
})
},
```
这样,你就可以使用EasyExcel在前后端进行Excel文件的导入和导出了。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
阅读全文