.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

Export a Query to Gridview then Excel

Posted By:      Posted Date: October 01, 2010    Points: 0   Category :ASP.Net

I have not done this before so pardin this question that I am sure has been asked before.

I have a user that asks me to run queries each month and copy & paste the data into a spreadsheet

I want to set up the queries in a web page for him to choose, enter the data in the parameters

and either show the data in a gridview then export or just export to the spreadsheet

direclty to the file name and location of his choice.

Using C#

Links or code samples would great.

View Complete Post

More Related Resource Links

Export Gridview data to Excel sheet


Hi ,

     I have a requirement where i need to export the gridview data to excel.My code export the data in excel that was fine.

But the enchancement needed now , I was exporting the 3 gridview in same excel sheet under the workbook.

Now i need to export the 3 grid view in 3 different sheet in the same workbook.

so can please tell or share the updated code in my below code ...

public void writeovrsummary()


        Response.Write("Overall Summary");
        //Response.Write("Report as of " + DateTime.Now.ToString("hh:mm ss tt"));

        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        SqlCommand com = MyConnection.CreateCommand();
        com.CommandText = "useroverall_Report";
        SqlParameter[] par = new SqlParameter[1];
        par[0] = new

Export altered gridview to Excel


Hello... stuck on this one!!  In a gridview RowDataBound named gvwResult I am altering one of the cells with a value retrieved from the query.

I have added:

if (hdnExchangeTo.Value.ToString() != "1")
            for (int i =0; i < gvwResult.Rows.Count; i++)
                gvwResult.Rows[i].Cells[7].Text = ExchangeCurrency(gvwResult.Rows[i].Cells[7].Text.ToString());

to the common code to export to Excel:

        Response.Buffer = true;
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        System.IO.StringWriter sw = new Syste

String Data missing start from "+" When Export Data To Excel Using Gridview

Hi,I am exporting data from gridview to excel file.My problem is that for all the string which contain "+", the rest of data begining from it is missing in excel file.For example: PWE-WER+78, when exported it will become PWE-WER.Below is my code.                Response.ClearContent() Response.Buffer = True Response.AddHeader("Content-Disposition", "attachment;filename=" & "ExportedExcel.xls") Response.ContentType = "application/vnd.ms-excel" Response.Charset = "" lobjStringWriter = New StringWriter lobjHtmlWriter = New HtmlTextWriter(lobjStringWriter) dgdResult.RenderControl(lobjHtmlWriter) Response.Write(lobjStringWriter.ToString()) Response.End() I  have search through online but no solution.Does anyone has idea on this?

How to export gridview to excel template

I have a gridview that I need to export to Excel. Excel is not installed on the server, but I would like to put a template on the server so that when the user exports the data it will use the template and open the file on the user's local machine. To make things slightly more complex, I need to export the data to Sheet2 because Sheet1 needs to be a graph (thus the need for the template). I cannot use any third party software and I have no ability to modify anything on the server. This is used in a corporate environment on a shared server that I don't have any control over. Any sample code would be very helpful. I'm running ASP.NET 3.5 with Visual Studio 2008.

ASP.NET-Gridview - Export to excel- column format

Hi All, I am exporting gridview data into excel sheet. I have a column  which is a code for which data is like 012,013 etc. But when this data is being exported , in the excel sheet I can see the data as 12,13,14. but actually it should be 012.... Is there any way to specify the format of such columns in my datatable so that they are in the same manner as in the database? Please provide some information on this.Thanks in advance.

gridview export to excel


The export to excel work successfully, but a name like this D'Costa is converted to Dâ?TCosta.

Any name with the tick converts within excel. Is there a fix?



