.Net導出Word和Excel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


public class ExportOffice
{
    //導出頁面或web控件方法#region 導出頁面或web控件方法
    /**/
    /// <summary>
    /// 將Web控件或頁面信息導出(不帶文件名參數)
    /// </summary>
    /// <param name="source">控件實例</param>        
    /// <param name="DocumentType">導出類型:Excel或Word</param>
    public void ExportControl(System.Web.UI.Control source, string DocumentType)
    {
        //設置Http的頭信息,編碼格式
        if (DocumentType == "Excel")
        {
            //Excel            
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("下載文件.xls", System.Text.Encoding.UTF8));
            HttpContext.Current.Response.ContentType = "application/ms-excel";
        }

        else if (DocumentType == "Word")
        {
            //Word
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("下載文件.doc", System.Text.Encoding.UTF8));
            HttpContext.Current.Response.ContentType = "application/ms-word";
        }

        HttpContext.Current.Response.Charset = "UTF-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;

        //關閉控件的視圖狀態
        source.Page.EnableViewState = false;

        //初始化HtmlWriter
        System.IO.StringWriter writer = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
        source.RenderControl(htmlWriter);

        //輸出
        HttpContext.Current.Response.Write(writer.ToString());
        HttpContext.Current.Response.End();
    }

    /**/
    /// <summary>
    /// 將Web控件或頁面信息導出(帶文件名參數)
    /// </summary>
    /// <param name="source">控件實例</param>        
    /// <param name="DocumentType">導出類型:Excel或Word</param>
    /// <param name="filename">保存文件名</param>
    public void ExportControl(System.Web.UI.Control source, string DocumentType, string filename)
    {
        //設置Http的頭信息,編碼格式
        if (DocumentType == "Excel")
        {
            //Excel            
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".xls", System.Text.Encoding.UTF8));
            HttpContext.Current.Response.ContentType = "application/ms-excel";
        }

        else if (DocumentType == "Word")
        {
            //Word
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".doc", System.Text.Encoding.UTF8));
            HttpContext.Current.Response.ContentType = "application/ms-word";
        }

        HttpContext.Current.Response.Charset = "UTF-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;

        //關閉控件的視圖狀態
        source.Page.EnableViewState = false;

        //初始化HtmlWriter
        System.IO.StringWriter writer = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
        source.RenderControl(htmlWriter);

        //輸出
        HttpContext.Current.Response.Write(writer.ToString());
        HttpContext.Current.Response.End();
    }
    #region 調用說明
    //方法ExportControl(System.Web.UI.Control source, string DocumentType,string filename)中
    //第一個參數source表示導出的頁面或控件名,當爲datagrid或dataList控件時,在導出Excel/word文件時,必須把控件的分頁、排序等屬性去除並從新綁定,
    //第二個參數DocumentType表示導出的文件類型word或excel
    //第三個參數filename表示須要導出的文件所取的文件名
    //調用方法:
    //ExportData export=new ExportData();
    //export.ExportControl(this, "Word","testfilename");//當爲this時表示當前頁面
    //這是將整個頁面導出爲Word,並命名爲testfilename
    #endregion
}

  

<!DOCTYPE html>

<html xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'>
<head runat="server">
 <!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View><w:TrackMoves>false</w:TrackMoves><w:TrackFormatting/><w:ValidateAgainstSchemas/><w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid><w:IgnoreMixedContent>false</w:IgnoreMixedContent><w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText><w:DoNotPromoteQF/><w:LidThemeOther>EN-US</w:LidThemeOther><w:LidThemeAsian>ZH-CN</w:LidThemeAsian><w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript><w:Compatibility><w:BreakWrappedTables/><w:SnapToGridInCell/><w:WrapTextWithPunct/><w:UseAsianBreakRules/><w:DontGrowAutofit/><w:SplitPgBreakAndParaMark/><w:DontVertAlignCellWithSp/><w:DontBreakConstrainedForcedTables/><w:DontVertAlignInTxbx/><w:Word11KerningPairs/><w:CachedColBalance/><w:UseFELayout/></w:Compatibility><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><m:mathPr><m:mathFont m:val='Cambria Math'/><m:brkBin m:val='before'/><m:brkBinSub m:val='--'/><m:smallFrac m:val='off'/><m:dispDef/><m:lMargin m:val='0'/> <m:rMargin m:val='0'/><m:defJc m:val='centerGroup'/><m:wrapIndent m:val='1440'/><m:intLim m:val='subSup'/><m:naryLim m:val='undOvr'/></m:mathPr></w:WordDocument></xml><![endif]-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style>
        table{ border: solid 1px black;border-collapse: collapse;}
        td{width: 200px;border: solid 1px black;word-wrap: break-word; word-break: break-all;}
        body,tr,td{font-size:14px;}
    </style>
</head>
<body>
<form id="form1" runat="server">
 <table style="margin:0px auto;" border="1" >
        <caption style="font:20px/30px Arail;"><asp:Literal runat="server" ID="lbl_Caption"></asp:Literal></caption>
        <tr>
            <td><asp:Literal runat="server" ID="lbl_username_title"></asp:Literal></td>
            <td><asp:Literal runat="server" ID="lbl_username_value"></asp:Literal></td>
            <td><asp:Literal runat="server" ID="lbl_userCode_title"></asp:Literal></td>
            <td><asp:Literal runat="server" ID="lbl_userCode_value"></asp:Literal></td>
        </tr>
        <tr>
            <td><asp:Literal runat="server" ID="lbl_sex_title"></asp:Literal></td>
            <td><asp:Literal runat="server" ID="lbl_sex_value"></asp:Literal></td>
            <td><asp:Literal runat="server" ID="lbl_birthday_title"></asp:Literal></td>
            <td><asp:Literal runat="server" ID="lbl_birthday_value"></asp:Literal></td>
        </tr>
        <tr>
            <td><asp:Literal runat="server" ID="lbl_nationality_title"></asp:Literal></td>
            <td><asp:Literal runat="server" ID="lbl_nationality_value"></asp:Literal></td>
            <td><asp:Literal runat="server" ID="lbl_company_title"></asp:Literal></td>
            <td><asp:Literal runat="server" ID="lbl_company_value"></asp:Literal></td>
        </tr>
        <tr>
            <td><asp:Literal runat="server" ID="lbl_hospitalName_title"></asp:Literal></td>
            <td><asp:Literal runat="server" ID="lbl_hospitalName_value"></asp:Literal></td>
            <td><asp:Literal runat="server" ID="lbl_tjDate_title"></asp:Literal></td>
            <td><asp:Literal runat="server" ID="lbl_tjDate_value"></asp:Literal></td>
        </tr>
        <asp:Literal runat="server" ID="lbl_data"> </asp:Literal>
    </table>
    </form>
</body>
</html>
相關文章
相關標籤/搜索