如何从Temp文件夹中渲染JSP页面中的上传Excel文件?

时间:2017-07-12 07:26:29

标签: java excel spring jsp

我不清楚将上传的Excel工作表呈现到JSP页面

以下是上传Excel的示例代码

importExcel.jsp

<body>
<form:form method="POST" action="fileUpload"  enctype="multipart/form-data">
  <div class="upload">
 <div class="upload-files">
  <header>
   <p>
    <i class="fa fa-cloud-upload" aria-hidden="true"></i>
    <span class="up">up</span>
    <span class="load">Load</span>
   </p>
  </header>
  <div class="body" id="drop">
   <i class="fa fa-file-text-o pointer-none" aria-hidden="true"></i>
   <p class="pointer-none"><b>Drag and drop</b> files here <br /> or <a href="" id="triggerFile">browse</a> to begin the upload</p>
            <input type="file" name="xlsFile" accept=".xls,.xlsx" />
  </div>
  <footer>
   <div class="divider">
    <span><AR>FILES</AR></span>
   </div>
   <div class="list-files">
    <!--   template   -->
   </div>
            <button class="importar">UPDATE FILE</button>
  </footer>
 </div>
</div>


</form:form>
</body>

HomeController.java

@PostMapping("/fileUpload")
    public String getFileUploadResult(@RequestParam("xlsFile") MultipartFile multiPartFile )throws Exception{
        try {

            if(multiPartFile!=null && !multiPartFile.isEmpty()){
                byte[] fileBytes = multiPartFile.getBytes();
                if(fileBytes!=null){
                    System.out.println("multiPartFile.getOriginalFilename() :: "  +multiPartFile.getOriginalFilename());
                    Path internalPath=Paths.get(RAW_DATA_FILE_PATH+multiPartFile.getOriginalFilename());
                    if(internalPath!=null){
                        System.out.println("file written");
                        Files.write(internalPath,fileBytes);
                    }else{
                        System.out.println("internalPath is null");
                    }
                }else{
                    System.out.println("fileBytes is null");
                }
            }else{
                return "importExcel";
            }
        } catch (Exception e) {
            throw e;
        }
        return "viewUploadedExcel";
    }

我需要在viewUploadedExcel.jsp文件中查看上传的Excel文件,因为我正在对此进行研究以提出解决方案而尚未满足期望。

注意:我正在研究JSP和Spring

1 个答案:

答案 0 :(得分:0)

只需将响应contentType中的mime / type设置为application / vnd.ms-excel,具体取决于文件格式。基于文件扩展名,xlsx将是application / vnd.openxmlformats-officedocument.spreadsheetml.sheet。然后只需使用out.write上传文件的内容。 显然,浏览器对excel的渲染取决于浏览器。