你的位置:
首页
>
业界
>
Easyexcel导出并下载excel到本地
后端:
public AjaxResult easyExcelAction(Tj0staff tj0staff)
{
//创建文件
FileOutputStream fileOut;
String reFileName = "static/img";
if (!new File(Global.getProfile() + "/" + reFileName).exists()) {
new File(Global.getProfile() + "/" + reFileName).mkdirs();
}
reFileName = reFileName+"/tj.xlsx";
StringBuilder fileName = new StringBuilder();
fileName.append(Global.getProfile() + "/" + reFileName);
try {
//String dateStr = tools.getStringTodayNoKong();
System.out.println("准备插入的文件名是:" + fileName);
//需要先成一个后缀是。xlsx的文件,格式不对,不能正常打开
File fileFill07 = new File(fileName.toString());
if(!fileFill07.exists()){
fileFill07.createNewFile();
}
String LargFilePath = "large";
String large = LargFilePath+"/fill_v02.xlsx";
StringBuilder fileNameLarg = new StringBuilder();
fileNameLarg.append(Global.getProfile() + "/" + large);
System.out.println("模板路径:"+fileNameLarg.toString());
File template07 = new File(fileNameLarg.toString());
ExcelWriter excelWriter = EasyExcel.write(fileFill07).withTemplate(template07).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
List<Tj0staff> tj0staffs = tj0staffService.selectTjAllList(tj0staff);
System.out.println("待导出的数据" + tj0staffs);
excelWriter.fill(tj0staffs, writeSheet);
excelWriter.finish();
} catch (Exception e) {
e.printStackTrace();
logger.error("导出出错:{}", e.getMessage());
return error("导出出错!");
}
return success(Constants.RESOURCE_PREFIX+"/"+reFileName);
}
难点是读取和保存路径,在打包后不能正常,不要放到项目中。
前端
function easyExcel() {
var formatter = $("#formId").serialize();
console.log(formatter)
if ($("#startTime").val() == "") {
$.modal.alertError("请选择开始时间");
return false;
}if ($("#endTime").val() == "") {
$.modal.alertError("请选择结束时间");
return false;
}
$.operate.post(ctx + "tj/0staff/easyExcel", formatter, function (data) {
if (data.code == 0) {
window.open(ctx + 'common/download/resource?resource=' + data.msg);
} else {
$.modal.alertError(data.msg);
}
}, 'json');
/*$.ajax({
type: "post",
dataType: "json",
contentType: "application/json",
url: ctx + "tj/0staff/easyExcel/",
data: formatter,
success: function (data) {
if (data.code == 0) {
window.location.href = ctx + 'common/download/resource?resource=' + data.msg;
} else {
$.modal.alertError(data.msg);
}
}
});*/
/* var aa = httpPost("http://127.0.0.1/tj/0staff/easyExcel", formatter);
alert("------执行结果--------"+aa);*/
}
调用了系统中的组件,而不是用ajax,实现了传递对象参数。
后端:
public AjaxResult easyExcelAction(Tj0staff tj0staff)
{
//创建文件
FileOutputStream fileOut;
String reFileName = "static/img";
if (!new File(Global.getProfile() + "/" + reFileName).exists()) {
new File(Global.getProfile() + "/" + reFileName).mkdirs();
}
reFileName = reFileName+"/tj.xlsx";
StringBuilder fileName = new StringBuilder();
fileName.append(Global.getProfile() + "/" + reFileName);
try {
//String dateStr = tools.getStringTodayNoKong();
System.out.println("准备插入的文件名是:" + fileName);
//需要先成一个后缀是。xlsx的文件,格式不对,不能正常打开
File fileFill07 = new File(fileName.toString());
if(!fileFill07.exists()){
fileFill07.createNewFile();
}
String LargFilePath = "large";
String large = LargFilePath+"/fill_v02.xlsx";
StringBuilder fileNameLarg = new StringBuilder();
fileNameLarg.append(Global.getProfile() + "/" + large);
System.out.println("模板路径:"+fileNameLarg.toString());
File template07 = new File(fileNameLarg.toString());
ExcelWriter excelWriter = EasyExcel.write(fileFill07).withTemplate(template07).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
List<Tj0staff> tj0staffs = tj0staffService.selectTjAllList(tj0staff);
System.out.println("待导出的数据" + tj0staffs);
excelWriter.fill(tj0staffs, writeSheet);
excelWriter.finish();
} catch (Exception e) {
e.printStackTrace();
logger.error("导出出错:{}", e.getMessage());
return error("导出出错!");
}
return success(Constants.RESOURCE_PREFIX+"/"+reFileName);
}
难点是读取和保存路径,在打包后不能正常,不要放到项目中。
前端
function easyExcel() {
var formatter = $("#formId").serialize();
console.log(formatter)
if ($("#startTime").val() == "") {
$.modal.alertError("请选择开始时间");
return false;
}if ($("#endTime").val() == "") {
$.modal.alertError("请选择结束时间");
return false;
}
$.operate.post(ctx + "tj/0staff/easyExcel", formatter, function (data) {
if (data.code == 0) {
window.open(ctx + 'common/download/resource?resource=' + data.msg);
} else {
$.modal.alertError(data.msg);
}
}, 'json');
/*$.ajax({
type: "post",
dataType: "json",
contentType: "application/json",
url: ctx + "tj/0staff/easyExcel/",
data: formatter,
success: function (data) {
if (data.code == 0) {
window.location.href = ctx + 'common/download/resource?resource=' + data.msg;
} else {
$.modal.alertError(data.msg);
}
}
});*/
/* var aa = httpPost("http://127.0.0.1/tj/0staff/easyExcel", formatter);
alert("------执行结果--------"+aa);*/
}
调用了系统中的组件,而不是用ajax,实现了传递对象参数。
与本文相关的文章
- Python处理CSV,Excel,PDF和图片
- 【Excel】
- EasyExcel
- Vue下载excel,easyExcel
- excel,word,ppt,pdf,swf 文件互相转换
- 删除Office(Word,Excel,PowerPoint)中Recent Document最近文档地址不存在的文件
- ChatGPT结合Excel与VBA:迈向AI办公智能化与高效化
- 计算机上做表表格不够用怎么添加,excle表格不够怎么加excel表格不够用怎么办...
- excel转word后表格超出页面_excel表格粘贴到word太宽显示不全怎么办?
- 取消、禁止OfficeExcel开机自动新建空白文件
- Easyexcel导出并下载excel到本地
- 全面掌握Office:Word、Excel和PowerPoint操作技巧
- Excel文件已损坏无法打开修复解决方法
- 学计算机没有电脑怎么办,电脑没有excel怎么办
- 用EXCEL打开TXT文件的办法
- excel表格xlsx解密方法,忘记excel表格xlsx密码怎么办?
- excel表格中忘了撤销工作表保护密码怎么办
- 计算机右键无法新建excel,右键新建中没有excel怎么删除_右键新建中没有excel怎么办...
- 在excel中输入一串数字,可是最后几位数变成了0怎么办?
- excel出现为了防止数据流失,无法移走非空单元格怎么办