springboot+vue实现导出excel表
直接上手,先贴出后端代码:@RestController@RequestMapping("/api/excel")public class ExcelController {@Autowiredprivate CompanyService companyService;@RequestMapping(value = "/companyExcel",method = Request...
@RestController
@RequestMapping("/api/excel")
public class ExcelController {
@Autowired
private CompanyService companyService;
@RequestMapping(value = "/companyExcel",method = RequestMethod.POST)
@ResponseBody
public void exportCompanyExcel(Integer id, String contacts, String contactsPhone,HttpServletResponse response) {
try {
List<Company> companyList = companyService.selectByIdOrContactOrPhone(id,contacts,contactsPhone );
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("委托方信息列表");
HSSFRow row = null;
row = sheet.createRow(0);
row.setHeight((short) (30 * 20));
row.createCell(0).setCellValue("委托方信息列表");
CellRangeAddress rowRegion = new CellRangeAddress(0, 0, 0, 2);
sheet.addMergedRegion(rowRegion);
row = sheet.createRow(1);
row.setHeight((short) (22.50 * 20));//设置行高
row.createCell(0).setCellValue("Id");//为第一个单元格设值
row.createCell(1).setCellValue("logo");//为第二个单元格设值
row.createCell(2).setCellValue("委托方中文名称");//为第三个单元格设值
row.createCell(3).setCellValue("委托方英文名称");
row.createCell(4).setCellValue("法人代表");
row.createCell(5).setCellValue("联系电话");
row.createCell(6).setCellValue("企业地址");
row.createCell(7).setCellValue("详细地址");
row.createCell(8).setCellValue("一般纳税人");
row.createCell(9).setCellValue("邮箱");
row.createCell(10).setCellValue("网址");
row.createCell(11).setCellValue("备注");
row.createCell(12).setCellValue("经营范围");
row.createCell(13).setCellValue("状态");
row.createCell(14).setCellValue("审核状态");
row.createCell(14).setCellValue("审核备注");
for (int i = 0; i < companyList.size(); i++) {
row = sheet.createRow(i + 2);
Company company = companyList.get(i);
row.createCell(0).setCellValue(company.getId());
row.createCell(1).setCellValue(company.getLogo());
row.createCell(2).setCellValue(company.getNameCN());
row.createCell(3).setCellValue(company.getNameEN());
row.createCell(4).setCellValue(company.getContacts());
row.createCell(5).setCellValue(company.getContactsPhone());
row.createCell(6).setCellValue(company.getAddress());
row.createCell(7).setCellValue(company.getAddressDetail());
row.createCell(8).setCellValue(company.getCommonTaxpayer() == 1? "是" :"否");
row.createCell(9).setCellValue(company.getEmail());
row.createCell(10).setCellValue(company.getUrl());
row.createCell(11).setCellValue(company.getRemark());
row.createCell(12).setCellValue(company.getBusinessScopeIds());
row.createCell(13).setCellValue(company.getStatus() ==1? "启用":"停用");
row.createCell(14).setCellValue(company.getAuditStatus() ==1);
row.createCell(14).setCellValue(company.getAuditRemark());
}
sheet.setDefaultRowHeight((short) (16.5 * 20));
//列宽自适应
for (int i = 0; i <= 13; i++) {
sheet.autoSizeColumn(i);
}
String title= "company";
String fileName = new String(title.getBytes("GB2312"), "ISO_8859_1");
fileName = URLEncoder.encode(fileName,"utf-8");
response.setContentType("application/vnd.ms-excel;charset=utf-8");
OutputStream os = response.getOutputStream();
response.setHeader("Content-disposition", "attachment;filename="+fileName+".xls");//默认Excel名称
wb.write(os);
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
前端vue代码:
const excApiBase = ‘/api/excel’
import {exportCompanyExcel} from ‘@/api/axios’
// 导出Excel文件
export const exportClientInfoExcel = (params) => {
return exportCompanyExcel(${excApiBase}/companyExcel
,params)
}
import axios from ‘axios’;
/**
-
导出产品信息表
-
@param url
-
@param params
-
@returns {AxiosPromise}
*/
export const exportCompanyExcel = ( id,contacts,contactsPhone ) => {
return axios({
method: ‘post’,
url:/api/excel/companyExcel
,
params: {id,contacts,contactsPhone},
responseType: ‘blob’,
headers: {
}
});
};<el-form-item> <el-button type="primary" icon="el-icon-download" @click="exportClientInfoExcel">导出</el-button> </el-form-item>
//js代码
import {exportCompanyExcel} from ‘@/api/axios’
exportClientInfoExcel() {
const that = this
//that.filters.id,that.filters.contacts,that.filters.contactsPhone 通过id 联系人 联系电话 导出excel
exportCompanyExcel(that.filters.id,that.filters.contacts,that.filters.contactsPhone).then(response => {
that.downloadFile(response.data);
})
},
downloadFile(data) {
// 文件导出
if (!data) {
return
}
let url = window.URL.createObjectURL(new Blob([data]));
let link = document.createElement('a');
link.style.display = 'none';
link.href = url;
link.setAttribute('download', '委托方信息表.xls');
document.body.appendChild(link);
link.click()
} }}
更多推荐
所有评论(0)