zoukankan      html  css  js  c++  java
  • asp.net无组件导出Excel

    最近要做个一导出数据功能,网上找了下,没有找到 有的说要Excel.dll等等   导出pdf好像需要第三方dll支持

    下边是我导出Excel的源码 大家谁有好的解决方案 不防给我介绍下  源码里的数据是我模拟的

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.IO;

    public partial class ReportExel : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    DataColumn coll = dt.Columns.Add("Title", typeof(string));
    coll.AllowDBNull = false;
    coll.Unique = true; DataRow dr;

    for (int i = 0; i < 10; i++)
    {
    dr = dt.NewRow();//新行
    dr["Title"] = "" + i + "列数据";
    dt.Rows.Add(dr);
    }

    ds.Tables.Add(dt);


    ExportDataToExcel(dt, "续办人事代理");
    //CreateExcel(ds, "aa.xls");
    }





    /// <summary>
    ///
    /// </summary>
    /// <param name="dt"></param>
    /// <param name="strFileName">含.xls</param>
    public static void ExportDataToExcel(DataTable dt, string FileName)
    {
    try
    {
    StringWriter sw = new StringWriter();
    string colstr = "";
    foreach (DataColumn col in dt.Columns)
    {
    colstr += col.ColumnName + "\t";
    }
    sw.WriteLine(colstr);

    foreach (DataRow row in dt.Rows)
    {
    colstr = "";
    foreach (DataColumn col in dt.Columns)
    {
    colstr += row[col.ColumnName].ToString() + "\t";
    }
    sw.WriteLine(colstr);
    }
    sw.Close();
    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName + ".xls", System.Text.Encoding.UTF8));
    HttpContext.Current.Response.ContentType = "application/ms-excel";
    System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
    System.Web.HttpContext.Current.Response.Write(sw);
    System.Web.HttpContext.Current.Response.End();
    }
    catch (Exception ex)
    {
    throw ex;
    }
    }




    public static void ExportDataToExcelByWeb(DataTable dt,System.Web.UI.WebControls.DataGrid DGOutPut,string FileName)
    {
    try
    {
    DGOutPut.Visible=true;
    DGOutPut.DataSource=dt;
    DGOutPut.DataBind();
    System.Web.HttpContext.Current.Response.Clear();
    System.Web.HttpContext.Current.Response.Buffer = true;
    System.Web.HttpContext.Current.Response.Charset = "GB2312";
    System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename="+FileName+".xls");
    System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
    System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
    DGOutPut.EnableViewState = false;
    System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
    System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
    DGOutPut.RenderControl(oHtmlTextWriter);
    System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString());
    System.Web.HttpContext.Current.Response.End();
    DGOutPut.Visible=false;
    }
    catch (Exception ex)
    {
    throw ex;
    }
    }





    }
  • 相关阅读:
    修改Cosbench源码 支持s3的 http range request 测试场景
    CEPH s3 java sdk PUT对象并在同一个PUT请求中同时设置ACL为 Public
    庆祝团队合著的《自主实现SDN虚拟网络与企业私有云》终于得以出版 --- 本人负责分布式存储部分的编写
    Cosbench测试 RGW S3 path_style_access=true模式支持
    RGW 系统吞吐量(TPS)、用户并发量、性能测试概念和公式
    CEPH 使用SSD日志盘+SATA数据盘, 随OSD数目递增对性能影响的递增测试
    使用CEPH RGW admin ops API 进行用户user AK/SK管理的秘诀
    prometheus consul docker redis_exporter 自动注册配置
    Consul 使用手册(感觉比较全了)
    RDS for MySQL权限问题(错误代码:1227,1725)
  • 原文地址:https://www.cnblogs.com/LYunF/p/2416502.html
Copyright © 2011-2022 走看看