Monday, July 22, 2013

Export to excel in ZIP format

Problem
 
Exporting large file from Asp.Net pages cause huge bandwidth consumption, slow response and time-out problem. In most of application there are couple of pages
where we export large data file in to excel. And the most common problems are response time-out and  slow UI response.
 
Solution
 
There are two solutions for this problem.
 
1. Enable streaming for download.
 
2. Download file in Zip format.
 
 
Streaming
 
Streaming is considered to be the best solution for downloading. But it need a dedicated streaming service(WCF/SOAP) and lots of working hour to implement this.
Streaming does not support all file types like .xls, .txt etc.
 
Zip
 
The second solution is zip. Which can be applied with all file types. And compared to streaming, it is quite easy to implement with couple of lines of code.
Zip give us up to 95% compression for excel files. So we really need not to worry for file size up to 100 MB in case of excel.  
 
Now the question is which solution we should implement in our application. Here I must say Zip if it is not a video/audio.
 
Step 1
 
Put a button control in our .aspx page.
 
 
Step 2
 
Import DotNetZip library. You can download it from CodePlex.
It is an Open Source Library with Microsoft Public License (Ms-PL).
 
Step 3
 
Import Iconic.Zip namespace to your code behind.
 
 
Step 4
 
On button click write the following code.
This is all you need to do for download file in zip format.
 
Output