excel导出
Vue:
导出组件:
<v-toolbar ref="toolbar" :top-line="true" :column-flag="false">
<template slot="left">
<el-button
type="success"
plain
icon="el-icon-download"
@click="openExportData"
>导出</el-button>
</template>
</v-toolbar>
导出方法:
// 文件导出
openExportData() {
exportDetailsReimbursement(
this.listQuery
).then(response => {
this.downloadFile(response.data)
}).catch(error => {
this.listLoading = false
this.$messageOK('error', error)
})
},
// 文件下载
downloadFile(data) {
if (!data) {
return
}
const url = window.URL.createObjectURL(new Blob([data]))
const link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', '报销明细.xls')
document.body.appendChild(link)
link.click()
},
js代码:
export function exportDetailsReimbursement(data) {
return axios({
url: 'reimburse/reiForm/exportDetailsReimbursement',
method: 'post',
responseType: 'blob',
headers: {
'Content-Type': 'application/json',
'X-Token': getToken()
},
transformRequest: [function(data) {
data = JSON.stringify(data)
return data
}],
data
})
}
java代码:
@Override
public void exportDetailsReimbursement(HttpServletResponse response, HttpServletRequest request, ReiFormVO vo) {
Workbook workBook = null;
ServletOutputStream outputStream = null;
try {
String fileName = "exportDetailsReimbursement.xls";
// 防止中文乱码问题
fileName = ExcelUtil.encodeChineseDownloadFileName(request, fileName);
String[] column = ReiFormConstantUtil.EXPORT_TITLE_DETAILS;
// 查出要导出的信息
ReiFormVO reiFormVO = getReiFormVOS(vo);
List<ReiFormVO> uuList = reiFormMapper.reimbursementDetailsQuery(reiFormVO);
uuList.forEach(f -> {
f.setReiAmtStr(MoneyUtil.fenToYuan(f.getReiAmt()));
});
List<Object[]> uList = new ArrayList<>(uuList.size());
if(uuList.size() > 0) {
// String beginDate = StringUtils.isNotEmpty(vo.getBeginApplyDate()) ? vo.getBeginApplyDate() : "";
// String endDate = StringUtils.isNotEmpty(vo.getEndApplyDate()) ? vo.getEndApplyDate() : "";
// String showDate = beginDate + "-" + endDate;
// 要导出的信息
for (int i = 0; i < uuList.size(); i++) {
Object[] tmp = new Object[30];
ReiFormVO psot = uuList.get(i);
tmp[0] = DateTimeUtil.timestampToString( psot.getApplyDate(), DateTimeUtil.DATE_FORMAT_YYYY_MM_DD);
tmp[1] = psot.getDeptName();
tmp[2] = psot.getProName();
tmp[3] = psot.getBizTypeStr();
tmp[4] = psot.getFormNo();
tmp[5] = psot.getFundNumber();
tmp[6] = psot.getOwnerName();
tmp[7] = psot.getReiAmtStr();
tmp[8] = psot.getFormStatusStr();
tmp[9] = psot.getOpName();
tmp[10] = psot.getFormStatus();
tmp[11] = psot.getApplyDate();
uList.add(tmp);
}
workBook = ExcelUtil.createWorkBook(fileName, column, uList);
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-disposition", "attachment;fileName=" + fileName);
response.flushBuffer();
outputStream = response.getOutputStream();
workBook.write(outputStream);
outputStream.flush();
outputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (workBook != null) {
try {
workBook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
其中表头写在ReiFormConstantUtil工具类中
public static final String[] EXPORT_TITLE_DETAILS = {"申请日期", "部门名称", "项目名称", "报销类型", "报销单号"};
Mapper中(带分页)
IPage<ReiFormVO> reimbursementDetailsQuery(@Param("page") Page<ReiFormVO> page, @Param("vo") ReiFormVO vo);
SQL语句:
<select id="reimbursementDetailsQuery" resultType="com.datalook.basic.utils.vo.reimburse.ReiFormVO" databaseId="oracle">
SELECT
REI_FORM.APPLY_DATE,
REI_FORM.APPLY_DEPT_NAME DEPT_NAME,
REI_FORM.APPLY_DEPT_ID ,
REI_FMS_PROJECT.PRO_NAME PRO_NAME,
REI_FORM.BIZ_TYPE BIZ_TYPE,
di1.DICT_LABEL AS bizTypeStr ,
REI_FORM.FORM_NO FORM_NO,
REI_FMS_PROJECT.FUND_NUMBER,
REI_FORM.OWNER_NAME OWNER_NAME,
REI_FORM.REI_AMT REI_AMT,
REI_FORM.FORM_STATUS FORM_STATUS,
di2.DICT_LABEL AS formStatusStr,
REI_FORM.OP_NAME OP_NAME
FROM REI_FORM
LEFT JOIN REI_FMS_PROJECT ON REI_FMS_PROJECT.ID = REI_FORM.PRO_ID
LEFT JOIN SYS_DICT_DATA di1 ON REI_FORM.BIZ_TYPE = di1.DICT_VALUE AND di1.DICT_TYPE='BIZ_TYPE'
LEFT JOIN SYS_DICT_DATA di2 ON REI_FORM.FORM_STATUS = di2.DICT_VALUE AND di2.DICT_TYPE='REI_STATUS'
WHERE REI_FORM.DEL_FLAG = 0 AND REI_FMS_PROJECT.DEL_FLAG=0
AND REI_FORM.FORM_STATUS in
<foreach collection="vo.formStatusList" item="item" open="(" separator="," close=")">
#{item, jdbcType=VARCHAR}
</foreach>
<if test="vo.proIds != null and vo.proIds.size() > 0">
AND REI_FORM.PRO_ID in
<foreach collection="vo.proIds" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="vo.proId != null and vo.proId != ''">
and REI_FORM.PRO_ID = #{vo.proId, jdbcType=VARCHAR}
</if>
<if test="vo.deptCode != null and vo.deptCode != ''">
and REI_FMS_PROJECT.DEPT_CODE = #{vo.deptCode, jdbcType=VARCHAR}
</if>
<if test="vo.bizType != null and vo.bizType != ''">
and REI_FORM.BIZ_TYPE = #{vo.bizType, jdbcType=VARCHAR}
</if>
<if test="vo.formStatus != null and vo.formStatus != ''">
and REI_FORM.FORM_STATUS = #{vo.formStatus, jdbcType=VARCHAR}
</if>
<if test="vo.fundNumber != null and vo.fundNumber != ''">
AND REI_FMS_PROJECT.FUND_NUMBER like '%'||#{vo.fundNumber}||'%'
</if>
<if test="vo.formNo != null and vo.formNo != ''">
AND REI_FORM.FORM_NO like '%'||#{vo.formNo}||'%'
</if>
<if test="vo.ownerName != null and vo.ownerName != ''">
AND REI_FORM.OWNER_NAME like '%'||#{vo.ownerName}||'%'
</if>
<if test="vo.beginApplyDate != null and vo.beginApplyDate != ''">
AND to_char(REI_FORM.APPLY_DATE, 'yyyy-MM-dd') >= #{vo.beginApplyDate}
</if>
<if test="vo.endApplyDate != null and vo.endApplyDate != ''">
AND to_char(REI_FORM.APPLY_DATE, 'yyyy-MM-dd') <= #{vo.endApplyDate}
</if>
ORDER BY REI_FORM.APPLY_DATE DESC,REI_FMS_PROJECT.PRO_CODE ASC
</select>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix