Your ID for both the DataGrid and the TextBox must be the same, notice, that
in the code sample, it has a textbox called txtfilename, which you can type
in the the xls filename you'd like. there is also a button, to actually do
the conversion process.
If you're not copying the entire code, make sure you make the changes
necessary, which match your page.
David Wier
http://aspnet101.com http://iWritePro.com - One click PDF, convert .doc/.rtf/.txt to HTML with no
bloated markup
"slinky" <ca***************@yahoo.comwrote in message
news:0d**********************************@z66g2000 hsc.googlegroups.com...
Thanks! I still have a rough edge or two in my code below the block:
Sub doExport(Source as Object, E as EventArgs)
RenderGridToExcelFormat(MyDataGrid,txtfilename.tex t)
End Sub
is giving compiler errors for both "MyDataGrid" and "txtfilename":
Compiler Error Message: BC30451: Name 'MyDataGrid' is not declared.
Source Error:
Line 6: <script language="VB" Runat="server">
Line 7: Sub doExport(Source as Object, E as EventArgs)
Line 8: RenderGridToExcelFormat(MyDataGrid,txtfilename.tex t)
Line 9: End Sub
Line 10:
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="MainDepartment.aspx.vb"
Inherits="Forsyth.HR_ReportingTool.UI.MainDepartme nt" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>MainDepartment</title>
<script language="VB" Runat="server">
Sub doExport(Source as Object, E as EventArgs)
RenderGridToExcelFormat(MyDataGrid,txtfilename.tex t)
End Sub
Sub RenderGridToExcelFormat(grid As DataGrid, saveAsFile As
String)
' Excel rows limit is 65536
If grid.Items.Count.ToString + 1 < 65536 Then
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.ContentType =
"application/vnd.ms-excel"
HttpContext.Current.Response.AddHeader("content-
disposition", "attachment;filename=" & saveAsFile & ".xls")
HttpContext.Current.Response.Charset = ""
grid.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
grid.RenderControl(hw)
HttpContext.Current.Response.Write(tw.ToString())
HttpContext.Current.Response.End()
Else
HttpContext.Current.Response.Write("Too many rows -
Export to Excel not possible")
End If
End Sub
</script>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<BODY background="file:///C:\Inetpub\wwwroot\HR_ReportingTool
\vignette.gif">
<div style="PADDING-RIGHT: 10px; PADDING-LEFT: 10px; FONT-SIZE:
medium; Z-INDEX: 101; BORDER-LEFT-COLOR: white; LEFT: 224px; FLOAT:
none; BORDER-BOTTOM-COLOR: white; MARGIN-LEFT: 20px; OVERFLOW: auto;
WIDTH: 736px; CLIP: rect(auto auto auto auto); COLOR: black; BORDER-
TOP-STYLE: inset; BORDER-TOP-COLOR: white; TEXT-INDENT: 5%; FONT-
FAMILY: 'Times New Roman'; BORDER-RIGHT-STYLE: inset; BORDER-LEFT-
STYLE: inset; POSITION: absolute; TOP: 100px; HEIGHT: 599px;
BACKGROUND-COLOR: white; TEXT-ALIGN: left; BORDER-RIGHT-COLOR: white;
BORDER-BOTTOM-STYLE: inset"
align="left">
<div style="POSITION: absolute">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="dgEmployees" style="Z-INDEX: 103; LEFT: -4px;
POSITION: absolute; TOP: 12px"
runat="server" ShowHeader="False" BorderColor="Silver"
AllowSorting="True" HorizontalAlign="Center"
BorderStyle="Solid" Height="136px" Width="640px">
<SelectedItemStyle Font-Underline="True" Font-Bold="True"></
SelectedItemStyle>
<AlternatingItemStyle BackColor="#C0FFC0"></
AlternatingItemStyle>
<Columns>
<asp:ButtonColumn Text="Details" ButtonType="PushButton"
CommandName="Details"></asp:ButtonColumn>
</Columns>
</asp:datagrid>
<asp:Button id="button1" Text="Export" onclick="doExport"
Runat="server" />
</form>
</div>
</div>
<asp:label id="EmpNum" style="Z-INDEX: 106; LEFT: 360px; POSITION:
absolute; TOP: 48px; TEXT-ALIGN: center"
runat="server" BorderStyle="Outset" Width="65px" Text="Manufacturer
Name" BackColor="LightSkyBlue"
Font-Bold="True">Emp #</asp:label><asp:label id="lblWelcome"
style="Z-INDEX: 105; LEFT: 8px; POSITION: absolute; TOP: 8px"
runat="server"
Height="40px" Width="216px" BackColor="Transparent" Font-
Bold="True" ForeColor="White" Visible="False">Hello</
asp:label><asp:label id="Label3" style="Z-INDEX: 104; LEFT: 648px;
POSITION: absolute; TOP: 48px; TEXT-ALIGN: center"
runat="server" BorderStyle="Outset" Width="278px" Text="Title"
BackColor="LightSkyBlue" Font-Bold="True">Title</asp:label><asp:label
id="Label1" style="Z-INDEX: 103; LEFT: 560px; POSITION: absolute; TOP:
48px; TEXT-ALIGN: center"
runat="server" BorderStyle="Outset" Width="88px" Text="First Name"
BackColor="LightSkyBlue" Font-Bold="True">First Name</
asp:label><asp:label id="Label2" style="Z-INDEX: 102; LEFT: 424px;
POSITION: absolute; TOP: 48px; TEXT-ALIGN: center"
runat="server" BorderStyle="Outset" Width="137px" Text="Last Name"
BackColor="LightSkyBlue" Font-Bold="True">Last Name</asp:label>
</BODY>
</HTML>
On Jun 13, 3:46 pm, "David Wier" <d...@dw.comwrote:
That's C# all right - however, it's very close to VB code - you can try
removing the semi-colons and see if it works.
Otherwise there's a code sample at ASPNet101 for
this:http://www.aspnet101.com/aspnet101/a...px?code=Export...
David Wierhttp://aspnet101.comhttp://iWritePro.com- One click PDF, convert
.doc/.rtf/.txt to HTML with no
bloated markup
"slinky" <campbellbrian2...@yahoo.comwrote in message
news:49**********************************@d77g2000 hsb.googlegroups.com...
I found the following code to transfer datagrid data to an Excel file.
Is this written in C#?... I'm a vb.netter. I'm just not sure where to
place the code to experiment on it. Should I place it in the event
handler for a form button. Is there other ways to accomplish this? I
looked at some on the web and usergroups, but was confused as to the
functioning. BTW my datagrid is enclosed in a <div></divfor ease of
scrolling if that would effect the solution.
Thanks! Brian
Response.Clear();
Response.AddHeader("content-disposition",
"attachment;filename=FileName.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.No Cache);
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();- Hide quoted text -
- Show quoted text -