Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello all
Actually i am exporting data from gridview to microsoft excell sheet
when i doing export data from grid view without keeping my page in




<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">








Course Name <asp:DropDownList ID="ddlCourseName" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlCourseName_SelectedIndexChanged">
<asp:ListItem Value="0" >- - Select - -
<asp:ListItem Value="1st-Year" >1st-Year
<asp:ListItem Value="2nd-Year" >2nd-Year
<asp:ListItem Value="3rd-Year" >3rd-Year
<asp:ListItem Value="4th-Year" >4th-Year

Course Year <asp:DropDownList ID="ddlCYear" runat="server" AutoPostBack="True">
<asp:ListItem Value="0" >- - Select - -
<asp:ListItem Value="1st-Year" >1st-Year
<asp:ListItem Value="2nd-Year" >2nd-Year
<asp:ListItem Value="3rd-Year" >3rd-Year
<asp:ListItem Value="4th-Year" >4th-Year

Session <asp:DropDownList ID="ddlSession" runat="server">
<asp:ListItem Value="--Select--">--Select--
<asp:ListItem Value="2013-2014">2013-2014
<asp:ListItem Value="2014-2015">2014-2015
<asp:ListItem Value="2015-2016">2015-2016
<asp:ListItem Value="2016-2017">2016-2017
<asp:ListItem Value="2017-2018">2017-2018
<asp:ListItem Value="2018-2019">2018-2019
<asp:ListItem Value="2019-2020">2019-2020

<asp:Label ID="lblsem" runat="server" Text="Semester">
<asp:DropDownList ID="ddlSemester" runat="server">
<asp:ListItem Value="Select">- - Select - -
<asp:ListItem Value="1st-Sem">1st-Sem
<asp:ListItem Value="2nd-Sem">2nd-Sem
<asp:ListItem Value="3rd-Sem">3rd-Sem
<asp:ListItem Value="4th-Sem">4th-Sem
<asp:ListItem Value="5th-Sem">5th-Sem
<asp:ListItem Value="6th-Sem">6th-Sem
<asp:ListItem Value="7th-Sem">7th-Sem
<asp:ListItem Value="8th-Sem">8th-Sem

    <asp:Label ID="lblMarkType" runat="server">
<asp:Label ID="lblType" runat="server" Text="Label" Visible="False">
   
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />








<asp:GridView runat="server" ID="Gview" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"
RowStyle-CssClass="GVRow" BorderWidth="1px" CellPadding="3" AutoGenerateColumns="False"
Width="100%" EmptyDataText="No Record Found" HorizontalAlign="Left"
CssClass="tablesorter" HeaderStyle-VerticalAlign="Middle" GridLines="Horizontal" OnRowDataBound="Gview_RowDataBound">
<rowstyle forecolor="#4A3C8C" backcolor="#E7E7FF">
<emptydatarowstyle bordercolor="#CCCCCC" borderstyle="Solid" borderwidth="1px" forecolor="Red">
Height="20px" HorizontalAlign="Center" VerticalAlign="Middle" Width="100%" />
<alternatingrowstyle backcolor="#F7F7F7">
<columns> <asp:TemplateField Visible="false">
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="2%" />
<itemstyle horizontalalign="Center" verticalalign="Top" width="2%">


<asp:TemplateField HeaderText="#No">
<itemtemplate>

<asp:Label ID="Label1" runat="server" Text='<%# Container.DisplayIndex+1 %>'>


<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="3%" />
<itemstyle horizontalalign="Left" verticalalign="Top" width="3%" cssclass="GridData">

<asp:TemplateField HeaderText="Enrollment">
<itemtemplate>
<%#Eval("Enrollment")%>

<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" CssClass="GridDataheader"
Width="15%" />
<itemstyle horizontalalign="Left" verticalalign="Top" width="15%" cssclass="GridData">
ForeColor="ActiveCaptionText" />


<asp:TemplateField HeaderText="Roll No">
<itemtemplate>
<asp:Label ID="lblRollNo" runat="server" Text='<%#Eval("RollNo")%>' CommandArgument='<%# Eval("RollNo") %>' >


<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" CssClass="GridDataheader"
Width="10%" />
<itemstyle horizontalalign="Left" verticalalign="Top" width="10%" cssclass="GridData">
ForeColor="ActiveCaptionText" />

<asp:TemplateField HeaderText="Course Year">
<itemtemplate>
<%#Eval("CourseYear")%>

