ewefr

CSharp語法 下載Word 合併Word Word轉Pdf

張貼者: Tony 2011年9月21日 星期三

using Word = Microsoft.Office.Interop.Word;//這個要先加入Microsoft.Office.Interop.Word參考 
using System.Reflection;//這個記得參考 

下載Word
/// <summary>
        /// 下載Word
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="FileName"></param>
        /// <param name="response"></param>
        /// <returns></returns>
        public static bool DownloadWord(string FilePath, string FileName, HttpResponse response)
        {
            try
            {
                FileInfo DownloadFile = new FileInfo(FilePath + FileName);
                response.Clear();
                response.ClearHeaders();
                response.Buffer = false; 
                 //Response.ContentType指定檔案類型  
                 //可以為application/ms-excel || application/ms-word || application/ms-txt  
                 //application/ms-html || 或其他瀏覽器可以支援的文件 
                response.ContentType = "application/octet-stream";//所有類型 
                 //下面這行很重要, attachment 参数表示作為附件下載,可以改成 online線上開啟 
                 //filename=FileFlow.xls 指定输出檔案名稱,注意其附檔名和指定檔案類型相符, 
                 //可以為:.doc || .xls || .txt ||.htm 
                response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
                response.AppendHeader("Content-Length", DownloadFile.Length.ToString());//取得檔案大小 

                response.WriteFile(DownloadFile.FullName);
                response.Flush();
                //response.End();


                return true;
            }
            catch
            {
                return false;
            }

        }


合併多個Wold
/// <summary>
        /// 合併多個Wold
        /// </summary>
        /// <param name="InFilePath"></param>
        /// <param name="OutFilePath"></param>
        /// <returns></returns>
        public static bool MergeWord(string[] InFilePath,string OutFilePath)
        {
            try
            {
                object missing = Missing.Value; 
                object iLastWord = InFilePath.Last();//最後合併的word
                object oOutputDoc = OutFilePath;//合併檔 
                object oPageBreak = Word.WdBreakType.wdSectionBreakContinuous;
                //object oPageBreak = Word.WdBreakType.wdLineBreak;//接下行合併(LineBreak) 
                //object oPageBreak = Word.WdBreakType.wdPageBreak;//接下頁合併(PageBreak) 

                Word.Application wordApp = new Word.Application();

                Word.Document origDoc = wordApp.Documents.Open(ref iLastWord, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                origDoc.Activate();
              
                for (int i = 0; i < InFilePath.Count();i++)
                {
                    if (i == (InFilePath.Count() - 1) || InFilePath.Count() == 0)
                        break;
                    wordApp.Selection.InsertFile(InFilePath[i], ref missing, ref missing, ref missing, ref missing);
                    wordApp.Selection.InsertBreak(ref oPageBreak);
                }

                wordApp.ActiveDocument.SaveAs(ref oOutputDoc, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); 
                wordApp.ActiveDocument.Close(ref missing, ref missing, ref missing); 
                wordApp.Quit(ref missing, ref missing, ref missing);    //加這行可以 Kill WINWORD.EXE process 

                return true;
            }
            catch
            {
                return false;
            }
        }


Word轉Pdf
/// <summary>
        /// Word轉Pdf
        /// </summary>
        /// <param name="WoldFilePath">Wold路徑</param>
        /// <param name="PdfFilePath">Pdf路徑</param>
        public static bool ConvertWordtoPdf(object WoldFilePath, object PdfFilePath)
        {

            // 在此处放置用户代码以初始化页面
            object Missing = Type.Missing;

            Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();

            //必须设置为不可见
            WordApp.Visible = false;

            try
            {

                //创建以strTemp为模板的文档
                //object oTemplate = Server.MapPath(strTemp);
                Word.Document WordDoc = WordApp.Documents.Add(ref WoldFilePath, ref Missing, ref Missing, ref Missing);
                WordDoc.Activate();


                //对标签"Title"进行填充
                //string strBM = "Title";
                //object objBM = strBM;

                //if (WordApp.ActiveDocument.Bookmarks.Exists(strBM) == true)
                //{
                //    WordApp.ActiveDocument.Bookmarks.get_Item(ref objBM).Select();
                //    WordApp.Selection.TypeText("公文標題");
                //}

                object FileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;

                //保存为新文件
                //object oNewFileName = Server.MapPath(newFileName);
                WordDoc.SaveAs(ref PdfFilePath, FileFormat, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing,
                ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing);
                WordDoc.Close(ref Missing, ref Missing, ref Missing);
                WordApp.Quit(ref Missing, ref Missing, ref Missing);

                return true;
            }
            catch
            {
                return false;
            }

        }
        #endregion

    }

0 意見

張貼留言

The Will Will Web

技術分享
MVP版主

重灌狂人

資訊分享

wowboxBlog

提供網頁設計相關資訊

KingKong Bruce記事

技術分享

C# Examples

csharp相關技術與範例

黑暗執行緒

技術分享
MVP版主

杨延成

技術分享
作者文章對於EF有詳細介紹

月光下的嘆息

工具分享
介紹許多好用的工具程式

Dot blogs

DotNet

格子樑/艾倫郭

技術分享
MVP版主