Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: (untagged)
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 28-Oct-12 21:21pm
ketfos1391

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use the following code.its working for me..
   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 Smile | :) JMD
  Permalink  
v2
Comments
ketfos1 at 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 at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 29 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100