首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

用aps.net服务器控件 table画出来的表格 怎么导出EXECL

2012-12-14 
用aps.net服务器控件 table画出来的表格 如何导出EXECL?………………asp:Table IDtb_Info runatserver Cs

用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();

        //不另存


        if (saveas == false)
        {
            returnFilePath = OldFilePath;
        }
        else
        {

            object Missing = System.Reflection.Missing.Value;
            //创建Excel对象
            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            //新建工作簿
            Microsoft.Office.Interop.Excel.Workbook workBook = excelApp.Workbooks.Open(OldFilePath, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
            try
            {
                workBook.SaveAs(NewFilePath, XlFileFormat.xlExcel9795, Missing, Missing, Missing, Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Missing, Missing, Missing, Missing, Missing);
                workBook.Close(Missing, Missing, Missing);

                //删除文件
                System.IO.File.Delete(OldFilePath);
            }
            finally
            {
                //excelApp.Visible = false;
                workBook = null;
                excelApp.Quit();
                excelApp = null;
            }
            returnFilePath = NewFilePath;
        }
        return returnFilePath;
    }
[其他解释]
 改了一下  
#region 将一个网页Html文件导成Excel文件
    /// <summary>
    ///  将一个网页Html文件导成Excel文件
    /// </summary>
    /// <param name="url">网址</param>
    /// <returns></returns>
    public static string HtmlToExcel(string url)
    {
        //导出临时文件的路径


        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();

        returnFilePath = OldFilePath;
       
        return returnFilePath;
    } 
[其他解释]
剪切板 复制 新建 excel 然后黏贴

热点排行