Click here to Skip to main content
13,092,310 members (55,457 online)
Rate this:
Please Sign up or sign in to vote.
I have implemented export dataset to excel using
:Excel template, which contains namerange where the data needs to be inserted
:FileStream and byte array

The problem m facing is, everytime i write data into excel, It also appends column header present in the datatable.
I want to avoid the column header, as this is already present in the excel template(row above nameranges)

Please provide me a solution.
Atleast let me know, how to get the content of HttpContext object in debug mode, so that i can do RnD with that.
Posted 10-Dec-12 17:48pm
Updated 17-Nov-16 5:42am
Vinay iGATE 11-Dec-12 22:25pm
I found out something in connection string as HDR.
Does anyone knows, whats the use of this?
HDR = YES / HDR = No
Phouttrat 22-Dec-15 5:56am
"HDR=Yes;" indicates that the first row contains columnnames, not data. "HDR=No;" indicates the opposite.
However it is possible that you'll also need to use "IMEX=1;", see the following link:

But another problem that you have is selecting the named range in your template file. With OLEDB you're unable to do that so as an alternative you may want to try out this excel's library for C#. It can enable you to export a DataTable into an excel file's named range.
Also it has a direct way of exporting excel file to browser in ASP.NET.

So your implementation steps would be
: Read excel template
: Get required NamedRange
: Insert DataTable on that NamedRange's position
: Stream excel to client's browser
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Article: Fast Exporting from DataSet to Excel[^]
Similar answer: How to Export Value from Data Set to Excel Sheet[^]
Lot more similar stuff on CodeProject[^]

In short:

for (int col = 0; col < dataTable.Columns.Count; col++)
    for (int row = 0; row < dataTable.Rows.Count; row++)
        excelSheet.Cells[row + 1, col + 1] =

..and for bulk data you can go with:
excelSheet.get_Range("A1:H25", Type.Missing).Value2 =
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try this I Hope it will solve your Issue

In .Aspx page
<asp:DataGrid ID="myGridView" Width="100%" runat="server" AutoGenerateColumns="False"
            DataKeyNames="Id" AlternatingRowStyle-CssClass="alt">
            <ItemStyle BackColor="#EBEBEB" />
            <AlternatingItemStyle BackColor="White" />
                <asp:TemplateColumn HeaderStyle-HorizontalAlign="Center" HeaderText="ID">
                    <ItemTemplate >
                        <asp:Label ID="lblId" runat="server" Text='<%# Eval("ID")%>'></asp:Label>
                    <ItemStyle HorizontalAlign="Center" Width="15%" />
            <asp:TemplateColumn ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" HeaderText="Name">
                        <asp:Label ID="lblName" Text='<%# Eval("Name")%>' runat="server"></asp:Label>
                    <ItemStyle HorizontalAlign="Center" Width="20%" />

In .cs page
               myGridView.DataSource = dd.tables[0];

               public void ExportToExcel(System.Web.UI.Control ctl)
                 using (Control myCtl = ctl)
                HttpContext.Current.Response.AppendHeader("Content-Disposition", attachment);
                HttpContext.Current.Response.Charset = charSet;
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                HttpContext.Current.Response.ContentType = content;
                myCtl.Page.EnableViewState = false;
                StringWriter sw = new StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);
               //Renders the control here.

Keep smiling :):):) Happy coding:)
Vinay iGATE 11-Dec-12 1:13am
I dont have datagridview. Data is coming directly from a table in a dataset :(
IpsitaMishra 11-Dec-12 6:32am
you can add a grid and make that invisible just a suggestion :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 3


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 17 Nov 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100