登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

学无止境

一点积累,与大家分享

 
 
 

日志

 
 

asp.net html table导出到Excel  

2010-07-16 13:39:50|  分类: .net技术 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExportServicePage.aspx.cs" Inherits="Demo.ExportServicePage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <%=System.DateTime.Now %>
    </div>
    <asp:TextBox ID="ExportContent" runat="server" Height="247px"
        TextMode="MultiLine" Width="572px" Text="<table><tr><td>中文</td><td>英文</td></tr></table>">
        </asp:TextBox>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    <asp:TextBox ID="ExportFile" runat="server">中文名称</asp:TextBox>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
/************************************************************************
 *
 * 模块说明:导出数据到Excel
 * 作者:dszhang
 * Email:dszhang@163.com
 * 模块功能:
 *  1、将Html Table 内容以(ExportContent字段)Post方法提交导出到Excel
 *
 * ************************************************************************
 */
namespace Demo
{
    public partial class ExportServicePage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["ExportContent"]))
            {

                string tmpFileName = "export.xls";

                string tmpContent = Request["ExportContent"];//获取传递上来的文件内容 
              ;
                if (Request["ExportFile"] != "")
                {

                    tmpFileName = Request["ExportFile"];// 获取传递上来的文件名 

                    //tmpFileName = System.Web.HttpUtility.UrlEncode(Request.ContentEncoding.GetBytes(tmpFileName));//处理中文文件名的情况                     

                }

                Response.Write("&amp;lt;script&amp;gt;document.close();&amp;lt;/script&amp;gt;");
                if (tmpContent.ToLower().IndexOf("<table") > -1)
                {
                    ExportDsToXls(tmpFileName, tmpContent, false);
                }
                else
                {
                    ExportDsToXls(tmpFileName, tmpContent, true);
                }
             
            } 
        }

        protected void ExportDsToXls( string fileName, string html,Boolean isAddTableHead)
        {

            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            Response.Clear();
            Response.Charset = "gb2312";
            Response.ContentType = "application/vnd.ms-excel";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            //转码 :System.Web.HttpUtility.UrlEncode(fileName)
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
           
            Response.Write("<html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"></head><body>");
            if(isAddTableHead)
                 Response.Write("<TABLE WIDTH=\"100%\"  BORDER=\"1\" align=\"center\" CELLPADDING=\"0\" CELLSPACING=\"0\" >");
          
            Response.Write(html);

            Response.Write(tw.ToString());
            if (isAddTableHead)
                Response.Write("</table>");

            Response.Write("</body></html>");
            Response.End();
            hw.Close();
            hw.Flush();
            tw.Close();
            tw.Flush();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }
    }
}

从其它页面Post提交过来实例:
js:
 ////////////////////////////////////导出到Excel///////////////////////////////// ////////////
     function toExcle(fromTableID,filename) {
         var elTable = document.getElementById(fromTableID);
         var strHtml = elTable.innerHTML;
         var tempHtml = mask_HTMLCode(strHtml);
         
         var exportContent = document.getElementById("ExportContent");
         exportContent.value = tempHtml;
         var exportFile = document.getElementById("ExportFile");
         exportFile.value = filename;
         theForm.action = "ExportServicePage.aspx";
         theForm.target = "_blank";
         theForm.submit();
        

     }

     function mask_HTMLCode(inputHTML) {
         var str = inputHTML;
         str = str.replace(/"/g, "'");
         str = str.replace(/<a.*?>(.*?)<\/a>/ig, "$1");
         str = str.replace(/<img.*?\>(.*?)/ig, "$1");
         str = str.replace(/["]/g, "")
         return str;
     }
.aspx:
 <asp:HiddenField ID="ExportContent" runat="server" />
 <asp:HiddenField ID="ExportFile" runat="server" />
<a href="javascript:void(0)"
                                    onclick="toExcle('dataTable','营业部交易情况数据');"><IMG SRC="http://dszhang.blog.163.com/blog/../../images/export.gif" border="0" WIDTH="16" HEIGHT="16" /></a>
  评论这张
 
阅读(3019)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018