asp .net create excel file with couple lines of code

I got very simple asp.net project. The task was to display gridview with data below button to download data in excel file than file upload so user can upload updated data and store it in sql database. So I use several different methods to create excel file. Here is the best way to do it. You need to download excellibrary.dll copy (you can download it from here http://code.google.com/p/excellibrary/ ) it to project bin folder add reference to it. Below code snippet how to create new excel file and download.

Imports ExcelLibrary.SpreadSheet 

context.Response.Clear()

context.Response.Charset = “”

context.Response.AddHeader(“content-disposition”, “attachment;filename=Sample2.xls”)

context.Response.ContentType = “application/vnd.ms-excel”

Dim file As String = context.Server.MapPath(“”) & “/sample2.xls”

Dim workbook As New Workbook()

Dim worksheet As New Worksheet(“Sheet1″)

 worksheet.Cells(0, 1) = New Cell(“FullName”)

 worksheet.Cells(0, 2) = New Cell(“Title”)

 worksheet.Cells(0, 3) = New Cell(“Location”)

workbook.Worksheets.Add(worksheet)

workbook.Save(file)

context.Response.TransmitFile(“sample2.xls”)

This entry was posted in .Net development and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>