我正在将网格视图导出为ex​​cel但不能正常工作。详见...... [英] I am exporting grid view to excel but not working. see in details...

查看:60
本文介绍了我正在将网格视图导出为ex​​cel但不能正常工作。详见......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好所有

实际上我将数据从gridview导出到microsoft excell sheet

当我从网格视图导出数据而不保留我的页面

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 =ToolkitScriptManager1runat =server>

















< tr> < td>课程年度
课程名称 < asp:DropDownList ID =ddlCourseNamerunat =serverAutoPostBack =TrueOnSelectedIndexChanged =ddlCourseName _SelectedIndexChanged>

< asp:ListItem Value =0> - - 选择 - -

< asp:ListItem Value =1st-Year> ;第一年

< asp:ListItem值=第二年>第二年

< asp:ListItem Value =3rd-Year> ; 3年级

< asp:ListItem值=第4年>第4年



< asp:DropDownList ID =ddlCYearrunat =serverAutoPostBack =True>

< asp:ListItem Value =0 > - - 选择 - -

< asp:ListItem Value =1st-Year> 1st-year

< asp:ListItem Value =2nd - 年>第二年

< asp:ListItem Value =3rd-Year> 3rd-year

< asp:ListItem Value =4th-Year> 4th-



会话 < asp:DropDownList ID =ddlSessionrunat =服务器>

< asp:ListItem Value = - 选择 - > - 选择 -

< 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:标签ID =lblsemrunat =serverText =Semester>

< asp:DropDownList ID =ddlSemesterrunat =server>

< asp:ListItem Value =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 =lblTyperunat =serverText =LabelVisible =False>

   
< asp:按钮ID =btnSearchrunat =serverText =SearchOnClick =btnSearch_Click/>




<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 =serverID =GviewBackColor =WhiteBorderColor =#E7E7FFBorderStyle =None

RowStyle-CssClass = GVRowBorderWidth =1pxCellPadding =3AutoGenerateColumns =False

Width =100%EmptyDataText =No Record foundHorizo​​ntalAlign =Left

CssClass =tablesorterHeaderStyle-VerticalAlign =MiddleGridLines =Horizo​​ntalOnRowDataBound =Gview_RowDataBound>

< rowstyle forecolor =#4A3C8Cbackcolor =#E7E7FF> ;

< emptydatarowstyle bordercolor =#C CCCCCborderstyle =Solidborderwidth =1pxforecolor =Red>

Height =20pxHorizo​​ntalAlign =CenterVerticalAlign =MiddleWidth =100%/> ;

< alternatingrowstyle backcolor =#F7F7F7>

< columns> < asp:TemplateField Visible =false>

< HeaderStyle Horizo​​ntalAlign =CenterVerticalAlign =MiddleWidth =2%/>

< itemstyle horizo​​ntalalign =Centerverticalalign =Topwidth =2%>





< asp:TemplateField HeaderText =#No>

< itemtemplate>



< asp:Label ID =Label1runat =serverText ='<%#Container.DisplayIndex + 1%>'>





< HeaderStyle Horizo​​ntalAlign =CenterVerticalAlign =MiddleWidth =3%/>

< itemstyle horizo​​nta lalign =Leftverticalalign =Topwidth =3%cssclass =GridData>



< asp:TemplateField HeaderText =Enrollment> ;

< itemtemplate>

<%#Eval(Enrollment)%>



< HeaderStyle Horizo​​ntalAlign =LeftVerticalAlign =MiddleCssClass =GridDataheader

Width =15%/>

< itemstyle horizo​​ntalalign =Left verticalalign =Topwidth =15%cssclass =GridData>

ForeColor =ActiveCaptionText/>





< asp:TemplateField HeaderText =Roll否>

< itemtemplate>

< asp:Label ID =lblRollNorunat =serverText ='<%#Eval(RollNo )%>'CommandArgument ='<%#Eval(RollNo)%>'>





< ; HeaderStyle Horizo​​ntalAlign =LeftVerticalAlign =MiddleCssClass =GridDataheader

Width =10%/>

< itemstyle horizo​​ntalalign =Left verticalalign =Topwidth =10%cssclass =GridData>

ForeColor =ActiveCaptionText/>



< asp:TemplateField HeaderText =课程年度>

< itemtemplate>

<%#Eval(CourseYear)%>



< HeaderStyle Horizo​​ntalAlign =LeftVerticalAlign =MiddleCssClass =GridDataheader

宽度=10%/>

< itemstyle horizo​​ntalalign =左verticalalign =Topwidth =10%cssclass =GridData>

ForeColor =ActiveCaptionText/>



< asp:TemplateField HeaderText =Session>

< itemtemplate>

<%#Eval(会话)%>



< HeaderStyle Horizo​​ntalAlign =Left VerticalAlign =MiddleCssClass =GridDatahead呃

宽度=10%/>

