Reputation: 145
I am trying export a datatable to excel on an asp.net machine without office interop, since I'm using telerik for asp.net, is there a way to use the radgrid's export to excel function on a datatable in code behind at runtime without creating a design radgrid in the page?
Upvotes: 0
Views: 2132
Reputation: 11154
Please try with the below code snippet.
protected void Page_Load(object sender, System.EventArgs e)
{
RadGrid1 = new RadGrid();
RadGrid1.ID = "RadGrid1";
RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
}
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Shipper", typeof(string));
dt.Columns.Add("ShipperTemp", typeof(string));
dt.Rows.Add("CShipper1", "1");
dt.Rows.Add("BShipper2", "2");
dt.Rows.Add("AShipper1", "1");
dt.Rows.Add("EShipper1", "2");
dt.Rows.Add("DShipper4", "4");
(sender as RadGrid).DataSource = dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
RadGrid1.Rebind();
Page.Controls.Add(RadGrid1); // It will not add control in your page
RadGrid1.MasterTableView.ExportToExcel();
}
Upvotes: 1