添加poi依赖
<!-- 表格导出--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency>
业务处理层:serviceImpl
@Override public void exportDataToEx(HttpServletResponse response) throws IOException { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("商品信息表"); List<TConstructorVo> allTConstructor = tConstructorMapper.getAllTConstructor(); // 设置要导出的文件的名字 SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss"); String fileName = "asd" + sf.format(new Date()) + ".xls"; // 新增数据行,并且设置单元格数据 int rowNum = 1; // headers表示excel表中第一行的表头 在excel表中添加表头 String[] headers = {"企业编号","企业名称","统一社会信用代码","类型", "法定代表人","法定代表人手机号码","法定代表人证件号码","传真号码", "座机号码","办公号码","邮箱地址","营业执照号","注册资本","单位性质", "省","市","县区","详细地址","联系人","联系人电话","企业网址","经营范围"}; HSSFRow row = sheet.createRow(0); for(int i=0;i<headers.length;i++){ HSSFCell cell = row.createCell(i); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } //在表中存放查询到的数据放入对应的列 for (TConstructorVo tConstructor : allTConstructor) { HSSFRow row1 = sheet.createRow(rowNum); row1.createCell(0).setCellValue(tConstructor.getNoid()); row1.createCell(1).setCellValue(tConstructor.getConstructorName()); row1.createCell(2).setCellValue(tConstructor.getConstructorCode()); row1.createCell(3).setCellValue(tConstructor.getTypeName()); row1.createCell(4).setCellValue(tConstructor.getFarenDaibiao()); row1.createCell(5).setCellValue(tConstructor.getFarenDaibiaoPhone()); row1.createCell(6).setCellValue(tConstructor.getFarenIdcare()); row1.createCell(7).setCellValue(tConstructor.getChuanzhenNum()); row1.createCell(8).setCellValue(tConstructor.getZuojiNum()); row1.createCell(9).setCellValue(tConstructor.getWorkPhone()); row1.createCell(10).setCellValue(tConstructor.getConstructorEmail()); row1.createCell(11).setCellValue(tConstructor.getYingyezhizhaohao()); row1.createCell(12).setCellValue(tConstructor.getRegisteredCapital()); row1.createCell(13).setCellValue(tConstructor.getNName()); row1.createCell(14).setCellValue(tConstructor.getPname()); row1.createCell(15).setCellValue(tConstructor.getCname()); row1.createCell(16).setCellValue(tConstructor.getCountyname()); row1.createCell(17).setCellValue(tConstructor.getAddress()); row1.createCell(18).setCellValue(tConstructor.getLianxiren()); row1.createCell(19).setCellValue(tConstructor.getTelphone()); row1.createCell(20).setCellValue(tConstructor.getQiyewangzhi()); row1.createCell(21).setCellValue(tConstructor.getJingyingfanwei()); rowNum++; } response.setContentType("application/octet-stream"); response.addHeader("Access-Control-Expose-Headers", "Content-Disposition"); response.setHeader("Content-disposition", "attachment;filename=" + fileName); response.flushBuffer(); OutputStream outputStream = response.getOutputStream(); workbook.write(response.getOutputStream()); outputStream.flush(); outputStream.close(); }
controller:
@RequestMapping("/downloadExcel") @ResponseBody public void downloadExcel(HttpServletResponse response){ try { tConstructorService.exportDataToEx(response); } catch (Exception e) { e.printStackTrace(); } }
前端页面:vue
downloadAllClassmate(){
axios({
method: 'post',
url:'/api/t_constructor/downloadExcel',
responseType: 'blob'
})
.then(res => {
const filename = decodeURI(res.headers['content-disposition'].split(';')[1].split('=')[1]) || '.xls'
const blob = new Blob([res.data], {
type: 'application/octet-stream'
})
let url = window.URL.createObjectURL(blob);
let link = document.createElement('a');
link.style.display = 'none';
link.href = url;
link.setAttribute('download', filename);
document.body.appendChild(link);
link.click()
}
)
}