JAVA 非同步ajax實現xls 檔案上傳 並且解析xls
阿新 • • 發佈:2019-01-08
html:
<!--upload html--> <input type="file" id="file" name="file" value="選擇檔案" /> <input type="button" value="上傳" onclick="upload();"/> <!--upload js---> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" src="js/ajaxfileupload.js"></script> <script type ="text/javascript"> var upload = function(){ $.ajaxFileUpload({ url: 'map/upload', secureuri: false, fileElementId: 'file', dataType: 'json', success: function (data, status){ alert("上傳成功"); }, error: function (data, status, e){ alert("上傳失敗"); } }); } </script>
java:
/** * 所用jar 這裡用的sping mvc 當然用其他的上傳得到流也是可以的 * jxl-2.4.3.jar * org.apache.common.io.jar * org.apache.common.fileupload.jar */ //上傳接收 @RequestMapping("/upload") public void upload(HttpServletRequest request, HttpServletResponse response) throws IOException{ MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; List<MultipartFile> fileList = multipartRequest.getFiles("file"); for (MultipartFile mf : fileList) { if(!mf.isEmpty()){ parseExcel(mf); break; } } //返回json toJson("{'status', 1}"); } //解析表格 public void parseExcel(MultipartFile mf){ Workbook workbook = null ; try { workbook = Workbook.getWorkbook(mf.getInputStream()); Sheet sheet = workbook.getSheet(0); int count = sheet.getRows(); for (int i= 1 ;i < count;i ++){ Cell[] cells = sheet.getRow(i); System.out.println(cells); } }catch (Exception e){ e.printStackTrace(); } finally { if (workbook != null ){ workbook.close(); } } }
原創:blog.csdn.net/qilin001cs