使用easyexcel填充模板数据,并导出excel

文章目录


前言

导出excel功能非常场景,本片文章记录如何使用模板填充数据后再导出。因直接导出excel数据样式不符合要求,所以做了模板填充然后再导出excel。

效果如下:


一、制作模板

注意:列表数据变量名前面要写点{.id},如果单条数据可以不写。

二、前端代码

使用表单提交:

c 复制代码
<% layout('/layouts/default.html', {title: '导出信用用户', libs: ['validate','fileupload']}){ %>
<style type="text/css">
	.delete {
		display: none;
	}
</style>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-tools pull-right">
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
			</div>
		</div>
		<#form:form id="inputForm" model="${cshopUser}" action="${ctx}/shop/user/cshopUser/download" method="post" class="form-horizontal">
			<div class="box-body">
				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2"><span class="required">*</span>${text('请选择村庄')}:</label>
							<div class="col-sm-10">
								<#form:treeselect id="office" title="乡镇/村庄选择"
								path="office.officeCode" labelPath="office.officeName"
								url="${ctx}/sys/office/treeData"
								class="required" allowClear="true"/>
							</div>
						</div>
					</div>
				</div>
				<div class="box-footer">
					<div class="row">
						<div class=" text-center">
							<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit">${text('导出用户')}</button>&nbsp;
							<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
						</div>
					</div>
				</div>
		</#form:form>
	</div>
</div>
<% } %>
<script>
$("#inputForm").validate({
	submitHandler: function(form){
		js.ajaxSubmitForm($('#inputForm'), {
			url:'${ctx}/shop/user/cshopUser/daochu',
			downloadFile:true
		});
    }
});
</script>

三、后端代码

实体代码:

java 复制代码
@Data
public class BianGengExcel {
    @ExcelProperty(value = "ID")
    private String id;
    @ExcelProperty(value = "姓名")
    private String name;
    @ExcelProperty(value = "手机号")
    private String phone;

}

controller代码:

java 复制代码
    /**
     * 导出用户
     *
     * @param cshopUser
     * @return
     */
    @PostMapping(value = "daochu")
    public String daochu(@Validated CshopUser cshopUser,
                         HttpServletResponse response) {
        List <BianGengExcel> data = cshopUserService.daochu(cshopUser);
        try {
            OutputStream out = response.getOutputStream();
            //文件名需要这样写,不能在setHeader直接写中文名,否则下载的文件名字为空,只有后缀
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("用户-"+System.currentTimeMillis(), "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            String templateFileName = this.getClass().getResource("/static/moban/daochu_moban.xls").getPath();
            ExcelWriter excelWriter = EasyExcelFactory.write(out,BianGengExcel.class).withTemplate(templateFileName).build();
//            ExcelWriter excelWriter = EasyExcel.write(out).withTemplate(templateFileName).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.FALSE).build();
            excelWriter.fill(data,fillConfig,writeSheet);
            excelWriter.finish();
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return renderResult(Global.TRUE, text("导出用户成功!"));
    }

总结

只对jeesite感兴趣可以订阅jeesite专栏。更多实战经验,请关注实战专栏,实战专栏包含了所有jeesite专栏文章。

相关推荐
AI导出鸭13 小时前
怎么让豆包做表格?AI导出鸭苹果版将豆包输出的管道表格智能解析为二维结构,一键导出为Excel或Word标准表格。
人工智能·chatgpt·word·excel·ai导出鸭
Am-Chestnuts2 天前
AI表格复制到Excel后日期金额和编号被改写:导入与格式检查方法
excel
方渐鸿2 天前
【无标题】使用Agent总结网页信息写入Excel
ai·excel
汉拓3D数字化2 天前
设计标准化怎么落地?企业设计标准化的5阶段实施路径
科技·excel·系统·软件
灵析表格3 天前
json_ObjectToKV 函数:Excel解析JSON对象的权威方案
json·excel
灵析表格3 天前
灵析表格文本处理函数技术白皮书:WPS Excel 官方函数扩展库 18 项文本规整与正则批处理企业落地标准
ai·json·excel·wps
灵析表格3 天前
WPS Excel官方函数扩展库数据安全加密校验函数深度研究报告:Excel国密SM3/SM4加解密、MD5摘要、UUID、Base64与URL编解码全栈解析
excel·wps
AC赳赳老秦4 天前
网页公开附件自动采集:OpenClaw 批量下载页面内嵌 Word/Excel 附件,统一格式后结构化入库
java·python·word·php·excel·deepseek·openclaw
VBAMatrix4 天前
表格导航!实现Word合并附注与多个Excel的数据同步
word·excel·审计报告·审计·会计师事务所·报告工具
AI英德西牛仔4 天前
手机文心怎么导出文档?AI 导出鸭一键解决格式崩塌与批量归档难题
人工智能·智能手机·excel·deepseek·ai导出鸭