刚才路过个狠人的BLOG,看到他有应用,就给你转过来了,希望对你有帮助!
using System;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
namespace DY.BLL
{
/// <summary>
/// BLL_Excel 的摘要说明。
/// </summary>
public class BLL_Excel
{ public BLL_Excel(){} #region 导出Exce
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="dr">OleDbDataReader 数据源</param>
/// <param name="FileName">文件名</param>
/// <param name="biaotou">表头</param>
public void EduceExcel(OleDbDataReader dr,string FileName,string[] biaotou)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer= true;//设置缓冲输出
HttpContext.Current.Response.Charset="GB2312";//设置输出流的HTTP字符集 HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=\""+System.Web .HttpUtility.UrlEncode (FileName,System.Text .Encoding .UTF8 )+".xls\"");
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/ms-";
// _page.EnableViewState = false;//是否保持视图状态
HttpContext.Current.Response.Write( HTML(dr,biaotou) );
HttpContext.Current.Response.End ();
}
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="_page">this</param>
/// <param name="DB">DataGrid控件名称</param>
/// <param name="FileName">要导出的文件名</param>
public void EduceExcel(Page _page, DataGrid DB,string FileName)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer= true;//设置缓冲输出
HttpContext.Current.Response.Charset="GB2312";//设置输出流的HTTP字符集 HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=\""+System.Web .HttpUtility.UrlEncode (FileName,System.Text .Encoding .UTF8 )+".xls\"");
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/ms-";
_page.EnableViewState = false;//是否保持视图状态 System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); //将信息写入字符串
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); //在WEB窗体上写出一系列的HTML特定字符和文本
DB.RenderControl (oHtmlTextWriter);
HttpContext.Current.Response.Write(oStringWriter.ToString ());
HttpContext.Current.Response.End ();
}
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="ds">DataSet 数据源</param>
/// <param name="FileName">文件名</param>
/// <param name="biaotou">表头</param>
public void EduceExcel(DataSet ds,string FileName,string[] biaotou)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer= true;//设置缓冲输出
HttpContext.Current.Response.Charset="GB2312";//设置输出流的HTTP字符集 HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=\""+System.Web .HttpUtility.UrlEncode (FileName,System.Text .Encoding .UTF8 )+".xls\"");
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/ms-";
// _page.EnableViewState = false;//是否保持视图状态
HttpContext.Current.Response.Write( HTML(ds,biaotou) );
HttpContext.Current.Response.End ();
}
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="ds">DataSet 数据源</param>
/// <param name="FileName">文件名</param>
/// <param name="biaotou">表头</param>
public void EduceExcel(DataTable dt,string FileName,string[] biaotou)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer= true;//设置缓冲输出
HttpContext.Current.Response.Charset="GB2312";//设置输出流的HTTP字符集 HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=\""+System.Web .HttpUtility.UrlEncode (FileName,System.Text .Encoding .UTF8 )+".xls\"");
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/ms-";
// _page.EnableViewState = false;//是否保持视图状态
HttpContext.Current.Response.Write( HTML(dt,biaotou) );
HttpContext.Current.Response.End ();
}
#endregion #region 构造HTML
/// <summary>
/// 使用DataSet蔉据源
/// </summary>
/// <param name="ds"></param>
/// <param name="biaotou"></param>
/// <returns></returns>
private string HTML (DataSet ds,string[] biaotou)
{
StringBuilder ss = new StringBuilder();
ss.Append("<table>");
ss.Append("<tr>");
ss.Append(" <td>序号</td>");
foreach(string str in biaotou)
{
ss.Append(" <td> "+ str +"</td>");
}
ss.Append("</tr>");
int ii=1;
foreach (DataRow dr in ds.Tables[0].Rows)
{
ss.Append("<tr>");
ss.Append(" <td> "+ (ii++).ToString() +"</td>");
int I = dr.Table.Columns.Count;
for (int i=0;i<I;i++)
{
ss.Append(" <td> "+ dr[i].ToString() +"</td>");
} ss.Append("</tr>");
}
ss.Append ("</table>"); return ss.ToString();
}
/// <summary>
/// 使用OleDbDataReader 数据源
/// </summary>
/// <param name="dr"></param>
/// <param name="biaotou"></param>
/// <returns></returns>
private string HTML (OleDbDataReader dr,string[] biaotou)
{
StringBuilder ss = new StringBuilder();
ss.Append("<table>");
ss.Append("<tr>");
ss.Append(" <td>序号</td>");
foreach(string str in biaotou)
{
ss.Append(" <td> "+ str +"</td>");
}
ss.Append("</tr>"); int ii=1;
while( dr.Read() )
{
ss.Append("<tr>");
ss.Append(" <td> "+ (ii++).ToString() +"</td>");
int I = dr.FieldCount;
for (int i=0;i<I;i++)
{
ss.Append(" <td> "+ dr[i].ToString() +"</td>");
} ss.Append("</tr>");
}
ss.Append ("</table>");
dr.Close();
return ss.ToString();
} /// <summary>
/// 使用DataTable数据源
/// </summary>
/// <param name="dt"></param>
/// <param name="biaotou"></param>
/// <returns></returns>
private string HTML (DataTable dt,string[] biaotou)
{
StringBuilder ss = new StringBuilder();
ss.Append("<table>");
ss.Append("<tr>");
ss.Append(" <td>序号</td>");
foreach(string str in biaotou)
{
ss.Append(" <td> "+ str +"</td>");
}
ss.Append("</tr>"); int ii=dt.Rows.Count;
foreach (DataRow dr in dt.Rows)
{
ss.Append("<tr>");
ss.Append(" <td> "+ (ii++).ToString() +"</td>");
int I = dr.Table.Columns.Count;
for (int i=0;i<I;i++)
{
ss.Append(" <td> "+ dr[i].ToString() +"</td>");
} ss.Append("</tr>");
}
ss.Append ("</table>");
return ss.ToString();
}
#endregion
}
}
using System;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
namespace DY.BLL
{
/// <summary>
/// BLL_Excel 的摘要说明。
/// </summary>
public class BLL_Excel
{ public BLL_Excel(){} #region 导出Exce
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="dr">OleDbDataReader 数据源</param>
/// <param name="FileName">文件名</param>
/// <param name="biaotou">表头</param>
public void EduceExcel(OleDbDataReader dr,string FileName,string[] biaotou)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer= true;//设置缓冲输出
HttpContext.Current.Response.Charset="GB2312";//设置输出流的HTTP字符集 HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=\""+System.Web .HttpUtility.UrlEncode (FileName,System.Text .Encoding .UTF8 )+".xls\"");
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/ms-";
// _page.EnableViewState = false;//是否保持视图状态
HttpContext.Current.Response.Write( HTML(dr,biaotou) );
HttpContext.Current.Response.End ();
}
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="_page">this</param>
/// <param name="DB">DataGrid控件名称</param>
/// <param name="FileName">要导出的文件名</param>
public void EduceExcel(Page _page, DataGrid DB,string FileName)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer= true;//设置缓冲输出
HttpContext.Current.Response.Charset="GB2312";//设置输出流的HTTP字符集 HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=\""+System.Web .HttpUtility.UrlEncode (FileName,System.Text .Encoding .UTF8 )+".xls\"");
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/ms-";
_page.EnableViewState = false;//是否保持视图状态 System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); //将信息写入字符串
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); //在WEB窗体上写出一系列的HTML特定字符和文本
DB.RenderControl (oHtmlTextWriter);
HttpContext.Current.Response.Write(oStringWriter.ToString ());
HttpContext.Current.Response.End ();
}
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="ds">DataSet 数据源</param>
/// <param name="FileName">文件名</param>
/// <param name="biaotou">表头</param>
public void EduceExcel(DataSet ds,string FileName,string[] biaotou)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer= true;//设置缓冲输出
HttpContext.Current.Response.Charset="GB2312";//设置输出流的HTTP字符集 HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=\""+System.Web .HttpUtility.UrlEncode (FileName,System.Text .Encoding .UTF8 )+".xls\"");
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/ms-";
// _page.EnableViewState = false;//是否保持视图状态
HttpContext.Current.Response.Write( HTML(ds,biaotou) );
HttpContext.Current.Response.End ();
}
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="ds">DataSet 数据源</param>
/// <param name="FileName">文件名</param>
/// <param name="biaotou">表头</param>
public void EduceExcel(DataTable dt,string FileName,string[] biaotou)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer= true;//设置缓冲输出
HttpContext.Current.Response.Charset="GB2312";//设置输出流的HTTP字符集 HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=\""+System.Web .HttpUtility.UrlEncode (FileName,System.Text .Encoding .UTF8 )+".xls\"");
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/ms-";
// _page.EnableViewState = false;//是否保持视图状态
HttpContext.Current.Response.Write( HTML(dt,biaotou) );
HttpContext.Current.Response.End ();
}
#endregion #region 构造HTML
/// <summary>
/// 使用DataSet蔉据源
/// </summary>
/// <param name="ds"></param>
/// <param name="biaotou"></param>
/// <returns></returns>
private string HTML (DataSet ds,string[] biaotou)
{
StringBuilder ss = new StringBuilder();
ss.Append("<table>");
ss.Append("<tr>");
ss.Append(" <td>序号</td>");
foreach(string str in biaotou)
{
ss.Append(" <td> "+ str +"</td>");
}
ss.Append("</tr>");
int ii=1;
foreach (DataRow dr in ds.Tables[0].Rows)
{
ss.Append("<tr>");
ss.Append(" <td> "+ (ii++).ToString() +"</td>");
int I = dr.Table.Columns.Count;
for (int i=0;i<I;i++)
{
ss.Append(" <td> "+ dr[i].ToString() +"</td>");
} ss.Append("</tr>");
}
ss.Append ("</table>"); return ss.ToString();
}
/// <summary>
/// 使用OleDbDataReader 数据源
/// </summary>
/// <param name="dr"></param>
/// <param name="biaotou"></param>
/// <returns></returns>
private string HTML (OleDbDataReader dr,string[] biaotou)
{
StringBuilder ss = new StringBuilder();
ss.Append("<table>");
ss.Append("<tr>");
ss.Append(" <td>序号</td>");
foreach(string str in biaotou)
{
ss.Append(" <td> "+ str +"</td>");
}
ss.Append("</tr>"); int ii=1;
while( dr.Read() )
{
ss.Append("<tr>");
ss.Append(" <td> "+ (ii++).ToString() +"</td>");
int I = dr.FieldCount;
for (int i=0;i<I;i++)
{
ss.Append(" <td> "+ dr[i].ToString() +"</td>");
} ss.Append("</tr>");
}
ss.Append ("</table>");
dr.Close();
return ss.ToString();
} /// <summary>
/// 使用DataTable数据源
/// </summary>
/// <param name="dt"></param>
/// <param name="biaotou"></param>
/// <returns></returns>
private string HTML (DataTable dt,string[] biaotou)
{
StringBuilder ss = new StringBuilder();
ss.Append("<table>");
ss.Append("<tr>");
ss.Append(" <td>序号</td>");
foreach(string str in biaotou)
{
ss.Append(" <td> "+ str +"</td>");
}
ss.Append("</tr>"); int ii=dt.Rows.Count;
foreach (DataRow dr in dt.Rows)
{
ss.Append("<tr>");
ss.Append(" <td> "+ (ii++).ToString() +"</td>");
int I = dr.Table.Columns.Count;
for (int i=0;i<I;i++)
{
ss.Append(" <td> "+ dr[i].ToString() +"</td>");
} ss.Append("</tr>");
}
ss.Append ("</table>");
return ss.ToString();
}
#endregion
}
}
解决方案 »
- 被打开的模式窗口 怎么不响应事件啊?
- 求个正则表达式
- session 过期问题
- 未能加载文件或程序集“DMSTime”或它的某一个依赖项。拒绝访问。试了好几种方法 没有解决......
- default.aspx不能访问CS类里的函数
- 3个textbox 当其中两个输入int时,另一个输出他们的和.改变其中任何一个,和也改变
- 收藏 不显示删除回复显示所有回复显示星级回复显示得分回复 aspx.cs文件里如何访问aspx页面控件的属性
- 初学者问题,在打开以前的WEB项目时出错误,应该怎样解决?
- 求关于一个Button事件的编写
- 如何理解session,cookie,viewstate的区别?
- ASP.NET站点本地化中的数据库设计问题
- 急急急!!!使用httpresponse方法下载文件时,当直接打开文件,可能会覆盖原来的父页面,哪位高手知道怎样解决这个问题呢?
而且没有相关联的文章,就这一篇^_^
等我要是找到了,我就再给你发上来OK?
http://blog.csdn.net/winner2050
我也是用repeater显示数据的
想请教下,我导出的数据没有网格线,改怎样让显示的数据有网格线,
最上面的那位贴出来的应该就是吧。
用DataGrid控件绑定的导出excel后也没有网格线啊,
请帮忙看看