Morpheusweb.it - Risorse per webmaster: script, ASP.NET, C#, Visual Basic .Net, tutorial, asp, javascript, css, php, html, java, ADO, VBScript, forms, frames, Active Server Pages, Dynamic HTML, database, gratis per webmaster e webdesigner

Datagrid To Excel - << C# VERSION >>


Export a DataGrid do Excel file


<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Data" %>
<HTML>
<script language="VB" runat="server">
Dim MyConnection As OleDbConnection
Public Sub Page_Load(ByVal Sender As System.Object, ByVal E As System.EventArgs) Handles MyBase.Load
  MyConnection = New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source=" & Server.MapPath("database/utenti.mdb"))
  BindGrid("id")
End Sub

Sub BindGrid(ByVal SortField As String)
  Dim DS As DataSet
  Dim MyCommand As OleDbDataAdapter
  MyCommand = New OleDbDataAdapter("select * from utenti", MyConnection)
  DS = New DataSet
  MyCommand.Fill(DS, "utenti")
  Dim Source As DataView = DS.Tables("utenti").DefaultView
  Source.Sort = SortField
  MyDataGrid.DataSource = Source
  MyDataGrid.DataBind()
End Sub

Sub ToExcel(sender As Object, e As System.EventArgs)
  Response.ContentType = "application/vnd.ms-excel"
  Response.Charset = ""
  Me.EnableViewState = False
  Dim tw As New System.IO.StringWriter
  Dim hw As New System.Web.UI.HtmlTextWriter(tw)
  MyDataGrid.RenderControl(hw)
  Response.Write(tw.ToString())
  Response.End()
End Sub
</script>
<body>
<form runat="server" ID="Form1">
<asp:Button id="Button1" runat="server" Text="Excel" OnClick="ToExcel"></asp:Button>
<br>
<br>
<ASP:DataGrid id="MyDataGrid" runat="server" Width="700px" BackColor="White" BorderColor="#CCCCCC"
CellPadding="3" Font-Name="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd" BorderStyle="None"
BorderWidth="1px" Font-Names="Verdana" EnableViewState="False">
<ItemStyle ForeColor="#000066"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#006699"></HeaderStyle>
</ASP:DataGrid><BR>
</form>
</body>
</HTML>



Download Code...


Print Page


 








Page top

risorse per webmaster