1. 程式人生 > >基於BootStrap的initupload()實現Excel上傳和獲取excel中的數據

基於BootStrap的initupload()實現Excel上傳和獲取excel中的數據

增強for循環 function tail throws tab https pow preview layer

簡單說明:後邊要做exl解析(還沒做呢),所以先有一個excel的的上傳以及獲取excel中的數據,展示出來。

代碼:

//html代碼
<div class="btn-group">
<button class="btn sbold green" id="" onclick="initUpload(‘excelFile‘,‘/vraxx/rightAxx/upload‘);">
    <span class="ladda-label">導入權益</span>
</button>
</div>
<div class="modal-body">
<div class="row">
<div class="form-group">
<div id="res_tree">
<input id="excelFile"
name="excelFile" type="file" multiple="multiple" accept=".xls,.xlsx"/>
</div>
</div>
</div>
</div>
//js代碼
function initUpload(ctrlName, uploadUrl) {
var control = $(‘#‘ + ctrlName);
   //延時可以去掉的,不影響
clickTimer = window.setTimeout(function(){
control.fileinput({
language: ‘zh‘, //設置語言
uploadUrl: rootPath+uploadUrl, //上傳的地址
uploadAsync: false, //默認異步上傳
showCaption: true,//是否顯示標題
showUpload: true, //是否顯示上傳按鈕
browseClass: "btn btn-primary", //按鈕樣式
allowedFileExtensions: ["xls", "xlsx"], //接收的文件後綴
maxFileCount: 0,//最大上傳文件數限制
previewFileIcon: ‘<i class="glyphicon glyphicon-file"></i>‘,
showPreview: true, //是否顯示預覽
previewFileIconSettings: {
‘docx‘: ‘<i ass="fa fa-file-word-o text-primary"></i>‘,
‘xlsx‘: ‘<i class="fa fa-file-excel-o text-success"></i>‘,
‘xls‘: ‘<i class="fa fa-file-excel-o text-success"></i>‘,
‘pptx‘: ‘<i class="fa fa-file-powerpoint-o text-danger"></i>‘,
‘jpg‘: ‘<i class="fa fa-file-photo-o text-warning"></i>‘,
‘pdf‘: ‘<i class="fa fa-file-archive-o text-muted"></i>‘,
‘zip‘: ‘<i class="fa fa-file-archive-o text-muted"></i>‘,
},
     //黃色部分是業務代碼,可以刪去
     //藍色部分是和excel文件相關的固定寫法
     //div_startimport是插件裏的某個元素
uploadExtraData: function () {
var rightCode=$("#rightCode").val();

if(rightCode == null){
layer.alert("請選取XX號進行上傳")
var oTable = new TableInit();
oTable.Init(data);
$("#div_startimport").show();
}else {
var extraValue = "test";
return {"excelType": extraValue};

}
}
});
     //後邊兩句也可以去掉,我自己的一個彈出樣式
$(‘#res_tree‘).jstree("deselect_all",true);
$(‘#responsive_1‘).modal();
}, 300);

}

$("#excelFile").on("filebatchuploadsuccess", function (event, data, previewId, index) {
   //樣式可刪去
$("#responsive_1").modal(‘hide‘);
   //業務代碼可刪除
var rightCode=$("#rightCode").val();
   //很關鍵 獲取excel裏的數據轉為json
var obj=JSON.stringify(data.response);
   //後邊是通過html動態加載,把數據傳到後臺
   //換成一般的ajax也是可以的,靈活一點
var html = $("#topli").html();
var tb = $("#loadhtml");
var searchServPath = "/vraxx/rightAxx/toexcel";
tb.html(CommnUtil.loadingImg());
tb.load(rootPath + searchServPath,{excelJson:obj,rightCode:rightCode}, function () {
/** 動態構建內容頁面的 path 連接 */
html += ‘<li data-path="‘ + searchServPath + ‘"><i class="fa fa-circle"></i><a href="javascript:void(0)">XX導入</a></li>‘;
$("#topli").html(html);
});
});

//後臺java代碼
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
private List<List<String>> importExcel(@RequestParam(value = "excelFile", required = false) MultipartFile[] file, HttpServletRequest request) {
ModelMap map=new ModelMap();
List<List<String>> list =new ArrayList<>();
try {
MultipartRequest multipartRequest=(MultipartRequest) request;
List<MultipartFile> files = ((MultipartHttpServletRequest) request)
.getFiles("excelFile");
for (MultipartFile mufile :files){
List<List<String>> datas = ExcelUtil.readXlsx(mufile.getInputStream(),mufile.getOriginalFilename());
list.addAll(datas);
}
} catch (Exception e) {
e.printStackTrace();
logger.error("導入失敗");
}
return list;
}


@RequestMapping("/toexcel")
public String toexcel(String excelJson,String rightCode,ModelMap map) throws Exception{
   List<List<String>> listString= JSONArray.fromObject(excelJson);
   List<VraxxTemporary> rightList = new ArrayList<>();
   /**
   * 對listString增強for循環取數據放到rightList的那坨代碼就不貼了
   */

   map.addAttribute("rightList", rightList);
   return VIEW_PATH+"/detail";//跳轉到數據展示頁
}

說明:網上有很多關於bootstrap fileupload的使用介紹,但是很多都不太好使,我做這個試了好久才成功,拿出來分享給大家。

基於BootStrap的initupload()實現Excel上傳和獲取excel中的數據