110,533
社区成员
发帖
与我相关
我的任务
分享
导出excel的类文件
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
/// <summary>
/// Excel文件处理类
/// </summary>
internal class TExcelHelper : IExport
{
Page m_page;
public event dlg_BindData OnBindData;
/// <summary>
/// 构造器
/// </summary>
public TExcelHelper( Page page )
{
this.m_page = page;
}
#region IExport 成员
/// <summary>
/// 名 称:导出文件
/// 功 能:将GridView表格中的数据导出到Excel中
/// 作 者:Hasen
/// 创建时间:2007-11-20
/// 最近修改:2007-11-20
/// 使用说明:
/// </summary>
/// <param name="srcGridView">原 GridView 网格控件(从客户程序传入该引用)</param>
/// <param name="FileName">默认导出文件名称</param>
public void Export( GridView srcGridView , string FileName )
{
m_page.Response.ClearContent();
m_page.Response.Charset = "gb2312";
m_page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");//设置输出流为简体中文
m_page.Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(FileName, Encoding.UTF8) + ".xls\"");
m_page.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter tw = new System.IO.StringWriter(myCItrad);
HtmlTextWriter hw = new HtmlTextWriter(tw);
srcGridView.AllowPaging = false;
if (OnBindData != null)
OnBindData();
srcGridView.RenderControl(hw);
m_page.Response.Write(tw.ToString());
m_page.Response.End();
}
}
#endregion
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "javascript:if(this.bgColor=='#c4d2ea'){this.bgColor='white'}else{this.bgColor='#c4d2ea';}");
//excel导出,文本化
for (int i = 0; i < e.Row.Cells.Count; i++)
{
e.Row.Cells[i].Attributes.Add("class", "text");
}
}
protected void imgBtnExcel_Click(object sender, ImageClickEventArgs e)
{
IExport iexport = TFileHelperFactory.CreateExcelHelper(Page);
if (iexport != null)
{
iexport.OnBindData += new dlg_BindData(InitPage);
iexport.Export(gvList, "生产信息统计");
DataAccess.SystemLog.InsertLogInfo(LoggingType.Select, "生产信息统计", "Excel导出生产信息", "");
}
}
public override void VerifyRenderingInServerForm(Control control)
{
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="StatProductionList.aspx.cs" Inherits="ProductionManagement_StatProductionList" EnableEventValidation="false" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>产量信息统计</title>
</head>
<body>
<form id="form1" runat="server">
<link href="../skins/<%=style%>" type="text/css" rel="stylesheet" />
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<table id="Table1" class="td" width="100%">
<tr>
<td style="height: 20px" colspan="3">
<span style="color: blue">
<asp:HyperLink ID="HyperLink1" runat="server" Font-Underline="True" ForeColor="Blue" NavigateUrl="~/ProductionManagement/ProductionManagementMain.aspx" Text="<%$ Resources:ProductionManage, ProductionManagement %>"></asp:HyperLink>--<asp:HyperLink ID="HyperLink2" runat="server" Font-Underline="True" ForeColor="Blue" NavigateUrl="~/ProductionManagement/PlanAnalyseMain.aspx" Text="<%$ Resources:ProductionManage, PlanAnalyse %>"></asp:HyperLink>--<asp:Label ID="Label1" runat="server" Text="<%$ Resources:ProductionManage, ProductionInformationStat %>"></asp:Label>--<asp:Label ID="Label" runat="server"></asp:Label></span></td>
</tr>
<tr>
<td align="right">
<asp:Label ID="lblMessage" runat="server" ForeColor="Red"></asp:Label></td>
<td style="width:10%" align="left">
<asp:ImageButton ID="imgBtnExcel" runat="server" ImageUrl="<%$ Resources:ProductionManage, ToExcel %>" OnClick="imgBtnExcel_Click" /></td>
<td style="width:10%" align="left">
<asp:ImageButton ID="imgBtnReturn" runat="server" ImageUrl="<%$ Resources:Public, ReturnButton %>" OnClick="imgBtnReturn_Click" /></td>
</tr>
</table>
<table id="Table2" class="td" width="100%">
<tr>
<td>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gvList" runat="server" AllowSorting="True" Width="98%" OnRowDataBound="gvList_RowDataBound" OnSorting="gvList_Sorting" CssClass="GridBackColor" ShowFooter="True" OnPageIndexChanged="gvList_PageIndexChanged" OnPageIndexChanging="gvList_PageIndexChanging">
<HeaderStyle CssClass="GridHeadBackColor" />
<FooterStyle CssClass="GridHeadBackColor" HorizontalAlign="Center" VerticalAlign="Middle"/>
<RowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel> </tr>
</table>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gvcollect" runat="server" AllowSorting="True" CssClass="GridBackColor" ShowFooter="True" OnRowDataBound="gvcollect_RowDataBound" OnSorting="gvcollect_Sorting" Visible="False" Width="40%" >
<HeaderStyle CssClass="GridHeadBackColor" HorizontalAlign="Center" VerticalAlign="Middle" />
<FooterStyle CssClass="GridHeadBackColor" HorizontalAlign="Center" VerticalAlign="Middle" />
<RowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>