天天看點

.NET通過調用Office元件導出Word文檔

????????????????????????????????????????????.NET??杩?璋???ffice缁?浠跺?煎??ord??妗?

??????杩???椤圭????瑕?瀹??頒?涓?瀹㈡?風??涓?杞?ord琛ㄦ?肩?????斤?璇ュ???芥???ㄦ?風?瑰??涓?杞借〃??锛????$??灏?璇ョ?ㄦ?風???版???ヨ?㈠?烘?ュ苟?????版????ord妯℃?誇腑锛?????棣?缁?瀹㈡?風??涓?杞姐??

瀹??版??璺?濡?涓?锛?

??????╃?ㄥ井杞???渚???Office??缁?浠舵?ュ????锛??ㄦ???″?ㄧ????瀹???褰??劇疆涓?涓?word妯℃??璇ユā?誇腑??瑕??挎?㈢???版??淇℃???ㄤ功绛炬??璁闆ソ)锛?褰?璇鋒?杩??ョ???跺??锛?璇誨??妯℃?誇俊??骞跺?涔??懼??瀹規?挎?㈡??浠??版??搴??峰???淇℃???ㄨ???缁?瀹㈡?風??涓?杞藉?沖??锛?浠g??濡?涓?锛?

#region ?規???寵?峰??ID?峰??妯℃?跨????word涓?杞芥??浠?涔??劇??褰㈠?)
    public void DownLoadWordOld(string id)
    {
        if (string.IsNullOrEmpty(id))
        {
            id = "0";
        }
        string sql = "SELECT ID,ProposerName,PhoneNo,ProposerAddress,HouseArea,HouseType,HouseNature,ApplyDate" +
                   " from BettingStationApply where ID=@id ";
        SqlParameter[] parm = new SqlParameter[] { new SqlParameter("@id", int.Parse(id)) };
        //?規??ID?峰??寰?褰????寵?峰????璇??淇℃??
        DataTable dt = DBHelper.GetDataSet(sql, parm);
        if (dt.Rows.Count > 0)
        {
            DataRow dr = dt.Rows[0];

            //1??????寤轟?涓???寤?ord??Application
            Word.Application wordApp = new Word.ApplicationClass();
            //2????寤轟?涓?word??妗?            Word.Document docFile = null;
            try
            {
                wordApp.Visible = false;
                //妯℃?垮?矽薄
                object objTemplete = Server.MapPath(@"/BettingStation/templete.docx");
                object objTrue = true;
                object objFalse = false;
                object objDocType = Word.WdDocumentType.wdTypeDocument;
                //瀹?渚???word??妗?宸茬?璇誨???版ā?跨??word??妗?
                docFile = wordApp.Documents.Add(ref objTemplete, ref objFalse, ref objDocType, ref objTrue);

                //瀹?涔?涔??懼????
                object ProposerName = "ProposerName";
                object PhoneNo = "PhoneNo";
                object ProposerAddress = "ProposerAddress";
                object HouseArea = "HouseArea";
                object HouseType = "HouseType";
                object HouseNature = "HouseNature";
                object ApplyDate = "ApplyDate";
                //?峰????????涔???                Word.Bookmarks books = docFile.Bookmarks;
                //缁?涔??捐???                if (books.Exists("ProposerName"))
                {
                    books.get_Item(ref ProposerName).Range.Text = dr["ProposerName"].ToString();
                }
                if (books.Exists("PhoneNo"))
                {
                    books.get_Item(ref PhoneNo).Range.Text = dr["PhoneNo"].ToString();
                }
                if (books.Exists("ProposerAddress"))
                {
                    books.get_Item(ref ProposerAddress).Range.Text = dr["ProposerAddress"].ToString();
                }
                if (books.Exists("HouseArea"))
                {
                    books.get_Item(ref HouseArea).Range.Text = dr["HouseArea"].ToString();
                }
                if (books.Exists("HouseType"))
                {
                    books.get_Item(ref HouseType).Range.Text = dr["HouseType"].ToString();
                }
                if (books.Exists("HouseNature"))
                {
                    books.get_Item(ref HouseNature).Range.Text = dr["HouseNature"].ToString();
                }
                if (books.Exists("ApplyDate"))
                {
                    if (dr["ApplyDate"] != DBNull.Value)
                    {
                        books.get_Item(ref ApplyDate).Range.Text = Convert.ToDateTime(dr["ApplyDate"].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
                    }
                }

                object fileName = Server.MapPath(@"/BettingStation/??娉ㄧ???寵?瘋〃.docx");

                object nothing = Type.Missing;
                //灏?褰?????浠朵?瀛??頒複?舵??浠朵腑
                docFile.SaveAs2(ref fileName, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing
                    , ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing
                    , ref nothing, ref nothing, ref nothing, ref nothing, ref nothing);
                //?抽??褰???word??妗?                docFile.Close(ref nothing, ref nothing, ref nothing);
                //???哄???word绋?搴???璋???                wordApp.Quit(ref nothing, ref nothing, ref nothing);
                docFile = null;

                //浠????″?ㄤ複?舵??浠跺す涓?涓?杞芥??浠惰???缁?瀹㈡?風??
                ExtWord(fileName.ToString(), dr["ProposerName"].ToString() + "_??娉ㄧ???寵?瘋〃.docx");
                //璋??ㄥ?㈡?風??js?規?

            }
            catch (Exception ex)
            {
                writeLog.WriteErrorLog("?規??妯℃?跨????Word??浠跺?洪??!??璇?淇℃??:" + ex.Message);
                //?哄?甯歌?闆????劇?稿?寵?婧?
                object nothing = Type.Missing;
                //?抽??褰???word??妗?                docFile.Close(ref nothing, ref nothing, ref nothing);
                //???哄???word绋?搴???璋???                wordApp.Quit(ref nothing, ref nothing, ref nothing);
                docFile = null;
                Message.show("?規??妯℃?跨????Word??浠跺?洪??!??璇?淇℃??:" + ex.Message);
            }
        }
        else
        {
            writeLog.WriteErrorLog("id=" + id + "娌℃???ユ?懼?頒換浣??版??!");
            Message.show("id=" + id + "娌℃???ユ?懼?頒換浣??版??!");
        }

    }
    #endregion      

View Code

璋???ffic缁?浠堕??瑕?娣誨??Interop.Microsoft.Office.Interop.Word.dll??寮???????佃??涓?瑁???Office2010锛???浠ユ坊??Microsoft Word 14.0 Object Library??寮???

Word妯℃?垮?ㄦ???″?ㄧ??????濂藉??锛??ㄥ?╃??esponse?規?灏???????word??妗e????缁?瀹㈡?風??涓?杞斤?浠g??濡?涓?锛?      
#region 浠????″?ㄦ??瀹?璺?寰?涓?杞?ord
    /// <summary>
    /// 浠????″?ㄦ??瀹?璺?寰?涓?杞?ord
    /// </summary>
    /// <param name="fileFullName">涓存?舵??浠跺?ㄦ???″?ㄤ???瀹??磋礬寰?</param>
    /// <param name="wordname">涓?杞界??Word??浠跺??</param>
    public void ExtWord(string fileFullName, string wordname)
    {
        //杈???ord
        FileInfo file = new System.IO.FileInfo(fileFullName);
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Charset = "GB2312";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
        // 娣誨??澶翠俊??锛?涓???浠朵?杞?????涓?瀵矽??妗???瀹?榛?璁ゆ??浠跺?? 
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(wordname, System.Text.Encoding.UTF8));
        // 娣誨??澶翠俊??锛???瀹???浠跺ぇ灏?锛?璁╂?瑙??ㄨ?藉??劇ず涓?杞借?搴?
        HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
        // ??瀹?杩???????涓?涓?涓??借?瀹㈡?風??璇誨????娴?锛?蹇?椤昏?涓?杞?
        HttpContext.Current.Response.ContentType = "application/ms-word";
        // ????浠舵??????闆?㈡?風?? 
        HttpContext.Current.Response.WriteFile(file.FullName);
        // ??姝㈤〉?㈢???ц? 
        HttpContext.Current.ApplicationInstance.CompleteRequest();
    }
    #endregion      

