473,385 Members | 1,357 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.

Export Grid View to Excel

I have a Grid View and I want to expoet into Excel. I am using a hyperlink
to export the GridView intop Excel.

But when I click on link it just shows me java script. It doesn't open
Excel sheet. Here is my Code.

In HTML

<p><asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="javascript:alert('Unable to process request');">Send to
Excel</asp:HyperLink></p>

In Code behind file

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Request.QueryString["m"]) &&
Request.QueryString["m"] == "xls")
{
isExportToExcel = true;
sendResultsToExcel();
}
}

}

protected void sendResultsToExcel()
{
// setup Grid View
gdExceptions.AllowPaging = false;
gdExceptions.ShowFooter = false;
gdExceptions.ShowResultSummary = false;
gdExceptions.Columns[3].Visible = false; // hide link

gdExceptions.DataBind();
// export to excel
Response.Clear();
Response.Buffer = true;

Response.AddHeader("content-disposition",
"attachment;filename=P3Exceptions.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";

this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new
System.Web.UI.HtmlTextWriter(oStringWriter);
this.ClearControls(gdExceptions);

oHtmlTextWriter.WriteLine("<style>.text { mso-number-format:\\@; }
</style>");
gdExceptions.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
Oct 12 '07 #1
1 4964
Check out this sample:

http://www.tipsdotnet.com/ArticleDet...subarea=ASPnet

It works fine, if you need additional control over the excel generated, you
can use XMLSS, Excel Services, more info about this:

http://www.tipsdotnet.com/ArticleBlo...el&PageIndex=0

Good luck
Braulio

/// ------------------------------
/// Braulio Diez
///
/// http://www.tipsdotnet.com
/// ------------------------------


"bobby" wrote:
I have a Grid View and I want to expoet into Excel. I am using a hyperlink
to export the GridView intop Excel.

But when I click on link it just shows me java script. It doesn't open
Excel sheet. Here is my Code.

In HTML

<p><asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="javascript:alert('Unable to process request');">Send to
Excel</asp:HyperLink></p>

In Code behind file

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Request.QueryString["m"]) &&
Request.QueryString["m"] == "xls")
{
isExportToExcel = true;
sendResultsToExcel();
}
}

}

protected void sendResultsToExcel()
{
// setup Grid View
gdExceptions.AllowPaging = false;
gdExceptions.ShowFooter = false;
gdExceptions.ShowResultSummary = false;
gdExceptions.Columns[3].Visible = false; // hide link

gdExceptions.DataBind();
// export to excel
Response.Clear();
Response.Buffer = true;

Response.AddHeader("content-disposition",
"attachment;filename=P3Exceptions.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";

this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new
System.Web.UI.HtmlTextWriter(oStringWriter);
this.ClearControls(gdExceptions);

oHtmlTextWriter.WriteLine("<style>.text { mso-number-format:\\@; }
</style>");
gdExceptions.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}

Oct 13 '07 #2

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

Similar topics

0
by: sunilkumar Reddy via DotNetMonster.com | last post by:
hai all first we can take binded grid Export to excel data from ultrawebgrid (or) datagrid rows, Each row data Expoting to one worksheet in excel workbook, like as grid row1 data contain sheet1...
0
by: sunilkumar Reddy via DotNetMonster.com | last post by:
hai all first we have taken binded grid Export to excel data from ultrawebgrid (or) datagrid rows, Each row data Expoting to one worksheet in excel workbook, like as grid row1 data contain...
3
by: Russ | last post by:
I have a usercontrol that is loaded by a webform. The usercontrol populates a datagrid which users need the capability to export data from the grid to Excel. The problem is that when I attempt to...
0
by: kvicky | last post by:
Is it possible to export an Image from a Grid view to excel? If so, can anybody please tell me how to do that? Thanks K
1
by: huseinv | last post by:
Hi. I have a search tool which gives data in listing format and grid format (based on selection) on the same asp page. If a user select grid, the result is listed in grid format. Now how i can...
1
by: Randy | last post by:
I have an application with a datagrid and a button to export the grid to excel. For the most part this is working fine. Here's the export method: Private Sub btnExcel_Click(ByVal sender As...
1
by: JawzX01 | last post by:
Hello All, First, thank you for any help you can provide. I'm trying to do a simple export to excel. I've used the classic code that is all over the internet, and of course it worked without a...
2
by: veenna | last post by:
hello, I writen the code in asp.net using vb for export the grid view to excel. but while executing i am getting the error "Control 'GridView1' of type 'GridView' must be placed inside a form tag...
4
by: hangar18 | last post by:
Hi, We are exporting data from a grid view to excel using the standard Response.Content type etc... But we would like to create new worksheets in case there are more than 65000 rows. I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.