Link to home
Start Free TrialLog in
Avatar of jlguerra
jlguerra

asked on

How i do the upload file with struts and POI for read a excel file through the inputstream

Hi,  friends

    i have an aplication web on Struts and i use the FileUpload for read an archive and later i need read the excel file without save the archive on disk.   here i put the code.

This code upload the archive on memory.    Look on "http://www.roseindia.net/struts/strutsfileupload.shtml"

//*****************************************************************
  public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response) throws Exception{
    StrutsUploadForm myForm = (StrutsUploadForm)form;

        // Procesar el archivo cargado en el ActionForm
        FormFile myFile = myForm.getTheFile();
        String contentType = myFile.getContentType();
        String fileName    = myFile.getFileName();
        int fileSize       = myFile.getFileSize();
        byte[] fileData    = myFile.getFileData();

    System.out.println("contentType: " + contentType);
    System.out.println("File Name: " + fileName);
    System.out.println("File Size: " + fileSize);
//*****************************************************************



For read the excel archive, this is the code

//*****************************************************************
    POIFSFileSystem fs = new POIFSFileSystem( new FileInputStream("../prueba.xls"));
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    HSSFSheet sheet = wb.getSheetAt(0);

    System.out.println( "Va a iterar por fila ");

    // Iterate over each row in the sheet
    Iterator rows = sheet.rowIterator();
    while( rows.hasNext() ) {          
        HSSFRow row = (HSSFRow) rows.next();
        System.out.println( "Row #" + row.getRowNum() );

        // Iterate over each cell in the row and print out the cell's content
        Iterator cells = row.cellIterator();
        while( cells.hasNext() ) {
            HSSFCell cell = (HSSFCell) cells.next();
            System.out.println( "Cell #" + cell.getCellNum() );
            switch ( cell.getCellType() ) {
                case HSSFCell.CELL_TYPE_NUMERIC:
                    System.out.println( cell.getNumericCellValue() );
                    break;
                case HSSFCell.CELL_TYPE_STRING:
                    System.out.println( cell.getStringCellValue() );
                    break;
                default:
                    System.out.println( "unsuported sell type" );
                    break;
            }
        }
       
    }
//*****************************************************************

How i merge that?  without save the archive,  maybe use the inputstream.  How?


          Thanks a lot.
     
ASKER CERTIFIED SOLUTION
Avatar of suprapto45
suprapto45
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
;)

Glad I could help

David
Avatar of javadevman
javadevman

Instead of printing to console. How do we send a value back to a jsp using this code example above?