permaculture playing cards*
The moose likes Struts and the fly likes How to read and write excel file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to read and write excel file" Watch "How to read and write excel file" New topic
Author

How to read and write excel file

krishna prakash
Greenhorn

Joined: Apr 02, 2009
Posts: 7
Hi All,
I am uploading excel file through jsp,and getting that excel file and writing into another location iin Action class.
when I access that excel file in my browser displaying not formatted data.
Can you please look into the below code.and suggest me where i did mistake.

UploadDownloadFileForm uploadForm = (UploadDownloadFileForm) form;
InputStream stream = null;
OutputStream bos = null;
stream = uploadForm.getFileToUpload().getInputStream();

BufferedInputStream bis = new BufferedInputStream(stream,4096);

FileOutputStream fos = new FileOutputStream(path3);

BufferedOutputStream writer=new BufferedOutputStream(fos,4096);
byte[] buf=new byte[4096];
int byteRead;
while ((byteRead=bis.read(buf,0,4096))>=0) {
writer.write(buf,0,byteRead);
}


Thanks,
Krishna.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Krishna please Use Code Tags when you post a source code. That way your code looks formatted. Unformatted code is hard to read. You can add code tags by wrapping your code in [code] [/code] tags. You can edit your message using button and then add code tags to it.

when I access that excel file in my browser displaying not formatted data.

If you are deploying your application on a local machine, then why don't you go and take a look at the file directly from where you upload it (path3 in your code). If the file renders formatted there, then there's something wrong with the code which sends the file to the browser.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
krishna prakash
Greenhorn

Joined: Apr 02, 2009
Posts: 7
Yes, I have gone through my code,written correctly.
displaying wrong formatted.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42621
    
  65
That's because you don't Use Code Tags.

What do you mean by "displaying not formatted data"? How is the browser getting at the file data? Post the relevant download code. You can also attach a screenshot.


Ping & DNS - my free Android networking tools app
krishna prakash
Greenhorn

Joined: Apr 02, 2009
Posts: 7
when i open in broser displaying like in attached format...


Thanks,
Krishna
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Ulf Dittmer wrote:What do you mean by "displaying not formatted data"? How is the browser getting at the file data? Post the relevant download code. You can also attach a screenshot.

krishna, you didn't do any of the things in bold. Neither did you told us what exactly you mean by not formatted data. You can post a screenshot of your MS Excel screen for that. You also didn't show us the code which is sending the file to the browser. The code that you have shown us is for uploading the file. How is the browser getting the file from the server?? Did you write some code or is the browser requesting the file directly as a static content??
krishna prakash
Greenhorn

Joined: Apr 02, 2009
Posts: 7
My jsp:

</head>
<html:form action="/uploadform.do" enctype="multipart/form-data">
<body>
This is my JSP page. <br>
Please uploadfile :
<html:file property="fileToUpload" size="55"></html:file>
sumit <html:submit ></html:submit>
</body></html:form>

and my action class

String str =uploadformForm.getFileToUpload().getFileName();
System.out.println("name"+str);
stream= uploadformForm.getFileToUpload().getInputStream();
String destPath="C:/files/"+str; // this is am storing in unix box
BufferedInputStream bis = new BufferedInputStream(stream);
FileOutputStream fos = new FileOutputStream(destPath);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] buffer = new byte[4096];
int byteRead;
while ((byteRead=bis.read(buffer,0,4096))>=0) {
bos.write(buffer,0,byteRead);
}



am able to store inthe unix location it is also displaying,but when am open this file opening like in attached format.

iam simply getting the excel file from UI and storing in unix location,am opening also unix location.

...krishna
 
 
subject: How to read and write excel file