| Author: Pavani 29 Nov 2008 | Member Level: Gold | Rating:  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 2008 | Member Level: Bronze | Rating:  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 2008 | Member Level: Gold | Rating:  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 2008 | Member Level: Silver | Rating:  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...
|