Excel Upload

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Suganth
    New Member
    • Mar 2014
    • 2

    Excel Upload

    Hi am trying to upload a excel sheet and parse the every columns and needs to save the columns in different tables for that am using jsp and servlet coding,in servlet coding am using apache POI for uploading, and i can able to upload the excel sheet but i cant parse the column values because encrypted values only i can able to get,

    Here is my JSP code,
    Code:
    <TITLE>Display file upload form to the user</TITLE></HEAD> 
    <BODY> <FORM ENCTYPE="multipart/form-data" ACTION="uploadExcel" METHOD=POST>
    <br><br><br>
    <tr><td colspan="2" align="center"> </td></tr>
    <tr><td colspan="2" align="center"><input type="submit" value="Send File"> </td></tr>
    <table>
    and my servlet code,

    Code:
    DiskFileItemFactory factory = new DiskFileItemFactory();
    		ServletContext servletContext = this.getServletConfig().getServletContext();
    File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
    factory.setRepository(repository);
    ServletFileUpload upload = new ServletFileUpload(factory);		
    try {
    @SuppressWarnings("unchecked")
    List<FileItem> items = upload.parseRequest(request);
    Iterator<FileItem> iter = items.iterator();
    while (iter.hasNext()) {
    FileItem item = iter.next();
    }
    } catch (FileUploadException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    Please give me suggestions ASAP.
    Thanks in advance.

    Regards,
    Suganth A
    Last edited by Rabbit; Mar 13 '14, 03:27 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Maybe the excel-sheet is protected or the cell values are calculated by VB-macros?
    please create a simple excel sheet yourself and try parsing that first!

    And "ASAP" (quotation), really very "ASAP", show us here what the input is, what values you expected to get and what values you really got, else we can't help you "ASAP". Thanks in advance, too.

    Comment

    • Suganth
      New Member
      • Mar 2014
      • 2

      #3
      Thanks for a help Dear chaarmann,

      The Excel file is not protected. and am attached the excel sheet in this reply.


      While executing this method in servlet

      Code:
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      		/*String XLFile=session.getAttribute("theName").toString();*/
      /*		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
      */		DiskFileItemFactory factory = new DiskFileItemFactory();
      		ServletContext servletContext = this.getServletConfig().getServletContext();
      		File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
      		factory.setRepository(repository);
      		ServletFileUpload upload = new ServletFileUpload(factory);		
      		try {
      			@SuppressWarnings("unchecked")
      			List<FileItem> items = upload.parseRequest(request);
      			Iterator<FileItem> iter = items.iterator();
      			while (iter.hasNext()) {
      			    FileItem item = iter.next();
      			    String root = getServletContext().getRealPath("/");
                      File path = new File(root + "/fileuploads");
                      System.out.println("EXACT PATH------------------------>>"+item);
      			    processFormField(item);
      			    /*if (item.isFormField()) {
      			        
      			    } else {
      			        processUploadedFile(item);
      			    }*/
      			}
      		} catch (FileUploadException e) {
      			// TODO Auto-generated catch block
      			e.printStackTrace();
      		} catch (Exception e) {
      			// TODO Auto-generated catch block
      			e.printStackTrace();
      		}
      }

      am getting the following details in console:

      name=SFD.xls, StoreLocation=C :\Users\css9242 1\workspace\.me tadata\.plugins \org.eclipse.ws t.server.core\t mp0\work\Catali na\localhost\Te stCAF\upload_2e 9eae35_144bf06d 095__8000_00000 000.tmp, size=57856bytes , isFormField=fal se, FieldName=file



      Instead of this details i need every column values presented in the uploaded excel sheet.

      in the sout i have printed the System.out.prin tln(item.getStr ing();)

      am getting the following output in the console,

      see the image i have attached.

      Thanks in advance chaarmann.
      Attached Files

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        The code above only uploads the file. I can't see any console-output-commands (except the file info) or processing of the uploaded file! Maybe it is contained in "processFormFie ld(item)"? Please list this method here ASAP. Thanks in advance. :-)

        The strange thing is that the console output should start with "EXACT PATH------------------------>>", but the console output you listed does not. Please post the correct console output here ASAP. Thanks in advance.

        The screenshot shows the binary data of the Excel-file. You should save it on disk (or hold it in a byte-array in memory) and then compare the original file with the uploaded one ASAP. Thanks in advance. If they are both the same, your upload works perfect!
        Now you need to use the POI library to parse the Excel file and extract the info. I suggest you first try to read an Excel-file from disk (instead of using the uploaded data) and use the POI library to parse it.
        I want to see sourcecode containing POI commands like:
        Code:
        FileInputStream file = new FileInputStream(new File("C:\\temp\SFD.xls"));
        HSSFWorkbook workbook = new HSSFWorkbook(file);
        HSSFSheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rows = sheet.iterator();
        for (row: rows) ... {
           Iterator<Cell> cells = row.cellIterator(); {
           for (cell: cells) ... {
              // print content of cell
        If each of these 2 separate tasks work (file uploading and excel parsing), and are well tested, then you can put both of them together ASAP. Thanks in advance.

        Comment

        Working...