views:

4061

answers:

5

How to create and download excel document using asp.net ?

The purpose is to use xml, linq or whatever to send an excel document to a customer via a browser.

Edit : Use case

The customer load a gridview ( made with ajax framework ) in a browser, the gridview is directly linked to an sql database. I put a button 'export to excel' to let customer save this gridview data on his computer ansd i would like to launch a clean download of an excel.

The solutions proposed here are not clean, like send an html document and change the header to excel document etc, i'm searching a simple solution on codeplex right now, i will let you know.

+1  A: 

Kindly refer to the following good post:

Reading and Writing Excel Spreadsheets Using ADO.NET C# DbProviderFactory

http://davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx

Billy
+1  A: 

just set Response.ContentType = "application/vnd.ms-excel" and your page will rendered as an excel sheet on the clients browser

Sample code here

Binoj Antony
but for simplicity's sake, output any data you want in excel's "grid" in html tables
Neil N
A: 

There are quite a few ways of handling this, depending on how extensive the Excel functionality is. Binoj's answer works if the Excel is just a spreadsheet and has no direct Excel functionality built in. The client can add functionality, concats, etc. These are "dumb" excel docs until the client does soemthing.

To create a more full featured Excel doc, you havve two basic choices that I can think of offhand.

  1. Use either the office components (re: bad) to create an excel document, or a third party component, like SoftArtisan's ExcelWriter. Great component, but there is a cost.

  2. Use a control on the page that allows export to Excel. Most vendors of ASSP.NET controls have this functionality on their grids.

Option #1 allows you pretty much all functionality of Excel. Option #2 is a bit more limited, at least in the controls I have tried.

Gregory A Beamer
+4  A: 
belaz