< itemstyle horizo​​ntalalign =Leftverticalalign =Topwidth =10%cssclass =GridData >

ForeColor =ActiveCaptionText/>



< asp:TemplateField HeaderText =Semester>

< itemtemplate>

< asp:Label ID =lblsemesterrunat =serverText ='<%#Eval(Semester)%>'> ;



< HeaderStyle Horizo​​ntalAlign =LeftVerticalAlign =MiddleCssClass =GridDataheader

Width =10%/> ;

< itemstyle horizo​​ntalalign =Leftverticalalign =Topwidth =10%cssclass =GridData>

ForeColor =ActiveCaptionText/> ;





<% - sub gridview - %>

< asp:TemplateField HeaderText =标记详细信息>

< itemtemplate>

< asp:GridView ID =Gviewmarksrunat =serverShowHeader =trueAutoGenerateColumns = false>

< columns>

< asp:TemplateField HeaderText =主题代码>

< itemtemplate>

<%#Eval(SubjectCode )%>









< asp:TemplateField HeaderText =主题名称>

< itemtemplate>

<%#Eval(SubjectName)%>







< asp:TemplateField HeaderText =Int.Obt Marks>

< itemtemplate&g t;

< asp:Label ID =lbliobtainmarkrunat =serverText ='<%#Eval(InternalObtainMarks)%>'>







< asp:TemplateField HeaderText =Int.Pass Marks>

< itemtemplate>

< asp:Label ID =lblipassmarkrunat =serverText ='<%#Eval(InternalPassingMarks)%>'> ;







< asp:TemplateField HeaderText =Int.Total Marks>

< itemtemplate>

< asp:Label ID =lblitotmarksrunat =serverText ='<%#Eval(InternalTotalMarks)%>' >







< asp:TemplateField HeaderText =获得标记>

< itemtemplate>

<%#Eval(ObtainedMarks)%>







< asp:TemplateField HeaderText =传递标记>

< itemtemplate>

<%#Eval(PassingMarks)%>







< asp:TemplateField HeaderText =总标记>

< itemtemplate>

<%#Eval( TotalMarks)%>













<% - end - %>



< pagersettings nextpagetext =Nextpreviouspagetext =Previous>

< pagerstyle cssclass =GridDataheight =30pxhorizo​​ntalalign =rightbackcolor =#E7E7FFforecolor =#4A3C8C>

< footerstyle backcolor =#B5C7DEforecolor =#4A3C8C>

< SelectedRowStyle BackColor =#738A9CFont-Bold =TrueForeColor =#F7F7F7Height =40px/> ;

< HeaderStyle BackColor =#4A3C8CFont-Bold =TrueForeColor =#F7F7F7Height =40px/>

< sortedascendingcellstyle backcolor =#F4F4FD>

< sortedascendingheaderstyle backcolor =#5A4C9D>

< sorteddescendingcellstyle backcolor =#D8D8F0>

< sorteddescendingheaderstyle backcolor = #3E3277>











<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 =btnExportrunat =serverText =ExportOnClick =btnExport_Click/ >



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





然后我可以从网格视图中导出数据。



但是(Care Here)当我将页面保留在更新面板控件中时,我无法从网格视图中导出数据




< ; asp:UpdatePanel ID =updcontrolrunat =server>

< contenttemplate>



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 =ToolkitScriptManager1runat =server>

















课程名称 < asp:DropDownList ID =ddlCourseNamerunat =serverAutoPostBack =TrueOnSelectedIndexChanged =ddlCourseName_SelectedIndexChanged>

< asp:ListItem值=0> - - 选择 - -

< asp:ListItem值=第一年>第一年

< asp:ListItem值=第二年>第二年

< asp:ListItem值=第三年>第三年

< asp:ListItem值=第4年>第4年



课程年度 < asp:DropDownList ID =ddlCYearrunat =serverAutoPostBack =True>

< asp:ListItem Value =0> - - 选择 - -

< asp:ListItem Value =1st-Year> 1st-year

< asp:ListItem Value =2nd-Year> 2nd-

< asp:ListItem Value =3rd-Year> 3rd-year

< asp:ListItem Value =4th-Year> 4th-



会话 < asp:DropDownList ID =ddlSessionrunat =服务器>

< asp:ListItem Value = - 选择 - > - 选择 -

< 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> 20 16-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 =lblsemrunat =serverText =Semester>

< asp:DropDownList ID =ddlSemesterrunat = server>

< asp:ListItem Value =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 =lblTyperunat =serverText =LabelVisible =False>

   
< asp:Button ID =btnSearchrunat =serverText =SearchOnClick =btnSearch_Click/>




<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 =serverID =GviewBackColor =WhiteBorderColor =#E7E7FFBorderStyle =None

RowStyle-CssClass = GVRowBorderWidth =1pxCellPadding =3AutoGenerateColumns =False

