asp.net無組件匯出Excel

來源:互聯網
上載者:User

最近要做個一匯出資料功能,網上找了下,沒有找到 有的說要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;
}
}





}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.