//exportExcelRequest.js
import axios from 'axios'
import { getToken } from '@/utils/auth'
const instance = axios.create({
baseURL: process.env.VUE_APP_BASE_API,
timeout: 10000
})
instance.interceptors.request.use((config) => {
config.headers['XXXXtoken'] = getToken()
return config
}), (err) => {
return Promise.reject(err)
}
const post = (url, data = {}) => {
return new Promise((resolve, reject) => {
instance.post(url, data, {
headers: {
'Content-Type': 'application/json'
},
responseType: 'arraybuffer'
}).then(response => {
if(response) {
resolve(response)
}
}), err => {
reject(err)
}
})
}
export {
post
}
import { post } from '@/utils/exportExcelRequest'
export const exportExcel = {
data: function() {
return {
};
},
methods: {
async exportExcel (data) {
const result = await post('/api/area-potential/potentialExportExcel', data)
console.log(result)
const blob = new Blob([result.data],{'content-type': "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
const url = window.URL.createObjectURL(blob);
const downloadLink = document.createElement('a');
downloadLink.href = url;
downloadLink.download = '列表.xlsx';
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
setTimeout(() => URL.revokeObjectURL(url), 100);
}
}
}
@Authorized
@PostMapping(value = "/potentialExportExcel")
public void potentialExportExcel(@RequestBody PageLandPotentialConditionDTO condition,HttpServletResponse response) throws Exception {
Sort sort = Sort.unsorted();
Page<VLandInfoListEntity> result = areaPotentialService.pageLandPotential(
PageRequest.of(0, Integer.MAX_VALUE, sort),
condition
);
try {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("测试", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), VLandInfoListEntity.class).sheet("模板").doWrite(result.getContent());
} catch ( Exception e) {
log.error("导出数据失败!错误信息为:{}", e.getMessage());
e.printStackTrace();
}
}
// 如果要转换导出的格式(比如日期)
// 定义一个类重写Converter接口的格式
public class ExportDateFormat implements Converter<Date> {
private static final String PATTERN_YYYY_MM_DD = "yyyy-MM-dd";
@Override
public Class<Date> supportJavaTypeKey() {
return Date.class;
}
@Override
public WriteCellData<String> convertToExcelData(WriteConverterContext<Date> context) throws Exception {
Date date = context.getValue();
if (date == null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_YYYY_MM_DD);
return new WriteCellData<>(sdf.format(date));
}
}
// 使用的时候,ExcelProperty指定自定义类
@ColumnWidth(30)
@ExcelProperty(value="合同开始日期",converter = ExportDateFormat.class)
private Date contractStDate;