Width =100%EmptyDataText =No Record foundHorizo​​ntalAlign =Left

CssClass =tablesorterHeaderStyle-VerticalAlign =MiddleGridLines =Horizo​​ntalOnRowDataBound =Gview_RowDataBound>

< rowstyle forecolor =#4A3C8Cbackcolor =#E7E7FF> ;

< emptydatarowstyle bordercolor =#C CCCCCborderstyle =Solidborderwidth =1pxforecolor =Red>

Height =20pxHorizo​​ntalAlign =CenterVerticalAlign =MiddleWidth =100%/> ;

< alternatingrowstyle backcolor =#F7F7F7>

< columns> < asp:TemplateField Visible =false>

< HeaderStyle Horizo​​ntalAlign =CenterVerticalAlign =MiddleWidth =2%/>

< itemstyle horizo​​ntalalign =Centerverticalalign =Topwidth =2%>





< asp:TemplateField HeaderText =#No>

< itemtemplate>



< asp:Label ID =Label1runat =serverText ='<%#Container.DisplayIndex + 1%>'>





< HeaderStyle Horizo​​ntalAlign =CenterVerticalAlign =MiddleWidth =3%/>

< itemstyle horizo​​nta lalign =Leftverticalalign =Topwidth =3%cssclass =GridData>



< asp:TemplateField HeaderText =Enrollment> ;

< itemtemplate>

<%#Eval(Enrollment)%>



< HeaderStyle Horizo​​ntalAlign =LeftVerticalAlign =MiddleCssClass =GridDataheader

Width =15%/>

< itemstyle horizo​​ntalalign =Left verticalalign =Topwidth =15%cssclass =GridData>

ForeColor =ActiveCaptionText/>





< asp:TemplateField HeaderText =Roll否>

< itemtemplate>

< asp:Label ID =lblRollNorunat =serverText ='<%#Eval(RollNo )%>'CommandArgument ='<%#Eval(RollNo)%>'>





< ; HeaderStyle Horizo​​ntalAlign =LeftVerticalAlign =MiddleCssClass =GridDataheader

Width =10%/>

< itemstyle horizo​​ntalalign =Left verticalalign =Topwidth =10%cssclass =GridData>

ForeColor =ActiveCaptionText/>



< asp:TemplateField HeaderText =课程年度>

< itemtemplate>

<%#Eval(CourseYear)%>



< HeaderStyle Horizo​​ntalAlign =LeftVerticalAlign =MiddleCssClass =GridDataheader

宽度=10%/>

< itemstyle horizo​​ntalalign =左verticalalign =Topwidth =10%cssclass =GridData>

ForeColor =ActiveCaptionText/>



< asp:TemplateField HeaderText =Session>

< itemtemplate>

<%#Eval(会话)%>



< HeaderStyle Horizo​​ntalAlign =Left VerticalAlign =MiddleCssClass =GridDatahead呃

宽度=10%/>

< itemstyle horizo​​ntalalign =Leftverticalalign =Topwidth =10%cssclass =GridData >

ForeColor =ActiveCaptionText/>



< asp:TemplateField HeaderText =Semester>

< itemtemplate>

< asp:Label ID =lblsemesterrunat =serverText ='<%#Eval(Semester)%>'> ;



< HeaderStyle Horizo​​ntalAlign =LeftVerticalAlign =MiddleCssClass =GridDataheader

Width =10%/> ;

< itemstyle horizo​​ntalalign =Leftverticalalign =Topwidth =10%cssclass =GridData>

ForeColor =ActiveCaptionText/> ;





<% - sub gridview - %>

< asp:TemplateField HeaderText =标记详细信息>

< itemtemplate>

< asp:GridView ID =Gviewmarksrunat =serverShowHeader =trueAutoGenerateColumns = false>

< columns>

< asp:TemplateField HeaderText =主题代码>

< itemtemplate>

<%#Eval(SubjectCode )%>









< asp:TemplateField HeaderText =主题名称>

< itemtemplate>

<%#Eval(SubjectName)%>







< asp:TemplateField HeaderText =Int.Obt Marks>

< itemtemplate&g t;

< asp:Label ID =lbliobtainmarkrunat =serverText ='<%#Eval(InternalObtainMarks)%>'>







< asp:TemplateField HeaderText =Int.Pass Marks>

< itemtemplate>

< asp:Label ID =lblipassmarkrunat =serverText ='<%#Eval(InternalPassingMarks)%>'> ;







< asp:TemplateField HeaderText =Int.Total Marks>

< itemtemplate>

< asp:Label ID =lblitotmarksrunat =serverText ='<%#Eval(InternalTotalMarks)%>' >







< asp:TemplateField HeaderText =获得标记>

< 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: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\" />



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


推荐答案

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;

}

}
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;
}
}


<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----

<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----
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();
    }


这篇关于我正在将网格视图导出为ex​​cel但不能正常工作。详见......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