日期:2014-05-20  浏览次数:20874 次

用aps.net服务器控件 table画出来的表格 如何导出EXECL?
………………

<asp:Table ID="tb_Info" runat="server" CssClass="table">
</asp:Table>
………………其他HTML代码就就不贴上来

这个服务器控件TABLE 在后台代码一个一个cell画出来的效果 如下:
    


现在要求汇出EXECL!咋整最方便??
------最佳解决方案--------------------
每一列单独算出来再合并咯
------其他解决方案--------------------
#region 将一个网页Html文件导成Excel文件
    /// <summary>
    ///  将一个网页Html文件导成Excel文件
    /// </summary>
    /// <param name="url">网址</param>
    /// <param name="saveas">是否在服务器端用Execl打开文件另存(文件容量减少)</param>
    /// <returns></returns>
    public static string HtmlToExcel(string url, bool saveas)
    {
        //导出临时文件的路径
        string OldFilePath = GetFilePath();
        string NewFilePath = GetFilePath();

        string returnFilePath = string.Empty;

        //取得数据
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        System.Web.HttpContext.Current.Server.Execute(url, oHtmlTextWriter);
        System.Web.UI.Page page = new System.Web.UI.Page();
        page.RenderControl(oHtmlTextWriter);
        string Str = oStringWriter.ToString();
        string strStart = "<!-- Excel Start -->";
        string strEnd = "<!-- Excel End -->";

        int IndexStart = Str.IndexOf(strStart);
        int IndexEnd = Str.IndexOf(strEnd);
        //如果页面没有 Excel导出标签 则将页面内容全部导出
        if (IndexStart != -1 && IndexEnd != -1)
        {
            int ContentLength = IndexEnd - IndexStart - strStart.Length;
            Str = Str.Substring(IndexStart + strStart.Length, ContentLength);
        }

        FileStream fs = File.Create(OldFilePath);
        System.IO.StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
        sw.Write(Str);
        sw.Flush();
        fs.Close();

        //不另存