File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Uploading Excel spreadsheet into JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Uploading Excel spreadsheet into JSP " Watch "Uploading Excel spreadsheet into JSP " New topic

Uploading Excel spreadsheet into JSP

Satyendra Kumar

Joined: Nov 18, 2003
Posts: 2
Hi All,
I have a JSP form in which a user can enter values and these values are saved in a database (DB2 or Oracle).

I need to give an option where a user can simply give an excel file and still have the data saved to a database without having to retype the data.

How do I do this?
Balan Raj
Ranch Hand

Joined: Aug 26, 2003
Posts: 74
Its not very simple.
1. You need a servlet that supports uploading of files
2. the Excel file should be in a format you require or have headers using which u can make sense out of the data.
3. U need to support proper parsing & error handling
4. All this can be time-consuming, which might make u think whether u want ot do it asynchronously
Perry Cheong

Joined: Aug 22, 2004
Posts: 7
currently im having the same problem too. i cant seem to think of a solution.

as suggested by balam ram do i have to create a servlet to extract the information from the spreadsheet before dumping it into the jsp page? if so then for the servlet would i be using codes from the java excel api? cuz i remembered there was such an api.
Balan Raj
Ranch Hand

Joined: Aug 26, 2003
Posts: 74
I guess you will have to have the servlet/bean to do the data scrapping from excel. Cant you force your users to send a CSV file instead of an excel? Excel allows u to store data as CSV! that way, its more of a File IO operation !

If you are stuck with excel, then to handle excel files, you might want to think about jakarta's POI. Sorry I haven't used it personally.
Perry Cheong

Joined: Aug 22, 2004
Posts: 7
yup im happily stucked with it.....and whats more is that the cilent's project im doing gave me some new requirements with just 2 weeks to my term break......
Perry Cheong

Joined: Aug 22, 2004
Posts: 7
oh ya and btw anyone knows how to get jakarta poi setup on winXP cuz im currently lost
Neeraj Vij
Ranch Hand

Joined: Nov 25, 2003
Posts: 315

this is partial code for file upload I had written.. Hope it helps...

1.) JSP form look's like

<form action="neeraj.jsp" enctype="MULTIPART/FORM-DATA" method=post>
Select file to upload <input type="file" name="filename" />

<input type="submit" value="Upload" />

2.) neeraj.jsp looks like

<jsp:useBean id="neerajBean" scope="page" class="FileUploadBean " />

3.)UploadBean file is as follows:

public class FileUploadBean {

public void doUpload(HttpServletRequest request) throws
IOException {
PrintWriter pw = new PrintWriter(
new BufferedWriter(new FileWriter("FileUploaded.csv")));
ServletInputStream in = request.getInputStream();

int i =;
while (i != -1) {
pw.print((char) i);
i =;

Sagar J Jambhulkar

Joined: Sep 10, 2004
Posts: 8
We used this for a similar requirement.
People(Clients) are still obssesed with excel sheets

Love Work and Bless All and you will be blessed by God<br />-----Sadguru Wamanrao Pai
Sherryl Philip

Joined: Jul 02, 2004
Posts: 20
Firstly, I'd like to know whether you are using the Struts Framework. Well, in my application - I use the Struts framework, so I had the Action class and ActionForm class.

See, if you need to upload an excel file into the database, you can read the excel file using the Jakarta POI package. But before you start reading the file, you need to decide upon the structure of the Excel file. It should have the same number of columns.

For reading the Excel file,

InputStream stream = file.getInputStream();
POIFSFileSystem fs = new POIFSFileSystem((stream));
HSSFWorkbook wb = new HSSFWorkbook(fs);

for (int k = 0; k < wb.getNumberOfSheets(); k++) {
HSSFSheet sheet = wb.getSheetAt(k);
int rows = sheet.getPhysicalNumberOfRows();

int r = sheet.getFirstRowNum();

HSSFRow row = sheet.getRow(r);
int cells = row.getPhysicalNumberOfCells();

for (int c = 0; c < cells; c++) {
HSSFCell cell = row.getCell((short) c);
/* what ever operation is required ..... */

Hope this will help you to get started!!
All the Best!

Have a good day!

Perry Cheong

Joined: Aug 22, 2004
Posts: 7
thxs alot guys but the biggest joke of all is the client has yet to send me a sample copy of the template
I agree. Here's the link:
subject: Uploading Excel spreadsheet into JSP
It's not a secret anymore!