0

I am trying to create a class to read a XLSX file when I upload it to a website.
I have the code to upload the file to the server. The file can be uploaded but it can't capture the data from the excel.
May I know how do I solve or modify this code such that the data is being able to be seen on the web?
I know there's some other duplicate questions out there but after trying, those answers can't seem to work for me.
If I remove the line public static void... , then I will get this error: Package should contain a content type part [M1.13]

public HashMap getConstructJXLList_xlsx(UploadedFile File, int Sheetindex) {
    String _LOC = "[PageCodeBase: getConstructJXLList_xlsx]";

    HashMap _m = new HashMap();
    return _m;
}

    //InputStream _is = null;

    public static void main(String[] args)
        {
            try {
            FileInputStream input = new FileInputStream(new File("C:\\Users\\admin\\Desktop\\Load_AcctCntr_Template.xlsx"));
            org.apache.poi.ss.usermodel.Workbook wb = WorkbookFactory.create(input);
            org.apache.poi.ss.usermodel.Sheet s = wb.getSheetAt(0);

            Iterator<Row> rows = s.rowIterator();

                    while (rows.hasNext())
                    {
                        Row row = rows.next();
                        Iterator cells = row.cellIterator();
                        while (cells.hasNext())
                        {
                            XSSFCell cell = (XSSFCell) cells.next();

                            if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING)
                            {
                                System.out.print(cell.getStringCellValue() + "t");
                            }
                            else if(cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC)
                            {
                                System.out.print(cell.getNumericCellValue() + "t");
                            }
                            else if(cell.CELL_TYPE_BLANK==cell.getCellType()) 
                                System.out.print( "BLANK " ); 
                            else 
                                System.out.print("Unknown cell type");
                        }
                input.close();
            }
                }
            catch (Exception e)
            {
                e.printStackTrace();
            }
}

If I just run the main method from above, this is the output:
0000002b SystemOut O [RedirectLogin: requiredRights]1.0en1102.xhtml
0000002b SystemOut O [En1102: doEn1102_command_readfileAction]1.0
0000002b SystemOut O [En1102: onPageLoadBegin]1.0
it seems that the server did not even run through this code above..

I can just download the excel template on the website and it will be saved on my desktop. The file will contain the title header and I am able to key in whatever data I want in the cells.

Cassie
  • 39
  • 11
  • possible duplicate of [org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException:](http://stackoverflow.com/questions/21992071/org-apache-poi-poixmlexception-org-apache-poi-openxml4j-exceptions-invalidforma) – ithofm May 21 '14 at 09:02
  • @ithofm Hi, thank you for giving me that link. It does help a little. Now there isn't any errors but there is still no data being capture on the website. – Cassie May 21 '14 at 09:28
  • Do you say, the code is working with the local file but not in your webapplication? If you just run your main method from above, what is the output? Could you also provide more information about your webapplication, please? How does it work, how do you store the uploaded file? Is the file completely written before you load it? – ithofm May 21 '14 at 11:42
  • @ithofm If I just run it, the file will be uploaded and it's suppose to show any data that I keyed in into the excel. But the data that I typed it into the excel won't show up on the website. By right after I download the XLSX Excel sheet, I am able to key in any information in the excel sheet. After that I can upload this excel file onto the web and the web will show the data. – Cassie May 22 '14 at 01:07
  • After I typed in any data I want on the excel: http://i.stack.imgur.com/6CWV4.jpg Data shown on web says "no results" http://i.stack.imgur.com/ztR9k.jpg – Cassie May 22 '14 at 01:13
  • ok, back to basic: Please change your code above and put some static data into the HashMap _m before you return it like this: `HashMap _m = new HashMap();` `_m.put("x1", "y1");` `_m.put("x2", "x2");` `return _m;` Then, try again, please, and comment if you see somthing on your website after uploading or not. – ithofm May 22 '14 at 08:19
  • Here is the **complete** method, try it out pls: public HashMap getConstructJXLList_xlsx(UploadedFile File, int Sheetindex) { String _LOC = "[PageCodeBase: getConstructJXLList_xlsx]"; HashMap _m = new HashMap(); _m.put("x1", "y1"); _m.put("x2", "x2"); return _m; } – ithofm May 22 '14 at 08:23
  • @ithofm Nope, still nothing on the website. Data shown on web says "no results" [link](i.stack.imgur.com/ztR9k.jpg) – Cassie May 22 '14 at 08:38
  • Ok, please provide more information and put it in your Q please. Start with answering my Questions from above:If you just run your main method from above, what is the output? Could you also provide more information about your webapplication, please? How does it work, how do you store the uploaded file? Is the file completely written before you load it? Its hard to help, without knowing, what you are really doing. – ithofm May 22 '14 at 08:44
  • Clearly your knowledge of java and the whole environment are not sufficient to fullfill this task. And stackoverflow is made to get answers for specific problems...while you need way more then that. I advise you to ask your colleges for more support. – ithofm May 22 '14 at 09:46
  • @ithofm Yup I understand that. But still, thank you for patience and help! – Cassie May 22 '14 at 09:51

1 Answers1

0

To read xlsx files, you should use:

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

XSSFWorkbook wb;
XSSFSheet sheet;
XSSFRow row;
XSSFCell cell;

FileInputStream input = new FileInputStream(new File("C:\\Users\\admin\\Desktop\\Load_AcctCntr_Template.xlsx"));
wb = new XSSFWorkbook(input );
yushulx
  • 11,695
  • 8
  • 37
  • 64
  • Hi. I tried both the codes you gave me and the codes I have above. Both doesn't work. Able to upload file but with empty data. – Cassie May 22 '14 at 06:15
  • in the other java file, I need to call this, getConstructJXLList_xlsx in order to make it run. In this case, my code doesn't work is it because under this code, getConstructJXLList_xlsx, there is nothing under it? There's only HashMap _m = new HashMap(); and return _m; – Cassie May 22 '14 at 06:16