.net 导出excel

protected void btn_Export_Click(object sender, EventArgs e)//导出按钮
{
DataTable dts=dsUDL().Tables[0];//数据源获取
BLL.TB_ActivateSoftware bllas = new HYTD.CAPlatform.BLL.TB_ActivateSoftware();
//取消表中多余列
dts.Columns.Remove("OS_Type");
dts.Columns.Remove("OS_DateTime");
dts.Columns.Remove("OS_Uid");
dts.Columns.Remove("OS_SoftID");
dts.Columns.Remove("OS_Year");
DataTable dtResult = new DataTable();
//克隆表结构
dtResult = dts.Clone();
#region 修改字段类型
foreach (DataColumn col in dtResult.Columns)
{
if (col.ColumnName == "OS_ID")
{
//修改列类型
col.DataType = typeof(Int32);
}

if (col.ColumnName == "OS_LoginName")
{
//修改列类型
col.DataType = typeof(String);
}
if (col.ColumnName == "OS_RealName")
{
//修改列类型
col.DataType = typeof(String);
}
if (col.ColumnName == "OS_SoftName")
{
//修改列类型
col.DataType = typeof(String);
}
}
#endregion
#region 字段修改值
int i = 1;
BLL.TB_User bllus = new HYTD.CAPlatform.BLL.TB_User();
foreach (DataRow row in dts.Rows)
{
DataRow rowNew = dtResult.NewRow();
//修改记录值
rowNew["OS_ID"] = i;
rowNew["OS_LoginName"] = row["OS_LoginName"];
rowNew["OS_RealName"] = row["OS_RealName"];
rowNew["OS_SoftName"] = row["OS_SoftName"];
dtResult.Rows.Add(rowNew);
i++;
}
#endregion
#region 修改标题
dtResult.Columns["OS_ID"].ColumnName = "编号";
dtResult.Columns["OS_SoftName"].ColumnName = "软件名称";
dtResult.Columns["OS_LoginName"].ColumnName = "用户名";
dtResult.Columns["OS_RealName"].ColumnName = "姓名";
#endregion

ExportExcel.GetExcel(dtResult, "用户下载软件清单");//调用excel类  导出excel表

}


/// <summary>
/// 导出Excel
/// </summary>
/// <param name="dtData"> DataTable表格</param>
/// <param name="FileName"> 文件名称</param>
public static void GetExcel(System.Data.DataTable dtData, string FileName)
{
System.Web.UI.WebControls.GridView gvExport = null;
// 当前对话
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
// IO用于导出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;

 if (dtData != null)
            {
                // 设置编码和附件格式 
                curContext.Response.ContentType = "application/vnd.ms-excel";
                curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(FileName) + DateTime.Today.ToShortDateString() + ".xls"); //定义输出文件和文件名
                curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
                curContext.Response.Charset = "";


                // 导出excel文件 
                strWriter = new System.IO.StringWriter();
                htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);


                // 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid 
                gvExport = new System.Web.UI.WebControls.GridView();
                gvExport.DataSource = dtData.DefaultView;
                gvExport.AllowPaging = false;
                gvExport.DataBind();


                // 返回客户端 
                gvExport.RenderControl(htmlWriter);
                curContext.Response.Write(strWriter.ToString());
                curContext.Response.End();
            }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值