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





    }
  • 相关阅读:
    使用vscode 用git 拉取代码,提示:在签出前,请清理存储库工作树
    区分手机端和pc端加载不同的样式
    关于vue的代码规范
    各种名词汇总整理
    ZB埋点汇总
    项目实战 OLAP数据提取
    大数据intern_1总结:数据埋点以及SQL复习
    leetcode 343+279+91+64+70 动态规划
    leetcode 241 加优先级括号
    leetcode 17+79+93+46+47+77+39+40+78+90+131+37 回溯法
  • 原文地址:https://www.cnblogs.com/LYunF/p/2416502.html
Copyright © 2020-2023  润新知