C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » .NET »

how to transfer data from datagridview to excel sheet in windows application using C#?


Posted Date: 29 Nov 2008      Posted By: bulli guruvu setty      Member Level: Gold     Points: 1   Responses: 5



hi

i want to transfer data from datagridview to excel sheet in windows application using C#.net.

please help me the code...

very urgent...

Thank you in advance.





Responses

Author: Pavani    29 Nov 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 4

Check this code:

Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(htmlWrite);
GVReports.RenderControl(htmlWrite);
Response.Write(htmlWrite.ToString());

Regards,
Pavani



Author: Raja    29 Nov 2008Member Level: BronzeRating: 2 out of 52 out of 5     Points: 6

It works fine for me

try this.

protected void bnExcel_Click(object sender, EventArgs e)//uploaded into excel
{
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);
dg1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}



Author: Lalit Vasant Patil    29 Nov 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 6

Hi,

You can also try below code:

Protected Sub btnReport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReport.Click

Call BindGridView()

Try

If gvReport.Rows.Count > 0 Then

Dim Style As String = "<style> .text { mso-number-format:\@; } </style> "

Dim StyleDate As String = "<style> .text1 { mso-number-format:dd\-mm\-yyyy; } </style> "

Response.ClearContent()

Response.AddHeader("content-disposition", "attachment; filename=TestReports.xls")

Response.Charset = ""

Response.Cache.SetCacheability(HttpCacheability.NoCache)

Response.ContentType = "application/excel"

Dim sw As New IO.StringWriter()

Dim htw As New System.Web.UI.HtmlTextWriter(sw)

gvReport.BorderWidth = 1

gvReport.HeaderStyle.BorderStyle = BorderStyle.None

gvReport.HeaderStyle.BackColor = Drawing.Color.White

gvReport.GridLines = GridLines.Both

gvReport.FooterRow.Visible = False

gvReport.PagerSettings.Visible = False

gvReport.EnableSortingAndPagingCallbacks = False

gvReport.PagerSettings.Visible = False

lbHeading.RenderControl(htw)

gvReport.RenderControl(htw)

Response.Write(Style)

Response.Write(StyleDate)

Response.Write(sw.ToString())

Response.End()

Else

Exit Sub

End If

Catch ex As Exception

End Try

End Sub


-Happy Codding(-_-)



Author: dilip kumar    29 Nov 2008Member Level: SilverRating: 2 out of 52 out of 5     Points: 6

Private Sub btnExportExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportExcel.Click
Try
If ((dgList.Columns.Count = 0) Or (dgList.Rows.Count = 1)) Then 'It Check DataGridview Rows
MsgBox("No Data")
Exit Sub
End If
Dim excel As Microsoft.Office.Interop.Excel.ApplicationClass = New Microsoft.Office.Interop.Excel.ApplicationClass()
excel.Application.Workbooks.Add(True)

Dim iColIndex As Integer = 0
Dim Datacol As DataColumn
For Each Datacol In dtList.Columns
iColIndex = iColIndex + 1
excel.Cells(1, iColIndex) = Datacol.ColumnName
Next
Dim iRowIndex As Integer = 0
Dim row As DataRow
Dim Datacolo As DataColumn
For Each row In dtList.Rows
iRowIndex += 1
iColIndex = 0
For Each Datacolo In dtList.Columns
iColIndex += 1
excel.Cells(iRowIndex + 1, iColIndex) = row(Datacolo.ColumnName).ToString()
Next
Next
excel.Columns.AutoFit()
excel.Save("Sheet.xls")
excel.Workbooks.Close()
Catch ex As Exception
End Try
End Sub

Before this u must adding the refernce Add refernce ->Com->Microsoft Office 12.0 Object Librory


bye...



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : send email using smtpclient
Previous : Windows Application C#
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use