<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" CssClass="GridDataheader"
Width="10%" />
<itemstyle horizontalalign="Left" verticalalign="Top" width="10%" cssclass="GridData">
ForeColor="ActiveCaptionText" />

<asp:TemplateField HeaderText="Session">
<itemtemplate>
<%#Eval("Session")%>

<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" CssClass="GridDataheader"
Width="10%" />
<itemstyle horizontalalign="Left" verticalalign="Top" width="10%" cssclass="GridData">
ForeColor="ActiveCaptionText" />

<asp:TemplateField HeaderText="Semester">
<itemtemplate>
<asp:Label ID="lblsemester" runat="server" Text='<%#Eval("Semester")%>'>

<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" CssClass="GridDataheader"
Width="10%" />
<itemstyle horizontalalign="Left" verticalalign="Top" width="10%" cssclass="GridData">
ForeColor="ActiveCaptionText" />


<%-- sub gridview--%>
<asp:TemplateField HeaderText="Marks Details">
<itemtemplate>
<asp:GridView ID="Gviewmarks" runat="server" ShowHeader="true" AutoGenerateColumns="false">
<columns>
<asp:TemplateField HeaderText="Subject Code">
<itemtemplate>
<%# Eval("SubjectCode") %>




<asp:TemplateField HeaderText="Subject Name">
<itemtemplate>
<%# Eval("SubjectName") %>



<asp:TemplateField HeaderText="Int. Obt Marks">
<itemtemplate>
<asp:Label ID="lbliobtainmark" runat="server" Text='<%# Eval("InternalObtainMarks") %>'>



<asp:TemplateField HeaderText="Int. Pass Marks">
<itemtemplate>
<asp:Label ID="lblipassmark" runat="server" Text='<%# Eval("InternalPassingMarks") %>'>



<asp:TemplateField HeaderText="Int. Total Marks">
<itemtemplate>
<asp:Label ID="lblitotmarks" runat="server" Text='<%# Eval("InternalTotalMarks") %>'>



<asp:TemplateField HeaderText="Obtain Marks">
<itemtemplate>
<%# Eval("ObtainedMarks") %>



<asp:TemplateField HeaderText="Passing Marks">
<itemtemplate>
<%# Eval("PassingMarks") %>



<asp:TemplateField HeaderText="Total Marks">
<itemtemplate>
<%# Eval("TotalMarks") %>






<%-- end--%>

<pagersettings nextpagetext="Next" previouspagetext="Previous">
<pagerstyle cssclass="GridData" height="30px" horizontalalign="Right" backcolor="#E7E7FF" forecolor="#4A3C8C">
<footerstyle backcolor="#B5C7DE" forecolor="#4A3C8C">
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" Height="40px" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" Height="40px" />
<sortedascendingcellstyle backcolor="#F4F4FD">
<sortedascendingheaderstyle backcolor="#5A4C9D">
<sorteddescendingcellstyle backcolor="#D8D8F0">
<sorteddescendingheaderstyle backcolor="#3E3277">





<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />


Then I can export data from grid view.

But ( Care Here) when i keep my page in update panel control ,I can not export data from grid view


<asp:UpdatePanel ID="updcontrol" runat="server">
<contenttemplate>




<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">








Course Name <asp:DropDownList ID="ddlCourseName" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlCourseName_SelectedIndexChanged">
<asp:ListItem Value="0" >- - Select - -
<asp:ListItem Value="1st-Year" >1st-Year
<asp:ListItem Value="2nd-Year" >2nd-Year
<asp:ListItem Value="3rd-Year" >3rd-Year
<asp:ListItem Value="4th-Year" >4th-Year

Course Year <asp:DropDownList ID="ddlCYear" runat="server" AutoPostBack="True">
<asp:ListItem Value="0" >- - Select - -
<asp:ListItem Value="1st-Year" >1st-Year
<asp:ListItem Value="2nd-Year" >2nd-Year
<asp:ListItem Value="3rd-Year" >3rd-Year
<asp:ListItem Value="4th-Year" >4th-Year

Session <asp:DropDownList ID="ddlSession" runat="server">
<asp:ListItem Value="--Select--">--Select--
<asp:ListItem Value="2013-2014">2013-2014
<asp:ListItem Value="2014-2015">2014-2015
<asp:ListItem Value="2015-2016">2015-2016
<asp:ListItem Value="2016-2017">2016-2017
<asp:ListItem Value="2017-2018">2017-2018
<asp:ListItem Value="2018-2019">2018-2019
<asp:ListItem Value="2019-2020">2019-2020