View Code

娴?璇?锛?杩?琛?娌¢??棰???

?????灏?寮?????澧?娴?璇?OK??绋?搴???甯??版?e??????″?ㄧ??澧??????幫?涓??借?琛?锛?瀵煎?轟??虹?伴??璇???瑙e?蟲?規?濡?涓?锛?

1??纭?淇????″?ㄤ?宸茬?瀹?瑁?浜?骞舵?娲?ord绋?搴?(寤鴻??瀹?瑁?word2010)锛?

2??褰?寮???????杩?word妯℃?跨????word绋?搴?瀹?????锛??ㄧ講??indows?稿?崇?????″?ㄤ??訛?濡????虹?頒互涓?????璇?(璇誨??word缁?浠剁??Com??????棰?)锛?

? ? ? ? ? ? ? ? ? ? ? ?

3???ョ????璇???瀛??㈡????锛?搴?璇ユ??COM缁?浠剁????缃?????棰?锛?缃?涓??ラ???稿?寵??????版??????????缃?????棰?锛?

4??浠ヤ?姝ラ?ら???規??缁?iis??浜?com缁?浠剁??璁塊??????:

姝ラ?や?锛??懼?闆?煎??ord???ㄥ?扮??com缁?浠訛?

锛?1锛??у?堕?㈡????绠$??宸ュ????缁?浠舵??????璁$???????????佃??-??DCOM??缃?-??Mircosoft Word??妗o?

娉??濡???杩??鋒??浣??句???ircosoft word?稿?蟲??妗o?涓??ㄦ??锛?灏?璇?浠ヤ??規?:

Cmd-??mmc -32-????浠???娣誨??/???ょ?$??????-?????╃?浠舵??????纭?瀹?-??璁$???????????佃??-??DCOM??缃?-???懼??ircosoft Word 97-2003??妗o????т互涓????規???浣??沖??锛?

姝ラ?や?锛??懼??ord??妗e??锛??ㄥ??涓??㈢?瑰?誨?抽??,?跺???瑰??灞???,

寮瑰??Microsoft word 搴??ㄧ?搴?灞???瀵矽??妗?锛?

姝ラ?や?锛??瑰????璇?"??绛?????浜や?寮??ㄦ??

姝ラ?や?锛??瑰??瀹?????绛??????ㄥ??婵?娲繪????"涓??瑰????瀹?涔?",?跺???瑰?誨?瑰???"缂?杈?"????,?ㄥ脊?虹??"瀹??ㄦ??瀵矽??妗?涓?濉??? IIS_IUSRS璐???IIS璐???锛?骞舵???у?涓??炬??绀烘??浜??稿?蟲????锛?

,

姝ラ?ゅ??:渚??舵??"瀹?????绛???璁塊??????"涓??瑰????瀹?涔?",?跺???瑰??缂?杈?",??缃???璐??峰???規???涓?涓?姝ユ??浣?涓??鳳?

??锛?????缃?绔?骞惰?琛?锛??瑰?諱?杞借〃?兼?????蟲??????