当前位置:编程学习 > C#/ASP.NET >>

怎么导出固定格式EXCEL文件啊??

要导出象那种单据形式的EXCEL --------------------编程问答-------------------- 沙发,帮忙顶 --------------------编程问答-------------------- 那不是得在页面或Gridview中的格式像单据那样形式就行了。 --------------------编程问答-------------------- 给你个例子 你看看
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 >OleDbDataReader 数据源</param>
  /// <param >文件名</param>
  /// <param >表头</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 >this</param>
  /// <param >DataGrid控件名称</param>
  /// <param >要导出的文件名</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 >DataSet 数据源</param>
  /// <param >文件名</param>
  /// <param >表头</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 >DataSet 数据源</param>
  /// <param >文件名</param>
  /// <param >表头</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 ></param>
  /// <param ></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 ></param>
  /// <param ></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 ></param>
  /// <param ></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
 }

--------------------编程问答-------------------- 按钮的点击事件
protected void Button1_Click(object sender, EventArgs e)
    {
        // EduceExcel(this, GridView1, "asdfexe");
        string strconn = ConfigurationManager.ConnectionStrings["51shangcheng2007ConnectionString"].ToString();
        SqlConnection cn = new SqlConnection(strconn);
        string sqlstr = Session["sqlstr"].ToString();
        cn.Open();
        SqlDataAdapter da = new SqlDataAdapter(sqlstr, cn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        cn.Close();
        string[] a ={ "学校", "订单日期", "订单号", "姓名", "性别", "身分证号", "地址", "电话","班号","课程名称","开课时间","价格"     };
        string name = System.DateTime.Now.Date.ToShortDateString()+"报名表";
        EduceExcel(ds, name, a);
    } --------------------编程问答-------------------- 恩不错
--------------------编程问答-------------------- 這種控件也很多,大致按照ls的方法寫 --------------------编程问答-------------------- ~ --------------------编程问答-------------------- 我也很想知道具体怎么操作,3楼的方法没法用。
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,