数据导入导出类的编写

#region 导出数据
  private void DownLoadExcel(DataTable table,string fileName)
  { 
   DataTable dt=table;
   if(dt==null)
   {
    Jscript.Alert("对不起,没有要导出的数据");
    return; 
   }
   try
   {
    StringBuilder sb = new StringBuilder();
    sb.Append("<table border=1 width=100%>");
  
    for(int i=0;i<dt.Columns.Count;i++)
    {
     sb.Append("<td>").Append(dt.Columns[i].ColumnName).Append("</td>");
    }
    sb.Append("</tr>");
   
    foreach(DataRow row in dt.Rows)
    {
     sb.Append("<tr>");
     for(int i=0;i<dt.Columns.Count;i++)
     {
      sb.Append("<td>").Append(row[i].ToString()).Append("</td>");
     }
     sb.Append("</tr>");
    }
    sb.Append("</table>");

    HttpResponse response = HttpContext.Current.Response; 
    //HttpContext.Current.Response.Charset   ="UTF-8";  
    //HttpContext.Current.Response.ContentEncoding   =System.Text.Encoding.Default;  
    HttpContext.Current.Response.Charset   ="GB2312";  
    HttpContext.Current.Response.ContentEncoding   =System.Text.Encoding.GetEncoding("GB2312");  
    response.AddHeader("base","target=_blank");
    response.ContentType = "application/ms-Excel";
    

    //设置导出报表的文件名
    if(fileName.Trim()=="")
    {
     fileName= DateTime.Now.ToString(@"yyyyMMddhhmmss")+".xls";
    }
    else
    {
     fileName = HttpUtility.UrlEncode(fileName.Trim(),System.Text.Encoding.UTF8)+".xls";;

    }
    response.AppendHeader("Content-Disposition","inhert; filename=" + fileName);
    response.Clear();
    response.Write("<html><head><meta http-equiv=/"Content-Type/" content=/"text/html; charset=GB2312/" /></head><body>"+sb.ToString()+"</body></html>");
    // response.Write(sb.ToString());
    response.Flush();
    response.End();
   }
   catch(Exception ex)
   {
    ILog log =LogManager.GetLogger(typeof(PagerControl));
    log.Warn("导出数据生成Table表格并导出流时出错!",ex);
   }
  }
  #endregion

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值