473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

datagrid data to an Excel file

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();
Jun 27 '08 #1
6 2712
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...=ExportDGExcel

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: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();

Jun 27 '08 #2
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 &lt; 65536 Then
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.ContentType =
"application/vnd.ms-excel"
HttpContext.Current.Response.AddHeader("content-
disposition", "attachment;filename=" &amp; saveAsFile &amp; ".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">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<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/...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 -
Jun 27 '08 #3
I've documented VB code to export a datagrid to Excel here:
http://SteveOrr.net/articles/ExcelExport.aspx

And here's another technique you may find useful as well:
http://SteveOrr.net/articles/ExportPanel.aspx

--
I hope this helps,
Steve C. Orr,
MCSD, MVP, CSM, ASPInsider
http://SteveOrr.net
http://iPhonePlaza.net

"slinky" <ca***************@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();
Jun 27 '08 #4
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 &lt; 65536 Then
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.ContentType =
"application/vnd.ms-excel"
HttpContext.Current.Response.AddHeader("content-
disposition", "attachment;filename=" &amp; saveAsFile &amp; ".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">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<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 -

Jun 27 '08 #5
Here is the vb.NET translation of that code
============================
Response.lear()
Response.AddHeader("content-disposition","attachment;filename=FileName.xls")
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.No Cache)
Response.ContentType = "application/vnd.xls"
Dim StringWriter As New System.IO.StringWriter()
Dim HTMLWriter As New System.Web.UI.HTMLTextWriter(StringWriter)
myDataGrid.RenderControl(HTMLWriter())
Response.End
Response.Write(StringWriter.ToString)

--
Okoronkwo Chinedu
Crazy About Learning
"slinky" wrote:
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();
Jun 27 '08 #6
Got that fixed and the button and the textbox are displaying in the
<div></divOK (would like them to be though outside in the main part
of the page (possible?). The main problem is, and it may be because I
had to alter the code somewhat is my Excel file opens but has no data.
<script language="VB" Runat="server">

Sub doExport(Source as Object, E as EventArgs)
RenderGridToExcelFormat(dgEmployees,txtFileName.te xt)
End Sub

Sub RenderGridToExcelFormat(grid As DataGrid, saveAsFile As
String)********************
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)
HttpContext.Current.Response.Write(tw.ToString())
HttpContext.Current.Response.End()*******
End Sub
On Jun 13, 5:11*pm, "David Wier" <d...@dw.comwrote:
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 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: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.te xt)
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 &lt; 65536 Then
* * * * * * * * HttpContext.Current.Response.Clear()
* * * * * * * * HttpContext.Current.Response.ContentType =
"application/vnd.ms-excel"
* * * * * * * * HttpContext.Current.Response.AddHeader("content-
disposition", "attachment;filename=" &amp; saveAsFile &amp; ".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">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<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 -- Hide quoted text -

- Show quoted text -
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Faith | last post by:
Hello. I need to take a column from Excel(unknown amount of rows) that will be selected by the user and copy those cells. Then I will need to paste those cells into the first column in a Data...
3
by: EMW | last post by:
How can I put everything that is in a datagrid into a SQL server database (which should be readable by an PocketPC program)? There is probably a simple solution for it, but as always it is...
1
by: vinay | last post by:
I have read the excel file into the datagrid and modified some data. Now i want to update the excel file with the data. How do I do it. Please help me.
2
by: Steve Chatham | last post by:
I use the following code: Private Sub RbtnExport_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RbtnExport.SelectedIndexChanged Dim sFile As String =...
2
by: TM | last post by:
I have an Excel sheet where I setup my needed formatting, page settings, ect, and would like to take my data from a datagrid and paste it into the excel file and print the excel file. Any idea...
1
by: steve | last post by:
Hi, I have generated a datagrid with the following columns: | Date-Time | Station ID | Parameter1 | .... | Parameter 2| I would like to: Create an Excel file and preferably give the user...
6
by: Sridhar | last post by:
Hi, I have to export data inside the datagrid into an excel worksheet. I have to do this on button click event. I am trying to do like this. First get the DataTable from the datagrid and...
4
by: Frank | last post by:
Hello All, I ham using VS.NET 2003. Have followed instructions from http://gridviewguy.com/ArticleDetails.aspx?articleID=26 along with several other articles to no avail. I am pulling my hair...
1
by: Phokojoe | last post by:
I have a vb project that uses datagrid. I have made it in such a way that users will be able to extract data from sql database able to excel. I managed that, but the dataset does not go with the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.