Go Back   CodingForums.com > :: Server side development > ASP.NET

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-17-2011, 04:43 AM   PM User | #1
newbb
New Coder

 
Join Date: Apr 2011
Posts: 19
Thanks: 2
Thanked 2 Times in 2 Posts
newbb is an unknown quantity at this point
Question Export data in excel

Hi programmers,

I need help on how i can export my data in excel using ASP.net. I already search for tutorials but it makes me more confused on how to do things out....
Can somebody give me a link in a simple tutorial?

Thanks!
newbb is offline   Reply With Quote
Old 05-18-2011, 11:03 AM   PM User | #2
dcwm
New Coder

 
Join Date: May 2009
Location: North west England
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
dcwm is an unknown quantity at this point
Exporting data to Excel

Your problem is not very well defined, but if you want to export the contents of a database table to an Excel worksheet then you can look at http://www.3bbb3.ru.com
dcwm is offline   Reply With Quote
Old 05-19-2011, 06:40 AM   PM User | #3
newbb
New Coder

 
Join Date: Apr 2011
Posts: 19
Thanks: 2
Thanked 2 Times in 2 Posts
newbb is an unknown quantity at this point
Hi dcwm,

Sorry for that. This is what I want to do:

1. I have a data grid where I have 3 columns. Let's name it colum1, colum2 and column3.
2. This data came from the database that is displayed in my web page.
3. There is a part in my page where you can export all the data in the datagrid to an excel file.

Problem:
I don't have any idea how to do it or if what i am thinking is possible.

By the way, thanks for the link I am still studying it. I be much careful next time in posting problems...

Last edited by newbb; 05-19-2011 at 06:44 AM..
newbb is offline   Reply With Quote
Old 05-24-2011, 08:39 AM   PM User | #4
newbb
New Coder

 
Join Date: Apr 2011
Posts: 19
Thanks: 2
Thanked 2 Times in 2 Posts
newbb is an unknown quantity at this point
hi dcwm!!

It is quite a hard lesson to learn and luckily I was able to export data from my datagrid to excel. For my first trial(it is just a simpel aspx page where in i create a datagrid where i display all the data that I want to export in excel), it all works fine. I was able to do save an excel file with all the data.

Know when I do the coding in my original page, an error occur after clicking the export button:

sys.webforums.PagerequestManagerParserErrorException
Details: error parsing near <div> <table cells'

this is my sample codes:
this is for my .aspx.cs
Quote:
protected void Button1_Click(object sender, EventArgs e)
{
PrepareGridViewForExport(GridView1);
ExportGridView();
}
private void ExportGridView()
{
string attachment = "attachment; filename=logs.xlsx";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
private void PrepareGridViewForExport(Control gv)
{
LinkButton lb = new LinkButton();
Literal l = new Literal();
string name = String.Empty;
for (int i = 0; i < gv.Controls.Count; i++)
{
if (gv.Controls[i].GetType() == typeof(LinkButton))
{
l.Text = (gv.Controls[i] as LinkButton).Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(DropDownList))
{
l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(CheckBox))
{
l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
if (gv.Controls[i].HasControls())
{
PrepareGridViewForExport(gv.Controls[i]);
}
}
}
this is from my .aspx page
Quote:
<%@ Page Language="c#" MasterPageFile="~/Master.master" AutoEventWireup="false" CodeFile="systemlogs.aspx.cs" Inherits="DeleteConfirm" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div id="bodycontent" class="divStyl">
<div class="divfloatloading" style="left: 407px; top: 179px">
<AjaxExtension:UpdateProgress id="UpdateProgress1" runat="server">
<progresstemplate>
<div>
<img src="images/loading/loading2.gif" alt="" width="40" height="40" />
</div>
</progresstemplate>
</AjaxExtension:UpdateProgress>
</div>
<AjaxExtension:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table class="tbl_titlebar_border">
<tr>
<td class="titlebar">
System Logs
</td>
</tr>
<tr>
<td class="data_row_height">
<div>
<table>
<tr>
<td>
Date from
<asp:TextBox ID="txtDateAppliedFrom" runat="server" Font-Names="Tahoma" Font-Size="8pt" Width="85px"></asp:TextBox>
<a id="anchor2" onclick="l_date.select('ctl00_ContentPlaceHolder1_txtDateAppliedFrom','anchor2','MM/dd/yyyy'); return false;" href="javascript:void(0);"><img src="images/icons/calendar.gif" style="height:16; border:0" alt="" />
</a>
&nbsp;&nbsp;&nbsp; to &nbsp;&nbsp;&nbsp;
<asp:TextBox ID="txtDateApplied" runat="server" Font-Names="Tahoma" Font-Size="8pt" Width="85px"></asp:TextBox>
<a id="anchor1" onclick="l_date.select('ctl00_ContentPlaceHolder1_txtDateApplied','anchor1','MM/dd/yyyy'); return false;" href="javascript:void(0);"><img src="images/icons/calendar.gif" style="width:16; height:16; border:0" alt="" />
</a>
&nbsp;&nbsp;<asp:ImageButton ID="ImgBtnGo" runat="server" ImageUrl="../petc_lts/images/icons/go.gif" />
<asp:Button>
<br/>
Click here to <a target="_blank" href="systemlogs.aspx"><u>export</u></a> to excel.
</td>
</tr>
<tr>
<td style="width: 100px">
<div id="tbl-container" style="height: 233px">
<strong><span style="font-size: small; font-family: Arial; text-decoration: underline">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Export To Excel" /></span></strong><br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="usr"
DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display." style="font-size: small; font-family: Arial" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical">
<Columns>
<asp:BoundField DataField="dt" HeaderText="Date" SortExpression="dt" />
<asp:BoundField DataField="usr" HeaderText="User badge number" SortExpression="usr" />
<asp:BoundField DataField="trans" HeaderText="Transaction" SortExpression="trans" />
</Columns>
<FooterStyle BackColor="#CCCC99" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStringsetc_ltsConnectionString %>"
SelectCommand="SELECT * from logs">
</asp:SqlDataSource>
<br />
</div>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</ContentTemplate>
</AjaxExtension:UpdatePanel>
</div>
</asp:Content>

do you have any idea, why this error occur?
newbb is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:42 AM.


Advertisement
Log in to turn off these ads.