<asp:Label ID="lblsem" runat="server" Text="Semester">
<asp:DropDownList ID="ddlSemester" runat="server">
<asp:ListItem Value="Select">- - Select - -
<asp:ListItem Value="1st-Sem">1st-Sem
<asp:ListItem Value="2nd-Sem">2nd-Sem
<asp:ListItem Value="3rd-Sem">3rd-Sem
<asp:ListItem Value="4th-Sem">4th-Sem
<asp:ListItem Value="5th-Sem">5th-Sem
<asp:ListItem Value="6th-Sem">6th-Sem
<asp:ListItem Value="7th-Sem">7th-Sem
<asp:ListItem Value="8th-Sem">8th-Sem

    <asp:Label ID="lblMarkType" runat="server">
<asp:Label ID="lblType" runat="server" Text="Label" Visible="False">
   
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />








<asp:GridView runat="server" ID="Gview" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"
RowStyle-CssClass="GVRow" BorderWidth="1px" CellPadding="3" AutoGenerateColumns="False"
Width="100%" EmptyDataText="No Record Found" HorizontalAlign="Left"
CssClass="tablesorter" HeaderStyle-VerticalAlign="Middle" GridLines="Horizontal" OnRowDataBound="Gview_RowDataBound">
<rowstyle forecolor="#4A3C8C" backcolor="#E7E7FF">
<emptydatarowstyle bordercolor="#CCCCCC" borderstyle="Solid" borderwidth="1px" forecolor="Red">
Height="20px" HorizontalAlign="Center" VerticalAlign="Middle" Width="100%" />
<alternatingrowstyle backcolor="#F7F7F7">
<columns> <asp:TemplateField Visible="false">
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="2%" />
<itemstyle horizontalalign="Center" verticalalign="Top" width="2%">


<asp:TemplateField HeaderText="#No">
<itemtemplate>

<asp:Label ID="Label1" runat="server" Text='<%# Container.DisplayIndex+1 %>'>


<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="3%" />
<itemstyle horizontalalign="Left" verticalalign="Top" width="3%" cssclass="GridData">

<asp:TemplateField HeaderText="Enrollment">
<itemtemplate>
<%#Eval("Enrollment")%>

<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" CssClass="GridDataheader"
Width="15%" />
<itemstyle horizontalalign="Left" verticalalign="Top" width="15%" cssclass="GridData">
ForeColor="ActiveCaptionText" />


<asp:TemplateField HeaderText="Roll No">
<itemtemplate>
<asp:Label ID="lblRollNo" runat="server" Text='<%#Eval("RollNo")%>' CommandArgument='<%# Eval("RollNo") %>' >


<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" CssClass="GridDataheader"
Width="10%" />
<itemstyle horizontalalign="Left" verticalalign="Top" width="10%" cssclass="GridData">
ForeColor="ActiveCaptionText" />

<asp:TemplateField HeaderText="Course Year">
<itemtemplate>
<%#Eval("CourseYear")%>

<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" CssClass="GridDataheader"
Width="10%" />
<itemstyle horizontalalign="Left" verticalalign="Top" width="10%" cssclass="GridData">
ForeColor="ActiveCaptionText" />

<asp:TemplateField HeaderText="Session">
<itemtemplate>
<%#Eval("Session")%>

<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" CssClass="GridDataheader"
Width="10%" />
<itemstyle horizontalalign="Left" verticalalign="Top" width="10%" cssclass="GridData">
ForeColor="ActiveCaptionText" />

<asp:TemplateField HeaderText="Semester">
<itemtemplate>
<asp:Label ID="lblsemester" runat="server" Text='<%#Eval("Semester")%>'>

<HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" CssClass="GridDataheader"
Width="10%" />
<itemstyle horizontalalign="Left" verticalalign="Top" width="10%" cssclass="GridData">
ForeColor="ActiveCaptionText" />


<%-- sub gridview--%>
<asp:TemplateField HeaderText="Marks Details">
<itemtemplate>
<asp:GridView ID="Gviewmarks" runat="server" ShowHeader="true" AutoGenerateColumns="false">
<columns>
<asp:TemplateField HeaderText="Subject Code">
<itemtemplate>
<%# Eval("SubjectCode") %>




