jsp upload Excel file with smartupload and parse the data with jxl in the background

Recently, I developed the function of uploading excel files and parsed the data. I specially made this record and shared it in case of emergency, and I hope it will be useful to everyone.

This example will implement the function:

  1. The front-end jsp page uploads the excel file.
  2. Use smartupload to receive excel files in the background.
  3. Read the excel file with jxl.jar and collect the data.

1. Upload the file at the front desk and upload
the code first

first.jsp

<FORM   NAME="uploadForm" METHOD='POST' 
    action="upload.jsp" 
    ENCTYPE="multipart/form-data" 
    onSubmit="return uploadCheck(this);" 
    target="importFrame">

<tr bgcolor="#F4F4F4" align="left">
    <td>
         请选择导入文件:&nbsp;
         <input type=file name="filename">
         <input type="submit" value="导入数据" name="ff" id="ff">
    </td>
</tr>
</form>

<iframe name="importFrame" width="100%" height="0" src="" FRAMEBORDER=NO ></iframe>

<script>
    function uploadCheck(f){
        var s=f.filename.value;
        if(s==""){
          alert("请选择文件"); 
          return false;
        }else if("xls"!=s.substr(s.length-3,3)){
            alert("请选择扩展名为xls的文件");
            return false;
        }
        f.ff.disabled = true;//点击之后,不可点
        return true;
    }
</script>

The code is simple, but there are a few small details to be aware of.

1. It seems that .jxl can only process EXCEL files in .xls format, so js is controlled here;
2. After uploading in first.jsp, the page does not jump, and the processing result needs to be displayed on the current page, so the target attribute of form is used , in this example, importFrame ;
3. After clicking the "Import Data" button, first set the button as unclickable to prevent repeated submissions;


2. smartupload receives excel files in the background

You need to download the smartupload.jar package in advance.

The code is as follows:
upload.jsp

 <%

String returnMessage = ""; //导入结果返回消息

try{
    com.jspsmart.upload.SmartUpload upload=new com.jspsmart.upload.SmartUpload();
       upload.initialize(pageContext);
       upload.setMaxFileSize(10000000); //限制上传文件的大小1兆
       upload.upload();//上传
      //文件上传后的文件对象
      com.jspsmart.upload.File myFile = upload.getFiles().getFile(0);
      String strFileName = myFile.getFileName();
      String abspath =request.getRealPath("/")+"/myworkupload/"+strFileName;
      myFile.saveAs(abspath);
%>

In this way, the edge receives the file uploaded in the foreground and saves it in a certain path on the server.


Three, JXL parsing EXCEL file

Of course, before using jxl, you still have to download the jxl.jar package.

upload.jsp

int sheetNum=0;
File sourcefile=new File(filePath);
InputStream is = new FileInputStream(sourcefile);
jxl.Workbook rwb = Workbook.getWorkbook(is);       
Sheet rs = rwb.getSheet(sheetNum);//页

int col = rs.getColumns();//列
int row = rs.getRows();
System.out.println("(文件:"+filePath+"["+sheetNum+"页]):总共"+row+"行,"+col
列col``
);

The above code reads the number of rows and columns in the first sheet in excel for use in reading the content in a loop.

upload.jsp

List workContentList = new ArrayList();//
List detailContentList = new ArrayList();//
List belongToList = new ArrayList();//
List beginDateList = new ArrayList();//
List endDateList = new ArrayList();//
List noteList = new ArrayList();//

for(int i=0;i<row;i++){//开始读取       
    //此处1代表从第二列读取,如果需要循环列,此处要再设置一层循环,这里为了演示,暂时略掉        
    String workContent= rs.getCell(1, i).getContents();
    workContentList.add(workContent);
    //将第二列中的内容,放到list中做后续处理,后续处理程序省略了

            ...
            ...

    System.out.println("第"+(i+1)+"行数据="+workContent);
}

For the above code functions, refer to the comments.


4. Return the processing result of first.jsp

Finally, don't forget, we also set a frame in first.jsp to wait for the result.

upload.jsp

<script>
alert('处理成功或者失败');
window.parent.document.getElementById('ff').disabled=false;
</script>

Since we set the action to display the result in the frame of the first.jsp page, here is the processing result that needs to be returned, through the alert prompt.

According to the actual processing result, pop up to the first.jsp page through alert.

After it pops up, set the button "Import Data" that cannot be clicked to clickable again.


No video, just take a few pictures

When the file suffix does not meet the requirements
write picture description here

When the upload is successful, the upload button cannot be clicked:
write picture description here

After clicking OK, the button resumes:
write picture description here

Well, let's share this first. If there is anything wrong, please leave a message for discussion.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325477822&siteId=291194637