先说思路:
前台Ajax上传文件后,后台excel信息补全,补全完成转换成base64码返回后,由前端下载
后端:
@RequestMapping("/importAssembling")
public String assembling(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletResponse response) throws Exception {
ExcelUtil<xx> util = new ExcelUtil<>(xx.class);
List<xxx> list = util.importExcel(file.getInputStream());
if (list.isEmpty()) {
return JSON.toJSONString(Messager.GetFailMessage("上传的数据不能为空!"));
}
//业务处理....
//构造excel 这个buildExcel是读取excel信息并处理,没啥重要的
HSSFWorkbook wk = buildExcel(list);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
wk.write(byteArrayOutputStream);
} catch (IOException e) {
e.printStackTrace();
}
byte[] bytes = byteArrayOutputStream.toByteArray();
String base64ExlCode = Base64Utils.encodeToString(bytes);
String excel = "data:application/vnd.ms-excel;base64," + base64ExlCode;
return JSON.toJSONString(Messager.GetSuccessMessage(200,"导入成功!",excel));
}
前端:
<a href="#"
class="easyui-linkbutton" data-options="iconCls:'icon-filter'"
style="width: 150px" onclick="importMigrationCompletion()">导入并补全</a>
<script>
/**
* 导入并装箱
*/
function importMigrationCompletion() {
//获取上传文件控件内容 File
let file = $("input[type='file']").val();
if (file == null) {
$.messager.alert("info", "错误,请选择excel再上传", "info");
return false;
} else if (file.lastIndexOf('.xls') < 0) {
$.messager.alert("info", "错误,请选择excel再上传", "info");
return false;
} else {
$.messager.progress({
title: '提示',
text: '正在导入中,请稍等...',
interval: '600'
});
var formData = new FormData(document.getElementById("importFileForm"));
$.ajax({
url: '../migrationCompletion/importAssembling',
type: 'POST',
data: formData,
async: true,
cache: false,
contentType: false,
processData: false,
success: function (result) {
// 关闭进度条
$.messager.progress('close');
$("#File").filebox({
buttonText: '选择文件',
prompt: '请选择excel文件'
});
var message = json2obj(result);
if (message.code == 200) {
$.messager.alert("info", message.message, "info");
download(message.data)
} else {
$.messager.alert({
width: 300,
height: 250,
title: "提示",
msg: '<div style="overflow:auto;height: 150px;">' + message.message + '</div>'
});
}
// 清空文件框
$("#File").filebox({
buttonText: '选择文件',
prompt: '请选择excel文件'
});
},
error: function () {
// 关闭进度条
$.messager.progress('close');
// 显示异常信息
$.messager.alert("warning","出现异常情况!请联系运维!", "warning");
}
})
}
}
function download(base64) {
let date = new Date();
if (!base64) {
return;
}
let aLink = document.createElement("a"); // 创建一个a标签
let blob = base64ToBlob(base64);
let event = document.createEvent("HTMLEvents");
event.initEvent("click", true, true);
aLink.download = date.getTime() + ".xls";
aLink.href = URL.createObjectURL(blob);
aLink.click();
}
// base64转Blob对象
function base64ToBlob(code) {
let parts = code.split(";base64,");
let contentType = parts[0].split(":")[1];
let raw = window.atob(parts[1]);
let rawLength = raw.length;
let uint8Array = new Uint8Array(rawLength);
for (let i = 0; i < rawLength; i++) {
uint8Array[i] = raw.charCodeAt(i);
}
return new Blob([uint8Array], {type: contentType});
}
</script>
//下面base64转Blob是从其他大佬博客看来的,但是忘记原地址了 TVT