-1

I want to read an excel file from user PC in web app. Is it possible to read cell by cell and insert the cell values to mysql db. Or do i have to upload the file first to server and then read it?(JSP is used to build web app)

ViRaPa
  • 13
  • 1
  • 9
  • You will need to upload it before you start parsing it. – Stuart Aug 28 '17 at 09:09
  • Is there any way i can access it directly. If not jsp in some other languages? – ViRaPa Aug 28 '17 at 09:16
  • You could try to use Javascripts `FileReader`, and then post the data to your back-end. – Stuart Aug 28 '17 at 09:18
  • I have no idea about javascript. Could you please explain with code. – ViRaPa Aug 28 '17 at 09:19
  • Heres the docs: https://developer.mozilla.org/en-US/docs/Web/API/FileReader. Also, see this anser: https://stackoverflow.com/questions/3582671/how-to-open-a-local-disk-file-with-javascript – Stuart Aug 28 '17 at 09:32
  • The files which i want to read are excel files. I want to read cell by cell and insert the cell data to mysql db – ViRaPa Aug 28 '17 at 09:46
  • LMGTFY: https://stackoverflow.com/questions/8238407/how-to-parse-excel-file-in-javascript-html5 – Stuart Aug 28 '17 at 09:47

1 Answers1

-2

give you show code:

//将Excel中的数据导入  
public ArrayList<Book> ExcelIn(){  
    ArrayList<Book>arrayList=new ArrayList<Book>();  
    Workbook bWorkbook=null;  
    try {  
        bWorkbook=Workbook.getWorkbook(new File("D:/book.xls"));  
        Sheet sheet=bWorkbook.getSheet(0);  
        for (int i = 0; i < sheet.getRows(); i++) {  
            Book book=new Book();  
            //获取单元格对象  
            Cell cell =sheet.getCell(0,i);  
            //获取单元格的值  
            book.setId(Integer.valueOf(cell.getContents()));  
            book.setName(sheet.getCell(1,i).getContents());  
            book.setType(sheet.getCell(2, i).getContents() );  
            arrayList.add(book);  

        }  

    } catch (BiffException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    } catch (IOException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }finally {  
        bWorkbook.close();  
    }  
    return arrayList;  
}  
wwj
  • 35
  • 2
  • 1
    This doesn't answer the question - you are simply accessing an Excel file at a hard coded location. – slugster Aug 28 '17 at 09:03
  • The path of that excel file is unknown. user has to select the path using browse button. And i want to insert the each cell value to mysql db. – ViRaPa Aug 28 '17 at 09:04
  • In above code, you are directly accessing the hard coded file path. In my case user will select the file by using browse button – ViRaPa Aug 28 '17 at 09:06
  • you can use js or jquery ,and use input submit .then get file path, use this path in the java code. – wwj Sep 12 '17 at 10:49