How to upload data from an excel file to database using servlets? #Java Short Notes

Use third party API bundles like jexcelApi.
http://jexcelapi.sourceforge.net/.

---
create an XML file to define the structure of your excel file like how many columns, column names, corresponding database table/class column/variable name,

Create a Java class that can retrieve information from that xml file. You can use XPATH and DOM for the purpose. check http://www.ibm.com/developerworks/library/x-javaxpathapi.html for ideas

---
write an html form that use input type='file', the form action should point to the servlet that will collect data from the excel file and store it in the database. servlet will collect data from the input stream

-----------
in the servlet use the class that represents the xml file. to get information about the file that will help mapping the excel file data to the corresponding table column. You can also create a class representing the database table. From servlet extract data, create an object of table class type, and create a function that takes the object and inserts into the table.

---------------
How servlet will process the excel file:

jexcelAPI Workbook class represents the total workbook
use getsheet method of workbook class to get the sheet number 0,1,2....

use getCell method of worksheet to get a particular cell object in the excel file
use getContents on the cell object to get data in string type.

from the XML file having excel file structure, you can get number of columns in the file. So you can write a look that will read data row,column wise from the excel file

----
code to process excel file [in servlet]

Workbook workbook = Workbook.getWorkbook(new File("myfile.xls"));

Sheet sheet = workbook.getSheet(0);

Cell a1 = sheet.getCell(0,0);

Cell b2 = sheet.getCell(1,1);

Cell c2 = sheet.getCell(2,1);

String stringa1 = a1.getContents();

String stringb2 = b2.getContents();

String stringc2 = c2.getContents();

----------------------

check the following webpage, it provides file upload and extract data from input stream in servlet applications

http://jexcelapi.sourceforge.net/resources/faq/

From: http://sitestree.com/?p=4801
Categories:Java Short Notes
Tags:
Post Data:2011-07-14 06:40:06

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada

Leave a Reply