How to export from datagridview to Excel?

46 views
Skip to first unread message

rubber

unread,
Apr 13, 2008, 3:23:53 PM4/13/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi,

I´m developing an application and I want some information in a
DataGridView. But i want to export that information into an Excel
sheet. I have the software MOffice 2003 installed in my computer. Is
it esay to resolve this problem?.
I will be very grateful if someone can explain me how to do it.
Thanks for all,
Bye

Andrew Badera

unread,
Apr 14, 2008, 12:39:07 PM4/14/08
to DotNetDe...@googlegroups.com
Unpossible.

Can't do it.

There certainly wouldn't be tens upon tens of thousands of results in Google telling you how to do just that, with a simple search.

Nope, just can't do it.


Translated: quit being a lazy asshat and go use Google.
--
--Andy Badera
http://andrew.badera.us/ http://flipbitsnotburgers.blogspot.com/
and...@badera.us
(518) 641-1280
Google me: http://www.google.com/search?q=andrew+badera

CK

unread,
Apr 14, 2008, 1:29:14 PM4/14/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
The easiest solution, is to highlight the datagrid, press CTRL-C, open
Excel, press CTRL-V.

Add formatting as required, with a pinch of salt.

srihari k

unread,
Apr 16, 2008, 6:17:46 AM4/16/08
to DotNetDe...@googlegroups.com
You need to write code to export data to excel sheet on click of export button, search in google , you will get code.
 
- Srihari.Patnaik

 

anna simookhin

unread,
Apr 17, 2008, 1:52:39 AM4/17/08
to DotNetDe...@googlegroups.com
one way:
at the first you should add "microsoft office 11.0 object library" via Add Refrence option if you want to have full feature excel sheet. but to have simple excel sheet it is not necessary like below sample.
this is sample procedure to export a table to excel file sheet. be care that the out put file is a "comma delimited" excel sheet.
 

public void ExportToExcel(DataSet dataSrc, string fileName)

{

//Add Response header

Response.Clear();

Response.AddHeader(

"content-disposition", string.Format("attachment;filename={0}.csv", fileName));

Response.Charset =

"";

Response.ContentType =

"application/vnd.xls";

//GET Data From Database

StringBuilder sb = new StringBuilder();

//Add Header

for (int count = 0; count < dataSrc.Tables[0].Columns.Count ; count++)

{

if (dataSrc.Tables[0].Columns[count].ColumnName != null)

sb.Append(dataSrc.Tables[0].Columns[count].ColumnName);

if (count < dataSrc.Tables[0].Columns.Count - 1)

{

sb.Append(

",");

}

}

Response.Write(sb.ToString() +

"\n");

Response.Flush();

//Append Data

for(int i = 0; i <dataSrc.Tables[0].Rows.Count;i++)

{

sb =

new StringBuilder();

for (int col = 0; col < dataSrc.Tables[0].Columns.Count ; col++)

{

string unit= dataSrc.Tables[0].Rows[i][col].ToString();

unit = unit.Replace(

",", " ");

sb.Append(unit);

sb.Append(

",");

}

// to have clear excel sheet.

sb.Replace(

"\n", " ");

sb.Replace(

"\r", " ");

sb.Replace(

"\t", " ");

Response.Write(sb.ToString() +

"\n");

Response.Flush();

}

Response.End();

}

Reply all
Reply to author
Forward
0 new messages