<asp:TemplateField HeaderText="Subject Name">
<itemtemplate>
<%# Eval("SubjectName") %>



<asp:TemplateField HeaderText="Int. Obt Marks">
<itemtemplate>
<asp:Label ID="lbliobtainmark" runat="server" Text='<%# Eval("InternalObtainMarks") %>'>



<asp:TemplateField HeaderText="Int. Pass Marks">
<itemtemplate>
<asp:Label ID="lblipassmark" runat="server" Text='<%# Eval("InternalPassingMarks") %>'>



<asp:TemplateField HeaderText="Int. Total Marks">
<itemtemplate>
<asp:Label ID="lblitotmarks" runat="server" Text='<%# Eval("InternalTotalMarks") %>'>



<asp:TemplateField HeaderText="Obtain Marks">
<itemtemplate>
<%# Eval("ObtainedMarks") %>



<asp:TemplateField HeaderText="Passing Marks">
<itemtemplate>
<%# Eval("PassingMarks") %>



<asp:TemplateField HeaderText="Total Marks">
<itemtemplate>
<%# Eval("TotalMarks") %>






<%-- end--%>

<pagersettings nextpagetext="Next" previouspagetext="Previous">
<pagerstyle cssclass="GridData" height="30px" horizontalalign="Right" backcolor="#E7E7FF" forecolor="#4A3C8C">
<footerstyle backcolor="#B5C7DE" forecolor="#4A3C8C">
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" Height="40px" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" Height="40px" />
<sortedascendingcellstyle backcolor="#F4F4FD">
<sortedascendingheaderstyle backcolor="#5A4C9D">
<sorteddescendingcellstyle backcolor="#D8D8F0">
<sorteddescendingheaderstyle backcolor="#3E3277">





<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />

Posted
Updated 1-Jun-15 21:02pm
v2
Comments
Sergey Alexandrovich Kryukov 2-Jun-15 2:04am    
Sorry, no relevant information, no advice.
If you want help, please explain it all in comprehensive detail.
—SA
KaushalJB 2-Jun-15 2:38am    
Can you update your question with what have you applied ?

using Excel = Microsoft.Office.Interop.Excel;

public void ExportExcel()
{
if (dataGridView1.DataSource == null)
{
MessageBox.Show("Sorry nothing to export into excel sheet..", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
int rowsTotal = 0;
int colsTotal = 0;
int I = 0;
int j = 0;
int iC = 0;
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
Excel.Application xlApp = new Excel.Application();

try
{
Excel.Workbook excelBook = xlApp.Workbooks.Add();
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelBook.Worksheets[1];
xlApp.Visible = true;

rowsTotal = dataGridView1.RowCount - 1;
colsTotal = dataGridView1.Columns.Count - 1;
var _with1 = excelWorksheet;
_with1.Cells.Select();
_with1.Cells.Delete();
for (iC = 0; iC <= colsTotal; iC++)
{
_with1.Cells[1, iC + 1].Value = dataGridView1.Columns[iC].HeaderText;
}
for (I = 0; I <= rowsTotal - 1; I++)
{
for (j = 0; j <= colsTotal; j++)
{
_with1.Cells[I + 2, j + 1].value = dataGridView1.Rows[I].Cells[j].Value;
}
}
_with1.Rows["1:1"].Font.FontStyle = "Bold";
_with1.Rows["1:1"].Font.Size = 12;

_with1.Cells.Columns.AutoFit();
_with1.Cells.Select();
_with1.Cells.EntireColumn.AutoFit();
_with1.Cells[1, 1].Select();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
//RELEASE ALLOACTED RESOURCES
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
xlApp = null;
}
}
 
Share this answer
 
Comments
Member 10038070 2-Jun-15 3:05am    
My COding are working only the problem is that when i keeps my page in update panel
I can not able to download the sheet
<div id="divTableContainer" runat="server">
<asp:GridView ID="GridView1" runat="server">


<asp:Button ID="btnExportExcel" runat="server" Text="Export Excel" OnClick="btnExportExcel_Click" />

C# Code----
C#
protected void btnExportExcel_Click(object sender, EventArgs e)
    {
        ExportToExcel();
    }
    protected void ExportToExcel()
    {
        Response.AppendHeader("content-disposition", "attachment;filename=DeathCertificate.xls");
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.ms-excel";
        this.EnableViewState = false;
        Response.Write(divTableContainer.InnerHtml);
        Response.End();
    }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900