Export GridView to Excel using .net 2010 (C#)


Can anyone tell me how to export gridview to excel using asp.net 2010? C# is the script language

what i have is working in VS2008 but not working in VS2010:

 public override void VerifyRenderingInServerForm(Control control)

after clicking the button:





Response.Charset = "" ;

Response.ContentEncoding = System.Text.Encoding .UTF8;

Response.Charset = "" ;

export the gridview data to excel problem



i am using master page and child pages in the child page

i want to export the gridview data to excel sheet for this i use the following code


        Response.AddHeader("content-disposition", "attachment; filename=shedules.xls");

        Response.ContentType = "application/excel";

        StringWriter sw = new StringWriter();

        HtmlTextWriter htw = new HtmlTextWriter(sw);




but in the gridShedules.RenderControl(htw) i got the following error but the form tag with  runat= server is already in the master page how to solve this issue

error:Control 'ctl00_ContentPlaceHolder1_grdshedules' of type 'GridView' must be placed inside a form tag with runat=server.

Error when trying to export gridview to Excel - RegisterForEventValidation can only be called during

I have a pretty basic vb.net page that has textbox control on it and a "generate" button that when clicked, fills a datagrid.  That all works great, but when the user clicks the "Export to Excel" button, I get this error.. RegisterForEventValidation can only be called during Render(); 

Here's my code behind, I thought I included everything I needed...

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub

Private Sub ExportToExcel(ByVal strFileName As String, ByVal dg As GridView)
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("content-disposition", "attachment;filename=" & strFileName)
        Response.Charset = ""
        Me.EnableViewState = False
        Dim StringWriter As New System.IO.StringWriter()
        Dim HtmlTextWriter As New System.Web.UI.HtmlTextWriter(Str

How to export data to excel from gridview and DataTable using asp.net?

In this article we will see how to export data from gridview control and dataTable to excel file in a simple way.

Export Visio Database Table Names to Excel

If you use the Enterprise Architect edition of Microsoft Visio for data modeling regularly, then there is a good chance that at some point you've wanted to export just the table names into Excel. You might want to do this to map logical ERD entities to physical data model tables, track project status by entity, or track overlap between database versions.

display data into GridView from excel sheet

I have to display data from excel sheet into my gridview and also I need to figure out a way to store data into excel files from my web page. I am using C# 3.5, VS 2008.

Export SharePoint List to Excel Spreadsheet Programmatically using C#

In SharePoint applications, Custom Lists are used to store business data and Document Libraries to store the documents. But for data manupulation and analysis, Microsoft Excel provides very rich features as compared to SharePoint Lists. That's why people still loves to work on Microsoft Excel Sheets.

Gridview that exports selected rows to Excel

his seems like a messy workaround, but I ended up using two Gridviews - one to display my data and show my checkboxes, and the other which was invisible amd would eventually end up as my Excel spreadsheet.

Export Dataset to excel sheet in a required format?



I know how to export a grid view to excel.

But here i have a Dataset which consists of data like below and also it need to display in a required format like below.

pno psubno pstate pdetailid pdetails

 12    1          CA         1           cbsdjcbdjc     

12     1          CA          2        cndcnjdcjcj

12     2           VA         4          xcndwkfcgygry

13     1            AL        1           ncdcnjdcndj

My excel sheet  should like this.

pid       pstate     

 12/1      CA             &nb

Excel-like experience - gridview focus problem



I'm creating a page where people can order several items from a list without having to press "edit" and "save" in between like you normally would in a gridview. The user writes down the quantity, and finally presses a "submit" button that saves the order.

I want the user to have a windows form (or excel) like eperience, so once the user puts in the quantity and moves on to the next item, either by TAB or by clicking on the TextBox, i make some calculations without the user beeing bothered with a full refresh or having to press a buton to execute the event, so I place the controls inside an UpdatePanel and use TextBox.TextChanged. The event runs code that calculates the sum for the row (price * quantity) and shows that in a column called "Sum", and updates a label in a fixed position with the new order total ("Total").

Any ideas on how to achieve this?

Right now I use a GridView with a templatefield for the Quantity and set the TextBox.TextChanged event to fire to make the calculations. The problem is that the focus then changes.

By using SmartNavigation or the ScriptManager.SetFocus I manage to retain focus on the TextBox that executed the Postback, but the user might have moved on to a textbox far down the gridview (thus causing the postback by leaving the previo

export to excel issue


Hello ,

I have a browser enabled form which i published to sharepoint ..now i want to export the contents of the form to excel..i have 1 repeating table and two repeating sections ..whenever i go to export to excel and then choose just one repeating table or a repeating section or any option other than export all form data i get an error ...first the progress bar just freezes and then when i end the task through task m anager i get a msg that microsoft excel could not start.make sure excel is installed correctly and then try again....

Can somebody tell me how to debug this..i have tried the export of a form containg repeating table and sections and it works good ..so that means there is some problem with my form..so now can somebody give me an idea as to what can the problem be and how can i debug it.


ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend