Пример #1
0
        /// <summary>
        /// 获取word 无格式的text文本
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string WordToText(string path)
        {
            string wordText = "";

            Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();//ApplicationClass();
            Microsoft.Office.Interop.Word.Document    WordDoc;
            Microsoft.Office.Interop.Word.Documents   docs = WordApp.Documents;

            Object oMissing = System.Reflection.Missing.Value;
            Type   wordType = WordApp.GetType();

            object fileName = path;

            WordDoc = WordApp.Documents.Open(ref fileName,
                                             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            wordText = WordDoc.Content.Text;

            WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
            WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
            GC.Collect();

            return(wordText);
        }
Пример #2
0
        public static void ConvertToPDFWithPrinter(String filePath, String destpath)
        {
            // file exist & check type
            if (!filePath.IsNormalized() || !File.Exists(filePath))
            {
                throw new Exception("未指定文件");
            }
            if (!filePath.EndsWith("doc") && !filePath.EndsWith("docx"))
            {
                throw new Exception("文件非 word 类型");
            }

            Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
            Type wordType = word.GetType();

            Microsoft.Office.Interop.Word.Documents docs = word.Documents;
            Type docsType = docs.GetType();

            Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { filePath, true, true });
            doc.Application.ActivePrinter = "Microsoft Print to PDF";
            Type docType = doc.GetType();

            docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument, destpath });
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
        }
Пример #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            object oMissing = System.Reflection.Missing.Value;

            Word.Application oWord = new Word.Application();
            oWord.Visible = true;
            Word.Documents oDocs = oWord.Documents;
            object         oFile = "f:\\Andre.doc";


            /*Word.Document oDoc = oDocs.Open("f:\\Andre.doc", ref oMissing,
             * ref oMissing, ref oMissing, ref oMissing, ref oMissing,
             * ref oMissing, ref oMissing, ref oMissing, ref oMissing,
             * ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
             */
            var oDoc = oWord.Documents.Open("f:\\Andre.docx");

            oWord.Run("Sort");

            oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(oDoc);
            oDoc = null;
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oDocs);
            oDocs = null;
            oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
            oWord = null;
            GC.Collect();
        }
Пример #4
0
        ///<summary>
        /// 将Word文件转换为HTML文件
        ///</summary>
        ///<param name="source_path">Word文件路径(绝对)</param>
        ///<param name="target_path">生成的Html文件路径(绝对),路径的目录结构必须存在,否则保存失败</param>
        ///<returns>是否执行成功</returns>
        public static bool toHtml(string source_path, string target_path)
        {
            Word.Application wordApp  = new Word.Application();
            Type             wordType = wordApp.GetType();

            Word.Documents docs     = wordApp.Documents;
            Type           docsType = docs.GetType();

            try
            {
                if (File.Exists(target_path))
                {
                    File.Delete(target_path);
                }
                Word.Document doc     = (Word.Document)docsType.InvokeMember("Open", BindingFlags.InvokeMethod, null, docs, new Object[] { source_path, true, true });
                Type          docType = doc.GetType();
                docType.InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, doc, new object[] { target_path, Word.WdSaveFormat.wdFormatFilteredHTML });
                return(true);
            }
            catch
            {
                return(false);
            }
            finally
            {
                wordType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, wordApp, null);
                try
                {
                    wordApp.Quit();
                }
                catch { }
                Thread.Sleep(3000);
            }
        }
Пример #5
0
 public MSWord_Controller(string filepath)
 {
     wordApp         = new Word.Application();
     wordApp.Visible = true;
     docs            = wordApp.Documents;
     doc             = docs.Open(filepath);
 }
Пример #6
0
 public MSWord_Controller()
 {
     wordApp         = new Word.Application();
     wordApp.Visible = true;
     docs            = wordApp.Documents;
     doc             = docs.Add();
 }
Пример #7
0
 /// <summary>
 /// word转成html
 /// </summary>
 /// <param name="wordFileName">绝对地址</param>
 public static string wordToHtml(object wordFileName)
 {
     try
     {
         //判断文件夹是否存在(不存在创建一个)
         if (!Directory.Exists(HttpContext.Current.Server.MapPath("/WordToHtml")))
         {
             new DirectoryInfo(HttpContext.Current.Server.MapPath("/WordToHtml")).Create();
         }
         //在此处放置用户代码以初始化页面
         Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
         Type wordType = word.GetType();
         Microsoft.Office.Interop.Word.Documents docs = word.Documents;
         //打开文件
         Type docsType = docs.GetType();
         Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });
         //转换格式,另存为
         Type   docType          = doc.GetType();
         string wordSaveFileName = wordFileName.ToString();
         string strSaveFileName  = "";
         strSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.Length - 3) + "html";
         object saveFileName = (object)strSaveFileName;
         docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
         docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
         //退出 Word
         wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
         return(saveFileName.ToString());
     }
     catch (Exception ex)
     {
         LogHelper.WriteLog(Convert.ToString("Word错误:" + ex.Message + "------------------------------------" + ex.StackTrace));
         throw new Exception(ex.Message, ex);
     }
 }
Пример #8
0
        //</Snippet6>

        static void VS2008()
        {
            var wordApp = new Word.Application();

            wordApp.Visible = true;
            // docs is a collection of all the Document objects currently
            // open in Word.
            Word.Documents docs = wordApp.Documents;

            // Add a document to the collection and name it doc.
            object useDefaultVal = Type.Missing;

            Word.Document doc = docs.Add(ref useDefaultVal, ref useDefaultVal,
                                         ref useDefaultVal, ref useDefaultVal);

            object n = 0;

            Word.Range range = doc.Range(ref n, ref n);

            range.InsertAfter("Testing, testing, testing. . .");

            //<Snippet14>
            // Call to ConvertToTable in Visual C# 2008 or earlier. This code
            // is not part of the solution.
            var    missing   = Type.Missing;
            object separator = ",";

            range.ConvertToTable(ref separator, 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);
            //</Snippet14>
        }
        /// <summary>
        /// Word转成Html
        /// </summary>
        /// <param name="path">要转换的文档的路径</param>
        /// <param name="savePath">转换成html的保存路径</param>
        /// <param name="wordFileName">转换成html的文件名字</param>
        public static void Word2Html(string path, string savePath, string wordFileName)
        {
            MyWord.Application word = new MyWord.Application();
            Type wordType           = word.GetType();

            MyWord.Documents docs     = word.Documents;
            Type             docsType = docs.GetType();

            MyWord.Document doc = (MyWord.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { (object)path, true, true });
            try
            {
                Type   docType         = doc.GetType();
                string strSaveFileName = System.IO.Path.Combine(savePath, $"{wordFileName}.html");
                object saveFileName    = (object)strSaveFileName;
                docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, MyWord.WdSaveFormat.wdFormatFilteredHTML });
                docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
                wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            }
            catch
            { }
            finally
            {
                doc  = null;
                docs = null;
                word = null;
            }
        }
Пример #10
0
        public ActionResult Document(HttpPostedFileBase file)
        {
            if (file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path     = Path.Combine(Server.MapPath("~/App_Data"), fileName);
                file.SaveAs(path);


                object            Sourcepath = path;
                object            TargetPath = Server.MapPath("~/App_Data/document.html");
                Word._Application newApp     = new Word.Application();
                Word.Documents    d          = newApp.Documents;
                object            Unknown    = Type.Missing;
                Word.Document     od         = d.Open(ref Sourcepath, ref Unknown,
                                                      ref Unknown, ref Unknown, ref Unknown,
                                                      ref Unknown, ref Unknown, ref Unknown,
                                                      ref Unknown, ref Unknown, ref Unknown,
                                                      ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                object format = Word.WdSaveFormat.wdFormatHTML;



                newApp.ActiveDocument.SaveAs(ref TargetPath, ref format,
                                             ref Unknown, ref Unknown, ref Unknown,
                                             ref Unknown, ref Unknown, ref Unknown,
                                             ref Unknown, ref Unknown, ref Unknown,
                                             ref Unknown, ref Unknown, ref Unknown,
                                             ref Unknown, ref Unknown);

                newApp.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
            }
            return(View());
        }
Пример #11
0
        /// <summary>
        /// 需要转换的Word文档路径
        /// </summary>
        /// <param name="fileName">完整路径</param>
        /// <returns>HTML页面路径</returns>
        private static string WordToHtml(object fileName)
        {
            Word.Application word     = new Word.Application();
            Type             wordType = word.GetType();

            Word.Documents docs = word.Documents;

            //打开文件
            Type docsType = docs.GetType();

            Word.Document doc = (Word.Document)docsType.InvokeMember("Open", BindingFlags.InvokeMethod, null, docs, new object[] { fileName, true, true });

            //转换格式,另存
            Type   docType          = doc.GetType();
            string wordSaveFileName = fileName.ToString();
            string htmlSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.LastIndexOf(".") + 1) + "html";
            object saveFileName     = (object)htmlSaveFileName;

            docType.InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
            docType.InvokeMember("Close", BindingFlags.InvokeMethod, null, doc, null);

            //退出Word
            wordType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, word, null);

            return(saveFileName.ToString());
        }
Пример #12
0
        /// <summary>
        /// word转成html
        /// </summary>
        /// <param name="wordFileName"></param>
        private string WordToHtml(object wordFileName, string htmlFileName)
        {
            object saveFileName = "";

            try
            {
                //在此处放置用户代码以初始化页面
                Word.ApplicationClass word = new Word.ApplicationClass();
                Type           wordType    = word.GetType();
                Word.Documents docs        = word.Documents;
                //打开文件
                Type          docsType = docs.GetType();
                Word.Document doc      = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });
                //转换格式,另存为
                Type   docType          = doc.GetType();
                string wordSaveFileName = wordFileName.ToString();


                string strSaveFileName = htmlFileName;
                saveFileName = (object)strSaveFileName;
                docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
                docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
                //退出 Word
                wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            }
            catch (Exception ex)
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), null, string.Format("<script>alert('{0}');</script>", ex.Message.ToString()));
            }
            return(saveFileName.ToString());
        }
Пример #13
0
        public override void Dispose()
        {
            if (_app == null)
            {
                // 已经释放,无需再释放
                return;
            }

            Console.WriteLine("关闭Word!");

            try
            {
                if (_docs != null)
                {
                    _docs.Close(false);
                    ReleaseCOMObject(_docs);
                    _docs = null;
                }

                _app.Quit(false);
                ReleaseCOMObject(_app);
                _app = null;
            }
            catch
            {
                // TODO 退出Wold程序失败
            }
        }
Пример #14
0
        public WordConverter()
        {
            _app         = new Word.Application();
            _app.Visible = false;

            _docs = _app.Documents;
        }
Пример #15
0
        public override void Convert(String inputFile, String outputFile)
        {
            Object nothing = System.Reflection.Missing.Value;

            try
            {
                if (!File.Exists(inputFile))
                {
                    throw new ConvertException("File not Exists");
                }

                if (IsPasswordProtected(inputFile))
                {
                    throw new ConvertException("Password Exist");
                }

                app  = new Word.Application();
                docs = app.Documents;
                doc  = docs.Open(inputFile, false, true, false, nothing, nothing, true, nothing, nothing, nothing, nothing, false, false, nothing, true, nothing);
                doc.ExportAsFixedFormat(outputFile, Word.WdExportFormat.wdExportFormatPDF, false, Word.WdExportOptimizeFor.wdExportOptimizeForOnScreen, Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument, 1, 1, Word.WdExportItem.wdExportDocumentContent, false, false, Word.WdExportCreateBookmarks.wdExportCreateNoBookmarks, false, false, false, nothing);
            }
            catch (Exception e)
            {
                release();
                throw new ConvertException(e.Message);
            }
            release();
        }
Пример #16
0
        public override void Convert(String inputFile, String outputFile)
        {
            Object nothing = System.Reflection.Missing.Value;
            try
            {
                if (!File.Exists(inputFile))
                {
                    throw new ConvertException("File not Exists");
                }

                if (IsPasswordProtected(inputFile))
                {
                    throw new ConvertException("Password Exist");
                }

                app = new Word.Application();
                docs = app.Documents;
                doc = docs.Open(inputFile, false, true, false, nothing, nothing, true, nothing, nothing, nothing, nothing, false, false, nothing, true, nothing);
                doc.ExportAsFixedFormat(outputFile, Word.WdExportFormat.wdExportFormatPDF, false, Word.WdExportOptimizeFor.wdExportOptimizeForOnScreen, Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument, 1, 1, Word.WdExportItem.wdExportDocumentContent, false, false, Word.WdExportCreateBookmarks.wdExportCreateNoBookmarks, false, false, false, nothing);
            }
            catch (Exception e)
            {
                release();
                throw new ConvertException(e.Message);
            }
            release();
        }
Пример #17
0
        public void GenerateWordFile()
        {
            Word._Application wApp  = new Word.Application();
            Word.Documents    wDocs = wApp.Documents;
            Word._Document    wDoc  = wDocs.Open(@"C:\Users\s.stadtler\source\repos\FST01\Sourcecode\FST.TournamentPlanner\FST.TournamentPlanner.Test\Urkunde_v1_edit.docx", ReadOnly: false);
            wDoc.Activate();

            Word.Bookmarks wBookmarks = wDoc.Bookmarks;
            Word.Bookmark  wBookmark  = wBookmarks["Tuniername"];
            Word.Range     wRange     = wBookmark.Range;
            wRange.Text = "Supertunier";
        }
Пример #18
0
        //<Snippet6>
        static void DisplayInWord()
        {
            var wordApp = new Word.Application();

            wordApp.Visible = true;
            // docs is a collection of all the Document objects currently
            // open in Word.
            Word.Documents docs = wordApp.Documents;

            // Add a document to the collection and name it doc.
            Word.Document doc = docs.Add();
        }
Пример #19
0
        public Word(string template = @"C:\Users\ReaLBERG\Desktop\3 курс\АИС\LB9\lb9tttt.doc")
        {
            wordapp         = new word.Application();
            wordapp.Visible = true;
            Object newTemplate  = false;
            Object documentType = word.WdNewDocumentType.wdNewBlankDocument;
            Object visible      = true;

            wordapp.Documents.Add(template, newTemplate, ref documentType, ref visible);
            worddocuments = wordapp.Documents;
            worddocument  = worddocuments.get_Item(1);
            worddocument.Activate();
        }
Пример #20
0
        /// <summary>
        /// word转成html
        /// </summary>
        /// <param name="wordFileName"></param>
        private string WordToHtml(object wordFileName, string htmlFileName)
        {
            object saveFileName = "";

            try
            {
                //在此处放置用户代码以初始化页面
                Word.ApplicationClass word = new Word.ApplicationClass();
                if (word == null)
                {
                    ServiceAppSetting.LoggerHander.Invoke("word is null", "Error");

                    return("");
                }
                Type           wordType = word.GetType();
                Word.Documents docs     = word.Documents;

                if (docs == null)
                {
                    ServiceAppSetting.LoggerHander.Invoke("docs is null", "Error");
                    return("");
                }
                //打开文件
                Type          docsType = docs.GetType();
                Word.Document doc      = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });

                if (doc == null)
                {
                    ServiceAppSetting.LoggerHander.Invoke("doc is null", "Error");
                    return("");
                }
                //转换格式,另存为
                Type   docType          = doc.GetType();
                string wordSaveFileName = wordFileName.ToString();


                string strSaveFileName = htmlFileName;
                saveFileName = (object)strSaveFileName;
                docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
                docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
                //退出 Word
                wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            }
            catch (Exception ex)
            {
                ServiceAppSetting.LoggerHander.Invoke(string.Format("<script>alert('{0}!');</script>", ex.Message), "Error");
            }

            return(saveFileName.ToString());
        }
Пример #21
0
        public ActionResult ReadTable(HttpPostedFileBase uploadfile)
        {
            uploadfile.SaveAs(Server.MapPath("\\Resume\\") + uploadfile.FileName);
            string filepath = Server.MapPath("\\Resume\\" + uploadfile.FileName.ToString());

            Microsoft.Office.Interop.Word.Application app  = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Documents   docs = app.Documents;
            Document doc   = docs.Open(filepath, ReadOnly: true);
            Table    t     = doc.Tables[1];
            Range    r     = t.Range;
            Cells    cells = r.Cells;

            tbl_model tmodel = new tbl_model();

            tmodel.name          = cells[3].Range.Text.ToString();
            tmodel.cnt           = cells[5].Range.Text.ToString();
            tmodel.qualification = cells[7].Range.Text;
            tmodel.email         = cells[9].Range.Text;

            for (int i = 1; i <= cells.Count; i++)
            {
                Cell   cell = cells[i];
                Range  r2   = cell.Range;
                String txt  = r2.Text;
                Marshal.ReleaseComObject(cell);
                Marshal.ReleaseComObject(r2);
            }

            //Rows rows = t.Rows;
            //Columns cols = t.Columns;
            // Cannot access individual rows in this collection because the table has vertically merged cells.
            //for (int i = 0; i < rows.Count; i++) {
            //  for (int j = 0; j < cols.Count; j++) {
            //      Cell cell = rows[i].Cells[j];
            //      Range r = cell.Range;
            //  }
            //}

            doc.Close(false);
            app.Quit(false);
            //Marshal.ReleaseComObject(cols);
            //Marshal.ReleaseComObject(rows);
            Marshal.ReleaseComObject(cells);
            Marshal.ReleaseComObject(r);
            Marshal.ReleaseComObject(t);
            Marshal.ReleaseComObject(doc);
            Marshal.ReleaseComObject(docs);
            Marshal.ReleaseComObject(app);
            return(View(tmodel));
        }
Пример #22
0
        public COMFormatter(string template = @"D:\a2.doc")
        {
            wordapp.Visible = true;

            Object newTemplate  = false;
            Object documentType = word.WdNewDocumentType.wdNewBlankDocument;
            Object visible      = true;

            wordapp.Documents.Add(template, newTemplate, ref documentType, ref visible);

            worddocuments = wordapp.Documents;
            worddocument  = worddocuments.get_Item(1);
            worddocument.Activate();
        }
Пример #23
0
        public Word.Document CreateWordDoc()
        {
            try
            {
                //新建一个文档
                m_WordDocs = m_WordApp.Documents;
                m_WordDoc  = m_WordDocs.Add(ref missing, ref missing, ref missing, ref missing);
            }
            catch (System.Exception)
            {
                m_WordDoc = null;
            }

            return(m_WordDoc);
        }
Пример #24
0
        /// <summary>
        /// Generates the winners certificates and stores them to a file
        ///
        /// The file path is returned
        /// </summary>
        /// </summary>
        /// <param name="name"></param>
        /// <param name="rank"></param>
        /// <param name="tournamentname"></param>
        /// <param name="place"></param>
        /// <param name="date"></param>
        /// <returns>Path to generated certificate</returns>
        public static XpsDocument Generate(string name, int rank, string tournamentname, string place, string date)
        {
            Word.Application wApp = new Word.Application();
            wApp.Visible = false;

            Word.Documents wDocs        = wApp.Documents;
            object         missing      = System.Reflection.Missing.Value;
            string         templatePath = AppDomain.CurrentDomain.BaseDirectory + @"WinnerCertificatesTemplate\Urkunde_v2_final.docm";

            Word.Document wDoc = wDocs.Open(templatePath,
                                            missing,
                                            ReadOnly: false,
                                            Visible: false,
                                            Revert: false);
            wApp.WindowState = WdWindowState.wdWindowStateMinimize;
            wDoc.Activate();

            Word.Bookmarks wBookmarks = wDoc.Bookmarks;
            Word.Bookmark  wBookmark  = wBookmarks["TournamentName"];
            Word.Range     wRange     = wBookmark.Range;
            wRange.Text = tournamentname;

            Word.Bookmark wBookmark2 = wBookmarks["Name"];
            Word.Range    wRange2    = wBookmark2.Range;
            wRange2.Text = name;

            Word.Bookmark wBookmark3 = wBookmarks["Rank"];
            Word.Range    wRange3    = wBookmark3.Range;
            wRange3.Text = rank.ToString();

            Word.Bookmark wBookmark4 = wBookmarks["Place"];
            Word.Range    wRange4    = wBookmark4.Range;
            wRange4.Text = place;

            Word.Bookmark wBookmark5 = wBookmarks["Date"];
            Word.Range    wRange5    = wBookmark5.Range;
            wRange5.Text = date;

            string convertedXpsDoc = string.Concat(Path.GetTempPath(), "\\", Guid.NewGuid().ToString(), ".xps");

            wDoc.SaveAs(convertedXpsDoc, WdSaveFormat.wdFormatXPS);
            XpsDocument xpsDocument = new XpsDocument(convertedXpsDoc, FileAccess.Read);

            wDoc.Close(SaveChanges: false);
            wApp.Quit();

            return(xpsDocument);
        }
Пример #25
0
        static void DisplayInWord()
        {
            var wordApp = new Word.Application();

            wordApp.Visible = true;
            // docs is a collection of all the Document objects currently
            // open in Word.
            Word.Documents docs = wordApp.Documents;

            // Add a document to the collection and name it doc.
            Word.Document doc = docs.Add();

            //<Snippet7>
            // Define a range, a contiguous area in the document, by specifying
            // a starting and ending character position. Currently, the document
            // is empty.
            Word.Range range = doc.Range(0, 0);

            // Use the InsertAfter method to insert a string at the end of the
            // current range.
            range.InsertAfter("Testing, testing, testing. . .");
            //</Snippet7>

            // You can comment out any or all of the following statements to
            // see the effect of each one in the Word document.

            // Next, use the ConvertToTable method to put the text into a table.
            // The method has 16 optional parameters. You only have to specify
            // values for those you want to change.

            //<Snippet9>
            // Convert to a simple table. The table will have a single row with
            // three columns.
            range.ConvertToTable(Separator: ",");
            //</Snippet9>

            // Change to a single column with three rows..
            //<Snippet10>
            range.ConvertToTable(Separator: ",", AutoFit: true, NumColumns: 1);
            //</Snippet10>

            // Format the table.
            //<Snippet11>
            range.ConvertToTable(Separator: ",", AutoFit: true, NumColumns: 1,
                                 Format: Word.WdTableFormat.wdTableFormatElegant);
            //</Snippet11>
        }
Пример #26
0
        /// <summary>生成PDF文件</summary>
        public override bool Execute(string sourceFile, string destFile)
        {
            bool success = true;

            Microsoft.Office.Interop.Word.ApplicationClass application = null;
            Microsoft.Office.Interop.Word.Documents        documents   = null;
            Microsoft.Office.Interop.Word.Document         document    = null;
            try
            {
                application = new Microsoft.Office.Interop.Word.ApplicationClass();
                //Type wordType = application.GetType();
                documents = application.Documents;

                // 打开文件
                Type docsType = documents.GetType();
                document = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
                                                                                         System.Reflection.BindingFlags.InvokeMethod, null, documents, new Object[] { sourceFile, true, true });

                Type docType = document.GetType();
                docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
                                     null, document, new object[] { destFile, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
            }
            catch (Exception)
            {
                success = false;
            }
            finally
            {
                if (document != null)
                {
                    document.Close();
                    Marshal.ReleaseComObject(document);
                }
                if (application != null)
                {
                    application.Quit();
                    Marshal.ReleaseComObject(application);
                }
                //关闭文档
                //docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, document, new object[] { null, null, null });

                //退出 Word
                //wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, application, null);
            }

            return(success);
        }
Пример #27
0
        /// <summary>
        /// word转成html
        /// </summary>
        /// <param name="path">要转换的文档的路径</param>
        /// <param name="savePath">转换成的html的保存路径</param>
        /// <param name="wordFileName">转换后html文件的名字</param>
        //private static void WordToHtml(string path, string savePath, string wordFileName)
        //{
        //    Word.ApplicationClass word = new Word.ApplicationClass();
        //    Type wordwordType = word.GetType();
        //    Word.Documents docs = word.Documents;
        //    Type docsdocsType = docs.GetType();
        //    Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { (object)path, true, true });
        //    Type docdocType = doc.GetType();
        //    string strSaveFileName = savePath + wordFileName + ".html";
        //    object saveFileName = (object)strSaveFileName;
        //    docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
        //    docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
        //    wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
        //}
        private static void WordToHtml(string path)
        {
            Word.Application word     = new Word.Application();
            Type             wordType = word.GetType();

            Word.Documents docs     = word.Documents;
            Type           docsType = docs.GetType();

            Word.Document doc             = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { (object)path, true, true });
            Type          docdocType      = doc.GetType();
            string        strSaveFileName = path.Substring(0, path.Length - 3) + ".html";
            object        saveFileName    = (object)strSaveFileName;

            docsType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
            docsType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
        }
Пример #28
0
    /// <summary>
    /// Word转成Html
    /// </summary>
    /// <param name="path">要转换的文档的路径</param>
    /// <param name="savePath">转换成html的保存路径</param>
    /// <param name="wordFileName">转换成html的文件名字</param>
    public static void Word2Html(string path, string savePath, string wordFileName)
    {
        Word.ApplicationClass word = new Word.ApplicationClass();
        Type wordType = word.GetType();

        Word.Documents docs     = word.Documents;
        Type           docsType = docs.GetType();

        Word.Document doc             = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { (object)path, true, true });
        Type          docType         = doc.GetType();
        string        strSaveFileName = savePath + wordFileName + ".html";
        object        saveFileName    = (object)strSaveFileName;

        docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
        docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
        wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
    }
Пример #29
0
            public WordReportTopBookOfGenre(string pathFrom)
            {
                path            = pathFrom;
                wordapp         = new word.Application();
                wordapp.Visible = true;

                object newTemplate  = false;
                object documentType = word.WdNewDocumentType.wdNewBlankDocument;
                object visible      = true;

                wordapp.Documents.Add(path, newTemplate, ref documentType, ref visible);

                worddocuments = wordapp.Documents;
                worddocument  = worddocuments.get_Item(1);
                //wordapp.Visible = false;
                worddocument.Activate();
            }
Пример #30
0
    protected void Button4_Click(object sender, EventArgs e)
    {
        object o      = Missing.Value;
        object oFalse = false;
        object oTrue  = true;

        Word._Application app  = null;
        Word.Documents    docs = null;
        Word.Document     doc  = null;

        object path = @"C:\pru\otro.doc";

        try
        {
            app               = new Word.Application();
            app.Visible       = true;
            app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;

            docs = app.Documents;
            //doc = docs.Open(ref path, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
            // segundo doc = docs.Open(ref path, ref o, false, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
            //doc = docs.Open(ref path, Missing.Value, true, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

            doc = docs.Open(ref path, Missing.Value, true, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

            doc.Activate();
        }
        finally
        {
            if (doc != null)
            {
                Marshal.FinalReleaseComObject(doc);
            }

            if (docs != null)
            {
                Marshal.FinalReleaseComObject(docs);
            }

            if (app != null)
            {
                Marshal.FinalReleaseComObject(app);
            }
        }
    }
Пример #31
0
        //private Dictionary<string, string> Bookmarks = null;

        public WordTemplate(string templatePath)
        {
            this.Type         = TemplateType.WordTemplate;
            this.TemplatePath = templatePath;

            oWord = new Application();
            oDoc  = new Document();

            object readOnly  = false;
            object isVisible = false;
            object oTemplate = TemplatePath;

            Microsoft.Office.Interop.Word.Documents oDocTmp = oWord.Documents;
            oWord.Visible = false;

            try
            {
                oDoc = oDocTmp.Open(ref oTemplate,
                                    ref _missing,
                                    ref readOnly,
                                    ref _missing,
                                    ref _missing,
                                    ref _missing,
                                    ref _missing,
                                    ref _missing,
                                    ref _missing,
                                    ref _missing,
                                    ref _missing,
                                    ref isVisible,
                                    ref _missing,
                                    ref _missing,
                                    ref _missing,
                                    ref _missing);
            }
            catch (Exception ex)
            {
                this.Dispose();
                throw new Exception("Не удалось открыть документ.\r\n" + ex.Message);
            }
            oBookmarks = oDoc.Bookmarks;
        }
Пример #32
0
        /// <summary>
        /// コンストラクタ
        /// </summary>
        public WordMachine()
        {
            application = new Word.Application();
            application.Visible = false;

            documents = application.Documents;

            objects = new List<Object>();
            objects.Add(application);
            objects.Add(documents);
        }