Java根據(jù)模板實(shí)現(xiàn)excel導(dǎo)出標(biāo)準(zhǔn)化
雖然標(biāo)題叫標(biāo)準(zhǔn)化,只不過是我自己的習(xí)慣,當(dāng)一件事情變得流程標(biāo)準(zhǔn)化之后,開發(fā)程序就會飛快,開發(fā)評估工作總是 搞個1~2天,實(shí)則前端后端一起開發(fā),1個小時就可以搞定。
前端
const exportXls = async () => {
var now = moment(new Date()).format('YYYYMMDDHHMMSS')
let name = '商品收發(fā)明細(xì)表.xls'
const res = await proxy.$api.invOrder.goodsRdDetail.export({...condForm.value})
let data = res.data;
let url = window.URL.createObjectURL(new Blob([data], ))
let link = document.createElement('a')
link.style.display = 'none'
link.href = url;
console.log(link);
link.setAttribute('download', now + name)
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
請求的代碼如下
public postOnlyFile = (url: string, data = {} , config: AxiosRequestConfig<any> = {}): Promise<any> =>
axios({
...this.baseConfig,
headers:{
...this.baseConfig.headers,
'Content-Type': "application/json"
},
responseType:'blob',
url,
method: 'post',
data,
...config,
})
后端
controller層基本就是復(fù)制粘貼,傳參數(shù)給到service層而已。
@PostMapping("export")
@ApiOperation("導(dǎo)出商品收發(fā)明細(xì)表")
public void export(@RequestBody PsiInvOrderReportCondDto condDto, HttpServletResponse response){
//設(shè)置響應(yīng)頭
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
//設(shè)置防止文件名中文亂碼
try {
//設(shè)置防止文件名中文亂碼
String fileName = URLEncoder.encode("商品收發(fā)明細(xì)表", "utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
//
if (!CheckEmptyUtil.isEmpty(condDto.getBillDateRange())){
condDto.setStartBillDate(condDto.getBillDateRange().get(0));
condDto.setEndBillDate(condDto.getBillDateRange().get(1));
}
goodsRdDetailService.export(response.getOutputStream(),"xls/GoodsRdDetail.xls", condDto);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
service層主要通過easyexcel填充數(shù)據(jù)
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
@Override
public void export(OutputStream outputStream, String pathName, PsiInvOrderReportCondDto condDto) {
List<GoodsRdDetailListDto> goodsRdDetailListDtos = select(condDto);
int line = 1;
for (GoodsRdDetailListDto goodsRdDetailListDto:goodsRdDetailListDtos){
goodsRdDetailListDto.setLine(String.valueOf(line++));
goodsRdDetailListDto.setBillDateStr(DateUtil.formatDate(goodsRdDetailListDto.getBillDate()));
BusinessTypeEnum businessTypeEnum = BusinessTypeEnum.getInvBusinessTypeEnum(goodsRdDetailListDto.getBusiType());
goodsRdDetailListDto.setBusiType(businessTypeEnum.display());
}
org.springframework.core.io.Resource resource = new ClassPathResource(pathName);
InputStream inputStream = null;
String fileName = DateUtil.getDateRandom() + ".xls";
File file = new File(TmpDic.url + File.separator + fileName);
try {
inputStream = resource.getInputStream();
FileUtils.copyInputStreamToFile(inputStream, file);
} catch (IOException e) {
e.printStackTrace();
}
ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(file).build();
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
//
WriteSheet sheet0 = EasyExcel.writerSheet(0,"單據(jù)明細(xì)(不合并表頭)")
//單元格
// .registerWriteHandler(new CustomCellWriteHandler())
.build();
excelWriter.fill(goodsRdDetailListDtos, fillConfig, sheet0);
//
PsiAccountSet psiAccountSet = psiAccountSetService.getById(condDto.getAsId());
GoodsRdDetailExcelHeaderDto excelHeaderDto = new GoodsRdDetailExcelHeaderDto();
excelHeaderDto.setCompanyName(psiAccountSet.getName());
excelHeaderDto.setStartBillDate(DateUtil.formatDate(condDto.getStartBillDate()));
excelHeaderDto.setEndBillDate(DateUtil.formatDate(condDto.getEndBillDate()));
excelWriter.fill(excelHeaderDto,sheet0);
//
excelWriter.finish();
file.delete();
}
excel模板
定義excel模板,就是上面的xls/GoodsRdDetail.xls

到此這篇關(guān)于Java根據(jù)模板實(shí)現(xiàn)excel導(dǎo)出標(biāo)準(zhǔn)化的文章就介紹到這了,更多相關(guān)Java導(dǎo)出excel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot環(huán)境下junit單元測試速度優(yōu)化方式
這篇文章主要介紹了SpringBoot環(huán)境下junit單元測試速度優(yōu)化方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
java正則表達(dá)式如何獲取xml文件中指定節(jié)點(diǎn)的值
這篇文章主要介紹了java正則表達(dá)式如何獲取xml文件中指定節(jié)點(diǎn)的值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
java短信驗(yàn)證碼獲取次數(shù)限制實(shí)例
這篇文章主要介紹了java短信驗(yàn)證碼獲取次數(shù)限制實(shí)例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
mybatis 插件: 打印 sql 及其執(zhí)行時間實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猰ybatis 插件: 打印 sql 及其執(zhí)行時間實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06

