Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using the following code to write output of datagrid into an excel file.

I am getting error message
"The name gvFiles cannot be use din the context"

System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
// Render grid view control.
gvFiles.RenderControl(htw);
// Write the rendered content to a file.
string renderedGridView = sw.ToString();
File.WriteAllText(@"C:\temp\ExportedFile.xlsx", renderedGridView);

I can see the output in the datagrid?
How do I save to Excel file in C#?
Posted

1 solution

Use the following code.its working for me..
C#
Response.Clear();
             Response.AddHeader("content-disposition", "attachment;filename=REPORTNAME.xls");
             Response.Charset = "iso-8859-2";
             Response.ContentType = "application/ms-excel";

             using (StringWriter StringWriter = new System.IO.StringWriter())
             {
                 using (HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter))
                 {
                     GridViewName.AllowPaging = false;
                     GridViewName.Attributes.AddAttributes(HtmlTextWriter);
                     GetUserTaskDetails();  //Get data for Grid view
                     GridViewName.RenderControl(HtmlTextWriter);
                   }
                 Response.Write(str);
                 Response.Write(StringWriter.ToString());
             }
             Response.End();
             GridViewName.AllowPaging = true;


Happy coding :-)JMD
 
Share this answer
 
v2
Comments
ketfos1 29-Oct-12 23:25pm    
I have added this code in the button click and I am getting error message
"The name Response does not exist in current context"
"The name GridView doesnot exisit in current context"
giri001 30-Oct-12 0:38am    
GridViewName is your GridName so replace it.and use proper assembly refrence as well,and if you still facing problem then please post your code,i will try to make it according your requirement.:-)JMD
ketfos1 30-Oct-12 1:34am    
Here is the code and error is " The name gvFiles do not exist in the current context".
I am getting this error while I render grid control statement.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;
using System.IO;
using System.Data.SqlClient;

namespace Oracleconnect1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}




string test1 = "Data Source=test;User Id=test;Password=test123;"; // C#
private void button1_Click(object sender, EventArgs e)
{

OracleConnection conn = new OracleConnection(test1);
{ conn.Open();
label1.Text = conn.State.ToString();
OracleCommand oc = conn.CreateCommand();
oc.CommandText = "SELECT * FROM county where rownum < 10 and policy_year =" + textBox1.Text ;
OracleDataReader reader = oc.ExecuteReader();
bindingSource1.DataSource = reader;
dataGridView1.DataSource = bindingSource1;
dataGridView1.BorderStyle = BorderStyle.Fixed3D;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
}

}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
// Render grid view control.
gvFiles.RenderControl(htw);
// Write the rendered removed to a file.
string renderedGridView = sw.ToString();
File.WriteAllText(@"C:\temp\ExportedFile.xlsx", renderedGridView);
}
giri001 30-Oct-12 4:52am    
just replace "gvfiles" with gridview name and its working fine.like <asp:GridView ID="grdUserTaskDetails" ...
where you are storing your datainto grid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900