刚写了个导入excel表格,现在来写个导出,其实形式都差不多,废话不多说,贴代码
<div>
<button type="button" class="btn-btn" @click="exportdata()">导出</button>
</div>
这里根据个人需求,我这里写时间设置,根据条件去请求导出,这里因人而异
exportdata(){
//判断用户有无填写操作类型
if (this.type != "") {
//默认全部操作类型,不传参
this.param["code"] = this.code;
}
//判断用户有无填写开始日期
if (this.startdate != '') {
this.param['statrdate'] = new date(this.startdate);
}
//判断用户有无填写结束日期
if (this.enddate != '') {
this.param['enddate'] = new date(this.enddate);
}
this.param[this.optionselected] = this.param.searchtext;
var url = "${ctx}/operatelog/exportoperatelog";
window.open(url);
}
java中controller层,来接受请求,数据库查询到的数据进行封装,然后使用excelutils进行输出。
@requestmapping("/exportoperatelog")
@responsebody
public void exportoperatelog(httpservletrequest request, httpservletresponse response, operatelogparam param) {
simpledateformat dateformat = new simpledateformat("yyyymmddhhmmss");
try {
response.reset();
response.setcontenttype("text/html;charset=gbk");// 设置response内容的类型
response.setheader("content-disposition", "attachment;filename=systeamlog" + dateformat.format(new date()) + ".xls;");
string strtablename = "系统日志"; //这个是标题
list<string> headers = new arraylist<>();
headers.add("序号"); //这里的表头,根据数据的字段命名也行,随你喜欢
headers.add("操作日期时间");
headers.add("操作类型");
headers.add("数据1");
headers.add("数据2");
headers.add("操作员id");
headers.add("操作员");
list<operatelogparam> operateloglist = operatelogservice.findoperateloglist(param);
list<string[]> operateliststr = new arraylist<>();
for (int i = 0; i < operateloglist.size(); i++) {
operatelogparam opelogpar = operateloglist.get(i);
string[] strarr = new string[]{(stringutils.isempty(opelogpar.getoperatelogid().tostring())?"-":opelogpar.getoperatelogid().tostring()),
(stringutils.isempty(opelogpar.getcreatetime().tostring())?"-":opelogpar.getcreatetime().tostring()),
(stringutils.isempty(opelogpar.gettitle())?"-":opelogpar.gettitle()),
(stringutils.isempty(opelogpar.getdata1())?"-":opelogpar.getdata1()),
(stringutils.isempty(opelogpar.getdata2())?"-":opelogpar.getdata2()),
(stringutils.isempty(opelogpar.getadminid().tostring())?"-":opelogpar.getadminid().tostring()),
(stringutils.isempty(opelogpar.getoperatorname())?"-":opelogpar.getoperatorname()),
};
operateliststr.add(strarr);
}
excelutil.createexcel(strtablename, headers, operateliststr, response.getoutputstream());
} catch (ioexception e) {
e.printstacktrace();
}
这里里面的excelutils 在我的另一篇笔记中有
excel导入功能
导出效果
到此这篇关于java实现导出excel功能的文章就介绍到这了,更多相关java导出excel内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!