asp.NetコンポーネントエクスポートExcelなし

10288 ワード

最近データをエクスポートする机能をして、ネット上で探して、ある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;
}
}





}