Easyexcel 내보내기 및 다운로드 excel 로컬
3562 단어 파일 가져오기 내보내기
백엔드: 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 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);*/
}
aax가 아닌 시스템의 구성 요소를 호출하여 전달 대상 파라미터를 실현했습니다.
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 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);*/
}
aax가 아닌 시스템의 구성 요소를 호출하여 전달 대상 파라미터를 실현했습니다.