This is Mariyappan.I want export to Excel in asp.net v3.5.My problem is export textbox vale.
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Ritesh DiyawarPosted Jun 2, 2010, 6:31 AM
hi Mariyappan...
this code may be usefule for u------------------------------------------------------------------code start-----------------------------------------
HtmlForm htmlfrom = new HtmlForm();
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
tbl.Parent.Controls.Add(htmlfrom);
htmlfrom.Controls.Add(tbl);
htmlWrite.BeginRender();
htmlfrom.RenderControl(htmlWrite);
htmlWrite.BeginRender();
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + name + ".xls");
Response.Write(stringWrite.ToString());
Response.End();
------------------------------------------------------------------code end-----------------------------------------
Ritesh Diyawar
PeroPosted Jun 2, 2010, 5:45 AM
you can try this Excel .NET library.
Here is a code snippet how to export DataGrid to Excel in Excel ASP.NET application:
// Get DataTable that DataGrid is bound to.
var dataTable = (DataTable)dataGrid.DataSource;
// Create new ExcelFile.
var ef = new ExcelFile();
// Add new worksheet to the file.
var ws = ef.Worksheets.Add(dataTable.TableName);
// Insert the data from DataTable to the worksheet starting at cell "A1".
ws.InsertDataTable(dataTable, "A1", true);
// Stream file to browser.
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.xls");
ef.SaveXls(Response.OutputStream);
Response.End();