Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a web application in asp.net, wherein I have to generate a excel file when a button is clicked on the web page.The excel file also contains formulae at various cells, therefore I have used interop to create the excel file. The steps done by me are:
1. create required excel file using interop.
2. give it a random name and save it in some folder.
3. Set the navigate url of some hyperlink to this file.
4. Then user have to click the hyperlink to get the file.
5. the excel file stored in the folder is removed by a scheduler program.

Now, I want to implement this in a way so that the excel file is generated on the fly on the click of a button and send to the client browser as attachment.


Please suggest some method to do this in asp.net.
Posted

 
Share this answer
 
It probably better to use NPOI --> https://npoi.codeplex.com/[^]

This way you can generate a new excel file in memory and send it to a user without even saving it somewhere.
 
Share this answer
 
Please refer this link which might help you
Export Gridview to Excel in asp.net and c#[^]
 
Share this answer
 
You can do this by overriding the HttpResponse buffer of your aspx page.

First clear out the response buffer with Response.Clear()[^]

You an then either use Response.BinaryWrite()[^] to stream in a byte array, or Response.WriteFile()[^] if you would rather use a file that has already been written to a byte array.

You may also need to set the appropriate mime type with Response.ContentType[^].

Essentiall you shoul en up with something like this:
Response.Clear();
Response.WriteFile(FilePath);
Response.ContentType = "application/vnd.ms-excel"; // Check this, it might be wrong.


As a sidenote, you may run into issues with running your application on a server if you're using Interop. I did when I tried to use it a couple of years ago; I've used OleDB since then and it pretty much fits my needs, the only catch is that you can't do formatting with it. Here's an article if you want a brief introduction: Read and Write Excel Documents Using OLEDB[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900