Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to read and write excel file

 
krishna prakash
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
krishna prakash
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I have gone through my code,written correctly.
displaying wrong formatted.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
krishna prakash
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i open in broser displaying like in attached format...


Thanks,
Krishna
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic