Pablo_B

hello
I have a problem, I want to make button,when i click them all my data in datagrid will be exported to xls file
I want it in vc#

I have found this article
http://www.codersource.net/published/view/283/exporting_data_grid_to_excel.aspx

private void button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
myDataGrid.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();

}


Error 1 The name 'Response' does not exist in the current context D:\LoginControl\Form3.cs


can You help me to do this function working


Re: .NET Framework Data Access and Storage problem with export data from datagrid to excel

Pablo_B

I cant find anything about this how to export from dataview in win form to excell
can anybody help me




Re: .NET Framework Data Access and Storage problem with export data from datagrid to excel

VMazur

This code will work only for ASP.NET application, because Response exists only in ASP.NET context. There is no direct way to do this in Windows applications, but there are several ways to accomplish it using your code

1. Use Jet OLEDB Provider. You need to create INSERT SQL statements and loop through the rows of the grid to insert records one\ by one

2. Use Microsoft Office Tools for .NET. This way is slower, but gives you more functionality and allows formatting of the data.

3. Write code that stores data into CSV or HTML. Those formats recognized by Excel.

4. Use third party components that could provide desired functionality

Following are some links wit the samples

http://support.microsoft.com/kb/306022/en-us

http://support.microsoft.com/kb/326548/en-us

http://support.microsoft.com/kb/307029/en-us