最近要做个一导出数据功能,网上找了下,没有找到 有的说要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; } }}
转载于:https://www.cnblogs.com/LYunF/archive/2012/03/25/2416502.html
相关资源:JAVA上百实例源码以及开源项目