前台代码
<el-button type="primary" @click="exportExcel">下载<i class="el-icon-download el-icon--right" ></i></el-button>
exportExcel() {
// this.$refs.exportExcel.dispatchEvent(new MouseEvent('click'))
// console.log(this.$refs.exportExcel.dispatchEvent(new MouseEvent('click')))
downloadAuditValue(Object.assign({
province_code: this.tenant_id
})).then(res => {
console.log(res)
const blob = new Blob([res], { type: 'application/ms-excel;charset=utf-8' })
const downloadElement = document.createElement('a')
const href = window.URL.createObjectURL(blob) // 创建下载的链接
downloadElement.href = href
downloadElement.download = '稽核价值.xlsx' // 下载后文件名
document.body.appendChild(downloadElement)
downloadElement.click() // 点击下载
document.body.removeChild(downloadElement) // 下载完成移除元素
window.URL.revokeObjectURL(href) // 释放掉blob对象
})
}
后台代码
pom依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
controller
@RequestMapping("downloadExcel")
public void exprotAuditValue(@RequestBody Map<String, Object> params, HttpServletResponse response) {
try {
List<AuditValue> auditValues = AuditValueService.downloadExcel(params);
String[] titleRow = new String[]{"省份编码", "省份", "稽核指标ID", "稽核指标名称",
"样本量", "发现异常记录", "已整改记录", "整改中记录", "无需整改记录",
"差错条数", "差错金额", "差错率", "准确率"};
List<String[]> data = new LinkedList<String[]>();
data.add(0, titleRow);
for (AuditValue auditValue : auditValues) {
data.add(new String[]{
auditValue.getProvince_code(),
auditValue.getProvince_name(),
auditValue.getQuota_id(),
auditValue.getQuota_name(),
auditValue.getSample_num(),
String.valueOf(auditValue.getAbnormal_record()),
auditValue.getModified_record(),
auditValue.getModifying_record(),
auditValue.getNo_record(),
String.valueOf(auditValue.getError_num()),
auditValue.getError_fee(),
auditValue.getError_rate(),
auditValue.getAccuracy()
});
}
Map<String, List<String[]>> exprotData = new HashMap<>();
exprotData.put("稽核价值总视图", data);
String res = FileUtils.createExceleFile(response, exprotData);
} catch (Exception e) {
logger.error(" Errors caused by downloading : {} ", e.toString());
}
}
FileUtils
public class FileUtils {
private static Logger logger = LoggerFactory.getLogger(FileUtils.class);
public static String createExceleFile(HttpServletResponse response, Map<String, List<String[]>> exportData) {
OutputStream outputStream = null;
try {
Workbook wb = new HSSFWorkbook();
for (String sheetName : exportData.keySet()) {
Sheet sheet = wb.createSheet(sheetName);
List<String[]> rowData = exportData.get(sheetName);
for (int i = 0; i < rowData.size(); i++) {
String[] cellData = rowData.get(i);
Row row = sheet.createRow(i);
for (int j = 0; j < cellData.length; j++) {
Cell cell = row.createCell(j);
cell.setCellValue(cellData[j]);
}
}
}
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.flushBuffer();
outputStream = response.getOutputStream();
wb.write(outputStream);
} catch (Exception e) {
logger.error("FileUtils failure : {}", e.toString());
return "failure";
} finally {
try {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
} catch (Exception e) {
logger.error("FileUtils close xxxStream: {}", e.toString());
}
}
return "success";
}
}