Example #1
0
        public static void InsertImageInWord(string wordPath, string imagePath)
        {
            Word._Application wordApp = new Word.Application();
            wordApp.Visible = true;
            Word._Document wordDoc = wordApp.Documents.Open(wordPath, ReadOnly: false, Visible: true);


            /* -- insert at Bookmark
             * int count = wordDoc.Bookmarks.Count;
             * for (int i = 1; i < count + 1; i++)
             * {
             *  object oRange = wordDoc.Bookmarks[i].Range;
             *  object saveWithDocument = true;
             *  object missing = Type.Missing;
             *  wordDoc.InlineShapes.AddPicture(imagePath, ref missing, ref saveWithDocument, ref oRange);
             * }
             */

            object oEndOfDoc        = "\\endofdoc";
            object saveWithDocument = true;
            object missing          = System.Reflection.Missing.Value;
            object wrdRng           = wordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;

            wordDoc.InlineShapes.AddPicture(imagePath, ref missing, ref saveWithDocument, ref wrdRng);

            wordDoc.Save();
            wordApp.Quit();
            wordDoc = null;
            wordApp = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        /// <summary>
        /// this is the datasource file for the mail merge activity
        /// </summary>
        private void CreateMailMergeDataFile()
        {
            int iCount;

            Object oName   = _tempDataSourceFileName;
            Object oHeader = "Name, Address";

            _wrdDoc.MailMerge.CreateDataSource(ref oName, ref _oMissing,
                                               ref _oMissing, ref oHeader, ref _oMissing, ref _oMissing,
                                               ref _oMissing, ref _oMissing, ref _oMissing);

            // Open the file to insert data.
            Word._Document oDataDoc = _wrdApp.Documents.Open(ref oName, 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 */);

            for (iCount = 1; iCount < _mailMergeData.RecipientCollection.Count; iCount++)
            {
                oDataDoc.Tables[1].Rows.Add(ref _oMissing);
            }
            // Fill in the data.
            for (int i = 0; i < _mailMergeData.RecipientCollection.Count; i++)
            {
                FillRow(oDataDoc, i + 2, _mailMergeData.RecipientCollection[i].Name, _mailMergeData.RecipientCollection[i].Address);
            }

            // Save and close the file.
            oDataDoc.Save();
            oDataDoc.Close(ref _oFalse, ref _oMissing, ref _oMissing);
        }
Example #3
0
        /// <summary>
        /// 给word文档添加页眉
        /// </summary>
        /// <param name="filePath">文件名</param>
        /// <returns></returns>
        public static bool AddPageHeaderFooterForWord(string filePath)
        {
            try
            {
                Object oMissing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
                WordApp.Visible = true;
                object filename = filePath;
                Microsoft.Office.Interop.Word._Document 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);

                ////添加页眉方法一:
                //WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
                //WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
                //WordApp.ActiveWindow.ActivePane.Selection.InsertAfter( "**公司" );//页眉内容

                ////添加页眉方法二:
                if (WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdNormalView ||
                    WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdOutlineView)
                {
                    WordApp.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView;
                }
                WordApp.ActiveWindow.View.SeekView            = WdSeekView.wdSeekCurrentPageHeader;
                WordApp.Selection.HeaderFooter.LinkToPrevious = false;
                //WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight;
                WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphDistribute;
                WordApp.Selection.HeaderFooter.Range.Text   = "                                             内部公开";
                WordApp.Selection.InlineShapes.AddPicture(@"C:\Users\Administrator\Desktop\2.jpg");

                // 去掉页眉的横线
                //WordApp.ActiveWindow.ActivePane.Selection.ParagraphFormat.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleNone;
                //WordApp.ActiveWindow.ActivePane.Selection.Borders[WdBorderType.wdBorderBottom].Visible = false;

                //在页眉的图片后面追加几个字
                //WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("  文档页眉");


                WordApp.ActiveWindow.View.SeekView            = WdSeekView.wdSeekCurrentPageFooter;
                WordApp.Selection.HeaderFooter.LinkToPrevious = false;
                WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                WordApp.Selection.HeaderFooter.Range.Text = "页脚内容";
                //WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("页脚内容");

                //跳出页眉页脚设置
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;

                //保存
                WordDoc.Save();
                WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                return(false);
            }
        }
Example #4
0
        /// <summary>
        /// 设置文档格式并添加内容
        /// </summary>
        /// <param name="filePath">文件名</param>
        /// <returns></returns>
        public static bool AddContent(string filePath)
        {
            try
            {
                Object oMissing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word._Application WordApp = new Application();
                WordApp.Visible = true;
                object filename = filePath;
                Microsoft.Office.Interop.Word._Document 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);

                //设置居左
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;

                //设置文档的行间距
                WordApp.Selection.ParagraphFormat.LineSpacing = 15f;
                //插入段落
                //WordApp.Selection.TypeParagraph();
                Microsoft.Office.Interop.Word.Paragraph para;
                para = WordDoc.Content.Paragraphs.Add(ref oMissing);
                //正常格式
                para.Range.Text = "This is paragraph 1";
                //para.Range.Font.Bold = 2;
                //para.Range.Font.Color = WdColor.wdColorRed;
                //para.Range.Font.Italic = 2;
                para.Range.InsertParagraphAfter();

                para.Range.Text = "This is paragraph 2";
                para.Range.InsertParagraphAfter();

                //插入Hyperlink
                Microsoft.Office.Interop.Word.Selection mySelection = WordApp.ActiveWindow.Selection;
                mySelection.Start = 9999;
                mySelection.End   = 9999;
                Microsoft.Office.Interop.Word.Range myRange = mySelection.Range;

                Microsoft.Office.Interop.Word.Hyperlinks myLinks = WordDoc.Hyperlinks;
                object linkAddr = @"http://www.cnblogs.com/lantionzy";
                Microsoft.Office.Interop.Word.Hyperlink myLink = myLinks.Add(myRange, ref linkAddr,
                                                                             ref oMissing);
                WordApp.ActiveWindow.Selection.InsertAfter("\n");

                //落款
                WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();
                WordDoc.Paragraphs.Last.Alignment  = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;

                //保存
                WordDoc.Save();
                WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                return(false);
            }
        }
Example #5
0
        /// <summary>
        /// 把Word文档转化为Html文件
        /// </summary>
        /// <param name="wordFileName">word文件名</param>
        /// <param name="htmlFileName">要保存的html文件名</param>
        /// <returns></returns>
        public static bool WordToHtml(string wordFileName, string htmlFileName)
        {
            try
            {
                Object oMissing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word._Application WordApp = new Application();
                WordApp.Visible = true;
                object filename = wordFileName;
                Microsoft.Office.Interop.Word._Document 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);

                // Type wordType = WordApp.GetType();
                // 打开文件
                Type docsType = WordApp.Documents.GetType();
                // 转换格式,另存为
                Type   docType      = WordDoc.GetType();
                object saveFileName = htmlFileName;
                docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, WordDoc,
                                     new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML });

                #region 其它格式:
                ///wdFormatHTML
                ///wdFormatDocument
                ///wdFormatDOSText
                ///wdFormatDOSTextLineBreaks
                ///wdFormatEncodedText
                ///wdFormatRTF
                ///wdFormatTemplate
                ///wdFormatText
                ///wdFormatTextLineBreaks
                ///wdFormatUnicodeText
                // 退出 Word
                //wordType.InvokeMember( "Quit", System.Reflection.BindingFlags.InvokeMethod,
                //    null, WordApp, null );
                #endregion

                //保存
                WordDoc.Save();
                WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                return(false);
            }
        }
Example #6
0
        /// <summary>
        /// 给word文档添加页眉
        /// </summary>
        /// <param name="filePath">文件名</param>
        /// <returns></returns>
        public static bool AddPageHeaderFooter(string filePath)
        {
            try
            {
                Object oMissing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word._Application WordApp = new Application();
                WordApp.Visible = true;
                object filename = filePath;
                Microsoft.Office.Interop.Word._Document 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);

                ////添加页眉方法一:
                //WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
                //WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
                //WordApp.ActiveWindow.ActivePane.Selection.InsertAfter( "**公司" );//页眉内容

                ////添加页眉方法二:
                if (WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdNormalView ||
                    WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdOutlineView)
                {
                    WordApp.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView;
                }
                WordApp.ActiveWindow.View.SeekView            = WdSeekView.wdSeekCurrentPageHeader;
                WordApp.Selection.HeaderFooter.LinkToPrevious = false;
                WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                WordApp.Selection.HeaderFooter.Range.Text = "页眉内容";

                WordApp.ActiveWindow.View.SeekView            = WdSeekView.wdSeekCurrentPageFooter;
                WordApp.Selection.HeaderFooter.LinkToPrevious = false;
                WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("页脚内容");

                //跳出页眉页脚设置
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;

                //保存
                WordDoc.Save();
                WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                return(false);
            }
        }
Example #7
0
        public static void CreateDocument(string fileName)
        {
            Word._Application app      = new Word.Application();
            object            oMissing = System.Reflection.Missing.Value;

            Word._Document oDoc = app.Documents.Add(ref oMissing, ref oMissing,
                                                    ref oMissing, ref oMissing);
            app.ActiveDocument.SaveAs2(fileName);

            oDoc.Save();
            app.Quit();
            oDoc = null;
            app  = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
Example #8
0
        private void CreateMailMergeDataFile(DataTable tabla)
        {
            Object oName    = tempFileRoute;
            Object oHeader2 = BuildHeader(tabla);

            wrdDoc.MailMerge.CreateDataSource(ref oName, ref oMissing, ref oMissing, ref oHeader2, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            // Open the file to insert data.
            Word._Document oDataDoc = wrdApp.Documents.Open(ref oName, 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);

            // Fill in the data.
            FillDataSource(oDataDoc, tabla);

            // Save and close the file.
            oDataDoc.Save();
            oDataDoc.Close(ref oFalse, ref oMissing, ref oMissing);
        }
Example #9
0
        /// <summary>
        /// 文档中添加图片
        /// </summary>
        /// <param name="filePath">word文件名</param>
        /// <param name="picPath">picture文件名</param>
        /// <returns></returns>
        public static bool AddPicture(string filePath, string picPath)
        {
            try
            {
                Object oMissing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word._Application WordApp = new Application();
                WordApp.Visible = true;
                object filename = filePath;
                Microsoft.Office.Interop.Word._Document 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);

                //移动光标文档末尾
                object count  = WordDoc.Paragraphs.Count;
                object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdParagraph;
                WordApp.Selection.MoveDown(ref WdLine, ref count, ref oMissing); //移动焦点
                WordApp.Selection.TypeParagraph();                               //插入段落

                object LinkToFile       = false;
                object SaveWithDocument = true;
                object Anchor           = WordDoc.Application.Selection.Range;
                WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(picPath, ref LinkToFile, ref SaveWithDocument, ref Anchor);

                //保存
                WordDoc.Save();
                WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                return(false);
            }
        }
Example #10
0
 /// <summary>
 /// 保存
 /// </summary>
 public void Save()
 {
     _doc.Save();
 }
Example #11
0
        public static bool WordReplace(string filePath, Dictionary <string, string> datas)
        {
            bool result = false;

            Microsoft.Office.Interop.Word._Application app = null;
            Microsoft.Office.Interop.Word._Document    doc = null;

            object nullobj = Type.Missing;
            object file    = filePath;


            try
            {
                app = new Microsoft.Office.Interop.Word.Application();//new Microsoft.Office.Interop.Word.ApplicationClass();

                doc = app.Documents.Open(
                    ref file, ref nullobj, ref nullobj,
                    ref nullobj, ref nullobj, ref nullobj,
                    ref nullobj, ref nullobj, ref nullobj,
                    ref nullobj, ref nullobj, ref nullobj,
                    ref nullobj, ref nullobj, ref nullobj, ref nullobj) as Microsoft.Office.Interop.Word._Document;
                object tmp          = "tt1";
                object missingValue = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word.Range startRange = app.ActiveDocument.Bookmarks.get_Item(ref tmp).Range;


                //删除指定书签位置后的第一个表格
                //Microsoft.Office.Interop.Word.Table tbl = startRange.Tables[1];
                //tbl.Delete();

                //添加表格
                for (int i = 0; i < 8; i++)
                {
                    MSWord.Table table = doc.Tables.Add(startRange, 3, 2, ref missingValue, ref missingValue);
                    table.AllowAutoFit    = true;
                    table.Borders.Enable  = 1;                                      //这个值可以设置得很大,例如5、13等等
                    table.Rows.HeightRule = MSWord.WdRowHeightRule.wdRowHeightAuto; //高度规则是:行高有最低值下限?
                    //table.Rows.Height = 15;//
                    table.Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
                    table.Range.ParagraphFormat.WordWrap  = 0;
                    table.Columns.PreferredWidthType      = MSWord.WdPreferredWidthType.wdPreferredWidthPercent;
                    table.Columns[1].PreferredWidth       = 30;
                    table.Columns[2].PreferredWidth       = 70;
                    table.Range.Font.Size       = 10.5F;
                    table.Range.Font.Bold       = 0;
                    table.Cell(1, 1).Range.Text = "文件名";
                    //table.Cell(1, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;

                    table.Cell(2, 1).Range.Text = "SHA256校验码";
                    table.Cell(2, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
                    table.Cell(3, 1).Range.Text = "时间戳时间";
                    table.Cell(3, 1).Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
                    table.Cell(1, 2).Range.Text = "智臻链";
                    table.Cell(2, 2).Range.Text = "rerererererererererererererererererererererereerererererererererererererererererererererererererererererererererererererererererererererererererere";
                    table.Cell(2, 2).HeightRule = MSWord.WdRowHeightRule.wdRowHeightAuto;
                    table.Cell(3, 2).Range.Text = "15393034920943048";
                }



                object objReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;


                foreach (var item in datas)
                {
                    app.Selection.Find.ClearFormatting();
                    app.Selection.Find.Replacement.ClearFormatting();
                    string oldStr = item.Key;                       //需要被替换的文本
                                        string newStr = item.Value; //替换文本 


                                        if (newStr == null)
                    {
                        newStr = "";
                    }
                    int newStrLength = newStr.Length;
                    if (newStrLength <= 255) // 长度未超过255时,直接替换
                    {
                                            {
                            app.Selection.Find.Text             = oldStr;
                            app.Selection.Find.Replacement.Text = newStr;


                            app.Selection.Find.Execute(ref nullobj, ref nullobj, ref nullobj,
                                                       ref nullobj, ref nullobj, ref nullobj,
                                                       ref nullobj, ref nullobj, ref nullobj,
                                                       ref nullobj, ref objReplace, ref nullobj,
                                                       ref nullobj, ref nullobj, ref nullobj);
                            continue;
                        }
                    }


                                        //word文本替换长度不能超过255
                                        int hasSubLength = 0;
                    int itemLength = 255 - oldStr.Length;
                    while (true)
                    {
                        string itemStr   = "";
                        int    subLength = 0;
                        if (newStrLength - hasSubLength <= 255)
                        {
                                                                 // 剩余的内容不超过255,直接替换
                                                    {
                                app.Selection.Find.ClearFormatting();
                                app.Selection.Find.Replacement.ClearFormatting();


                                app.Selection.Find.Text             = oldStr;
                                app.Selection.Find.Replacement.Text = newStr.Substring(hasSubLength, newStrLength - hasSubLength);


                                app.Selection.Find.Execute(ref nullobj, ref nullobj, ref nullobj,
                                                           ref nullobj, ref nullobj, ref nullobj,
                                                           ref nullobj, ref nullobj, ref nullobj,
                                                           ref nullobj, ref objReplace, ref nullobj,
                                                           ref nullobj, ref nullobj, ref nullobj);


                                break; // 结束循环
                                                       
                            }
                        }


                                                // 由于Word中换行为“^p”两个字符不能分割
                                                // 如果分割位置将换行符分开了,则本次少替换一个字符
                                                if (newStr.Substring(hasSubLength, itemLength).EndsWith("^") &&
                                                    newStr.Substring(hasSubLength, itemLength + 1).EndsWith("p"))
                        {
                            subLength = itemLength - 1;
                        }
                        else
                        {
                            subLength = itemLength;
                        }


                        itemStr = newStr.Substring(hasSubLength, subLength) + oldStr;


                        app.Selection.Find.ClearFormatting();
                        app.Selection.Find.Replacement.ClearFormatting();


                        app.Selection.Find.Text             = oldStr;
                        app.Selection.Find.Replacement.Text = itemStr;


                        app.Selection.Find.Execute(ref nullobj, ref nullobj, ref nullobj,
                                                   ref nullobj, ref nullobj, ref nullobj,
                                                   ref nullobj, ref nullobj, ref nullobj,
                                                   ref nullobj, ref objReplace, ref nullobj,
                                                   ref nullobj, ref nullobj, ref nullobj);


                        hasSubLength += subLength;
                    }
                }

                //    object tmp = "tttttt";
                //object missingValue = System.Reflection.Missing.Value;

                //Microsoft.Office.Interop.Word.Range startRange = app.ActiveDocument.Bookmarks.get_Item(ref tmp).Range;


                ////删除指定书签位置后的第一个表格
                //Microsoft.Office.Interop.Word.Table tbl = startRange.Tables[1];
                //    tbl.Delete();

                ////添加表格
                //doc.Tables.Add(startRange, 5, 4, ref missingValue, ref missingValue);

                //    //为表格划线
                //    startRange.Tables[1].Borders[MSWord.WdBorderType.wdBorderTop].LineStyle = MSWord.WdLineStyle.wdLineStyleSingle;
                //    startRange.Tables[1].Borders[MSWord.WdBorderType.wdBorderLeft].LineStyle = MSWord.WdLineStyle.wdLineStyleSingle;
                //    startRange.Tables[1].Borders[MSWord.WdBorderType.wdBorderRight].LineStyle = MSWord.WdLineStyle.wdLineStyleSingle;
                //    startRange.Tables[1].Borders[MSWord.WdBorderType.wdBorderBottom].LineStyle = MSWord.WdLineStyle.wdLineStyleSingle;
                //    startRange.Tables[1].Borders[MSWord.WdBorderType.wdBorderHorizontal].LineStyle = MSWord.WdLineStyle.wdLineStyleSingle;
                //    startRange.Tables[1].Borders[MSWord.WdBorderType.wdBorderVertical].LineStyle = MSWord.WdLineStyle.wdLineStyleSingle;


                //保存
                doc.Save();


                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close(ref nullobj, ref nullobj, ref nullobj);
                    doc = null;
                }
                if (app != null)
                {
                    app.Quit(ref nullobj, ref nullobj, ref nullobj);
                    app = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return(result);
        }
Example #12
0
        /// <summary>
        /// Generate word document with register of system documents
        /// </summary>
        /// <param name="tv"></param>
        public string RegisterOfSytemDocuments(TreeView tv, string clientFolder)
        {
            object oMissing = System.Reflection.Missing.Value;
            var    pastPlannedActivities = string.Empty;

            //Start Word and create a new document.
            WordNet._Application oWord = new Application {
                Visible = false
            };
            WordNet._Document oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                                                         ref oMissing, ref oMissing);

            oDoc.PageSetup.Orientation = WordNet.WdOrientation.wdOrientLandscape;

            PrintToWord(oDoc, "Register of System Documents", 16, 0, FCMWordAlign.CENTER);
            PrintToWord(oDoc, " ", 8, 0);

            // Locate client folder
            //
            string clientFileLocationName = Utils.getFilePathName(@"%TEMPLATEFOLDER%\ClientSource\", "ROS-001 Register Of System Documents.doc");

            FullFileNamePath = clientFileLocationName;
            FileName         = "RegisterOfSystemDocuments.doc";

            if (File.Exists(clientFileLocationName))
            {
                // Delete file
                try
                {
                    File.Delete(clientFileLocationName);
                    uioutput.AddOutputMessage("File replaced: " + clientFileLocationName);
                }
                catch (Exception)
                {
                    uioutput.AddOutputMessage("Error deleting file " + clientFileLocationName);
                    uioutput.AddErrorMessage("Error deleting file " + clientFileLocationName);
                    return(clientFileLocationName);
                }
            }


            // string filename = Path.Combine(Path.GetTempPath(), Path.ChangeExtension(Path.GetTempFileName(), "doc"));
            oDoc.SaveAs(clientFileLocationName);

            string msg = ">>> Generating file... ";

            if (uioutput != null)
            {
                uioutput.AddOutputMessage(msg);
            }

            PrintToWord(oDoc, " ", 8, 1);

            WordNet.Range wrdRng;
            WordNet.Table oTable;

            wrdRng = oDoc.Bookmarks.get_Item(oEndOfDoc).Range;
            int rowCount = 30;

            // Get number of rows for a client document, client document set
            //
            var cds = new BUSClientDocumentSet(Utils.ClientID, Utils.ClientSetID);

            rowCount = cds.DocumentCount;

            if (rowCount < 1)
            {
                return(clientFileLocationName);
            }

            oTable = oDoc.Tables.Add(wrdRng, rowCount, 6, ref vkFalse, ref vkFalse);

            //oTable.Borders.InsideLineWidth = WordNet.WdLineWidth.wdLineWidth050pt;
            //oTable.Borders.InsideColor = WordNet.WdColor.wdColorAutomatic;
            //oTable.Borders.OutsideLineWidth = WordNet.WdLineWidth.wdLineWidth050pt;
            //oTable.Borders.OutsideColor = WordNet.WdColor.wdColorAutomatic;

            oTable.Rows[1].HeadingFormat = -1;

            WordNet.Row headingRow = oTable.Rows[1];

            ApplyHeadingStyle(headingRow.Cells[1], 40);
            headingRow.Cells[1].Range.Text = "";

            ApplyHeadingStyle(headingRow.Cells[2], 30);
            headingRow.Cells[2].Range.Text = "";

            ApplyHeadingStyle(headingRow.Cells[3], 60);
            headingRow.Cells[3].Range.Text = "Document Number";

            ApplyHeadingStyle(headingRow.Cells[4], 50);
            headingRow.Cells[4].Range.Text = "Version";

            ApplyHeadingStyle(headingRow.Cells[5], 300);
            headingRow.Cells[5].Range.Text = "Document Name";

            ApplyHeadingStyle(headingRow.Cells[6], 100);
            headingRow.Cells[6].Range.Text = "Comments";

            int line = 0;

            foreach (var treeNode in tv.Nodes)
            {
                line++;
                WriteLineToRoSD(tv.Nodes[0], oDoc, oTable, prefix: "", parent: "", seqnum: line);
            }


            msg = ">>> End ";
            if (uioutput != null)
            {
                uioutput.AddOutputMessage(msg);
            }

            PrintToWord(oDoc, " ", 12, 1);

            oDoc.Save();
            oDoc.Close();

            oWord.Visible = true;
            oWord.Documents.Open(FileName: clientFileLocationName);
            oWord.Activate();

            return(clientFileLocationName);
        }
Example #13
0
        private void btnHeZhong_Click(object sender, EventArgs e)
        {
            //取得要架设的舟桥的长度,判断是否输入正确
            string meter = this.textBox1.Text.Trim().ToString();
            double temp;
            bool   isNum = true;

            try
            {
                temp = Convert.ToDouble(meter);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("输入错误,请重新输入");
                isNum = false;
                return;
            }
            if (isNum)
            {
                temp = Convert.ToDouble(meter);
                if (temp < 101.26 || temp > 1000)
                {
                    MessageBox.Show("输入错误:河中部分最小宽度为101.26米,最大为1000米");
                    this.textBox1.Text = null;
                    return;
                }
            }



            //根据输入的米数,计算需要架设多少米的桥
            //这里有两个数字,门桥数量和边孔数量
            //门桥长度=16.2,边孔长度=34.28,门桥数量需要计算,边孔数量则固定为2个
            //粗略计算出来的门桥长度 = (要架设的桥长度-边孔长度/2)/门桥长度,这里只需要取整数部分即可
            double lengthOfmenqiao  = 16.2;  //门桥长度
            double lengthOfbiankong = 34.28; //边孔长度
            int    numOfbiankong    = 2;     //边孔数量固定,不需要变
            double doubleOfmenqiao  = (Convert.ToDouble(meter) - 2 * lengthOfbiankong) / lengthOfmenqiao;
            //int numOfmenqiao = Convert.ToInt16(doubleOfmenqiao);//这里只需要取整,得到门桥数量
            int    numOfmenqiao = Convert.ToInt16(Math.Truncate(doubleOfmenqiao));
            double numofbeiyong = 1.2;
            int    K            = 1;

            Word._Application appCreate;// = new Word.ApplicationClass();//= new Word.Application();
            Word._Document    doc = null;
            appCreate = new Word.Application();
            object missing   = System.Reflection.Missing.Value;
            object readOnly  = false;
            object isVisable = false;
            object index     = 1;

            File.Copy(Application.StartupPath + "\\PrimData\\铁路舟桥河中部分器材数量表模板.doc",
                      Application.StartupPath + "\\PrimData\\temp.doc", true);
            object FileName = Application.StartupPath + "\\PrimData\\temp.doc";
            string finalFileName;

            try
            {
                this.textBox1.Enabled   = false;
                this.btnHeZhong.Enabled = false;

                doc = appCreate.Documents.Open(ref FileName, ref missing, ref readOnly,
                                               ref missing, ref missing, ref missing, ref missing, ref missing,
                                               ref missing, ref missing, ref missing, ref isVisable, ref missing,
                                               ref missing, ref missing, ref missing);


                //在文档的首行写入门桥数量,边孔数量,以及架设桥的长度
                string strTextOfFirstLine   = "舟桥总长度为: " + (numOfmenqiao * lengthOfmenqiao + numOfbiankong * lengthOfbiankong).ToString() + "米" + "\r\n";
                string strTextOfSecondtLine = "门桥数量为: " + numOfmenqiao.ToString() + "     " + "边孔数量:" + numOfbiankong.ToString() + "\r\n";
                doc.Paragraphs[1].Range.Select();
                doc.Paragraphs[1].Range.Text = strTextOfFirstLine;
                doc.Paragraphs[2].Range.Select();
                doc.Paragraphs[2].Range.Text = strTextOfSecondtLine;
                //string teststr = doc.Paragraphs[1].Range.Text.ToString();
                //object bookMarksIndex = 1;
                //doc.Bookmarks.get_Item(ref bookMarksIndex).Range.Text= strTextOfFirstLine;
                //bookMarksIndex = 2;
                //doc.Bookmarks.get_Item(ref bookMarksIndex).Range.Text = strTextOfSecondtLine;
                //doc.Paragraphs[1].Range.Text = strTextOfFirstLine.ToString();

                // MessageBox.Show(doc.Tables.Count.ToString()); 统计word内的表格数o

                //获取第一个表格,并修改其相应内容
                Word.Table tempTable = doc.Tables[1];

                //填充第一个表的表头,舟桥长度, 2行6列
                tempTable.Cell(2, 6).Range.Text = meter.ToString();
                #region     //填充第一张表的其他部分,第一张表只需要填充下表头即可,数据不用修改,注释掉先

                //填充第一个表的3-14行
                //for (int i = 3; i <= 14; i++)
                //{

                //    string tableContent = tempTable.Cell(i, 6).Range.Text.Trim().ToString();
                //    tableContent = tableContent.Remove(tableContent.Length - 2, 2);

                //    int num = Convert.ToInt16(tableContent) * K;
                //    tempTable.Cell(i, 6).Range.Text = num.ToString();
                //}
                #endregion

                int numBeforeBracket, numInBracket;

                #region                       //获取第二个表格,只需要填充第9列,把7,8列的对应相乘即可
                tempTable = doc.Tables[2];
                //填充第二个表的舟桥长度, 1行7列
                tempTable.Cell(1, 7).Range.Text = "架设" + meter.ToString() + "米舟桥数量";


                //单独处理10行7列和26行7列的小数,再相加
                for (int i = 3; i <= 34; i++)
                {
                    if (i == 10 || i == 26)      //过滤掉第10列和第26列
                    {
                        i = i + 1;
                        string rowTen = tempTable.Cell(10, 7).Range.Text.ToString();
                        rowTen = rowTen.Remove(rowTen.Length - 2, 2);
                        tempTable.Cell(10, 9).Range.Text = (Convert.ToDouble(rowTen) * numOfmenqiao).ToString();

                        rowTen = tempTable.Cell(26, 7).Range.Text.ToString();
                        rowTen = rowTen.Remove(rowTen.Length - 2, 2);
                        tempTable.Cell(26, 9).Range.Text = (Convert.ToDouble(rowTen) * numOfmenqiao + Convert.ToInt16(tempTable.Cell(26, 8).Range.Text.ToString().Remove(tempTable.Cell(26, 8).Range.Text.ToString().Length - 2, 2))).ToString();
                    }

                    string tableContent = tempTable.Cell(i, 9).Range.Text.Trim().ToString();
                    if (tableContent.Length < 2)
                    {
                        tempTable.Cell(i, 9).Range.Text = null;
                    }
                    else if (tableContent.Length >= 8)
                    {
                        string rowOfSvStr = null;
                        if (tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfSvStr = tempTable.Cell(i, 7).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfSvStr = null;
                        }

                        string rowOfEIStr = null;
                        if (tempTable.Cell(i, 8).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfEIStr = tempTable.Cell(i, 8).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 8).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfEIStr = null;
                        }
                        int rowIofSV = Convert.ToInt16(rowOfSvStr) * numOfmenqiao;     //处理第7列的门桥数量
                        int rowIofEI = Convert.ToInt16(rowOfEIStr) * numOfbiankong;    //处理第8列的边孔数量
                        tempTable.Cell(i, 9).Range.Text = (rowIofSV + rowIofEI).ToString() + "\n" + "(" + (Convert.ToInt16((rowIofSV + rowIofEI) * numofbeiyong)).ToString() + ")";
                    }
                    else
                    {
                        string rowOfSvStr = null;
                        if (tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfSvStr = tempTable.Cell(i, 7).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfSvStr = null;
                        }

                        string rowOfEIStr = null;
                        if (tempTable.Cell(i, 8).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfEIStr = tempTable.Cell(i, 8).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 8).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfEIStr = null;
                        }
                        int rowIofSV = Convert.ToInt16(rowOfSvStr) * numOfmenqiao;     //处理第7列的门桥数量
                        int rowIofEI = Convert.ToInt16(rowOfEIStr) * numOfbiankong;    //处理第8列的边孔数量
                        // tableContent = tableContent.Remove(tableContent.Length - 2, 2);
                        // int num = Convert.ToInt16(tableContent) * K;
                        tempTable.Cell(i, 9).Range.Text = (rowIofSV + rowIofEI).ToString();
                    }
                }

                //单独处理10行7列和26行7列的小数

                #endregion       //第二个表格处理完毕


                #region      //处理第三个表格
                tempTable = doc.Tables[3];
                //表头
                tempTable.Cell(1, 7).Range.Text = "架设" + meter.ToString() + "米舟桥数量";
                //处理表格数据
                for (int i = 3; i <= 11; i++)
                {
                    if (i == 10 || i == 26)      //过滤掉第10列和第26列
                    {
                        i = i + 1;
                    }

                    string tableContent = tempTable.Cell(i, 9).Range.Text.Trim().ToString();
                    if (tableContent.Length < 2)
                    {
                        tempTable.Cell(i, 9).Range.Text = null;
                    }
                    else if (tableContent.Length >= 8)
                    {
                        string rowOfSvStr = null;
                        if (tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfSvStr = tempTable.Cell(i, 7).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfSvStr = null;
                        }

                        string rowOfEIStr = null;
                        if (tempTable.Cell(i, 8).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfEIStr = tempTable.Cell(i, 8).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 8).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfEIStr = null;
                        }
                        int rowIofSV = Convert.ToInt16(rowOfSvStr) * numOfmenqiao;     //处理第7列的门桥数量
                        int rowIofEI = Convert.ToInt16(rowOfEIStr) * numOfbiankong;    //处理第8列的边孔数量
                        tempTable.Cell(i, 9).Range.Text = (rowIofSV + rowIofEI).ToString() + "\n" + "(" + (Convert.ToInt16((rowIofSV + rowIofEI) * numofbeiyong)).ToString() + ")";
                    }
                    else
                    {
                        string rowOfSvStr = null;
                        if (tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfSvStr = tempTable.Cell(i, 7).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfSvStr = null;
                        }

                        string rowOfEIStr = null;
                        if (tempTable.Cell(i, 8).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfEIStr = tempTable.Cell(i, 8).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 8).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfEIStr = null;
                        }
                        int rowIofSV = Convert.ToInt16(rowOfSvStr) * numOfmenqiao;     //处理第7列的门桥数量
                        int rowIofEI = Convert.ToInt16(rowOfEIStr) * numOfbiankong;    //处理第8列的边孔数量
                        // tableContent = tableContent.Remove(tableContent.Length - 2, 2);
                        // int num = Convert.ToInt16(tableContent) * K;
                        tempTable.Cell(i, 9).Range.Text = (rowIofSV + rowIofEI).ToString();
                    }
                }
                #endregion     //第三个表格无特殊情况,处理完毕


                #region      //处理第四个表格
                tempTable = doc.Tables[4];
                //表头
                tempTable.Cell(1, 7).Range.Text = "架设" + meter.ToString() + "米舟桥数量";
                //处理表格数据,4-26行 ,16行单独处理
                for (int i = 4; i <= 26; i++)
                {
                    if (i == 16)
                    {
                        i = i + 1;
                        //16行有括号,在这里单独处理第16行
                        string tempstrSixteen = tempTable.Cell(16, 7).Range.Text.ToString();                                                              //.Remove(tempTable.Cell(16,7).Range.Text.ToString().Length-2,2);//16行7列
                        numBeforeBracket = getNumBeforeBracketNoR(tempstrSixteen);                                                                        //每门桥
                        tempstrSixteen   = tempTable.Cell(16, 8).Range.Text.ToString().Remove(tempTable.Cell(16, 8).Range.Text.ToString().Length - 2, 2); //16行7列
                        int tempSixteenEightC = Convert.ToInt16(tempstrSixteen);                                                                          //16行8列,每边孔
                        tempTable.Cell(16, 9).Range.Text = (numBeforeBracket * numOfmenqiao + tempSixteenEightC * numOfbiankong).ToString();
                    }
                    //有些行不是9列,跳过
                    if (i == 19 || i == 23)
                    {
                        i = i + 1;
                    }

                    string tableContent = tempTable.Cell(i, 9).Range.Text.Trim().ToString();
                    if (tableContent.Length < 2)
                    {
                        tempTable.Cell(i, 9).Range.Text = null;
                    }
                    else if (tableContent.Length >= 8)
                    {
                        string rowOfSvStr = null;
                        if (tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfSvStr = tempTable.Cell(i, 7).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfSvStr = null;
                        }

                        string rowOfEIStr = null;
                        if (tempTable.Cell(i, 8).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfEIStr = tempTable.Cell(i, 8).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 8).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfEIStr = null;
                        }
                        int rowIofSV = Convert.ToInt16(rowOfSvStr) * numOfmenqiao;     //处理第7列的门桥数量
                        int rowIofEI = Convert.ToInt16(rowOfEIStr) * numOfbiankong;    //处理第8列的边孔数量
                        tempTable.Cell(i, 9).Range.Text = (rowIofSV + rowIofEI).ToString() + "\n" + "(" + (Convert.ToInt16((rowIofSV + rowIofEI) * numofbeiyong)).ToString() + ")";
                    }
                    else
                    {
                        string rowOfSvStr = null;
                        if (tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfSvStr = tempTable.Cell(i, 7).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfSvStr = null;
                        }

                        string rowOfEIStr = null;
                        if (tempTable.Cell(i, 8).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfEIStr = tempTable.Cell(i, 8).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 8).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfEIStr = null;
                        }
                        int rowIofSV = Convert.ToInt16(rowOfSvStr) * numOfmenqiao;     //处理第7列的门桥数量
                        int rowIofEI = Convert.ToInt16(rowOfEIStr) * numOfbiankong;    //处理第8列的边孔数量
                        // tableContent = tableContent.Remove(tableContent.Length - 2, 2);
                        // int num = Convert.ToInt16(tableContent) * K;
                        tempTable.Cell(i, 9).Range.Text = (rowIofSV + rowIofEI).ToString();
                    }
                }


                #endregion      //第四个表格处理完毕


                #region     //处理第五个表格,无特殊情况,直接处理

                tempTable = doc.Tables[5];
                //表头
                tempTable.Cell(1, 7).Range.Text = "架设" + meter.ToString() + "米舟桥数量";
                //处理表格数据,4-25行
                for (int i = 4; i <= 25; i++)
                {
                    //第10行不需要填充,,跳过
                    if (i == 10)
                    {
                        i = i + 1;
                    }
                    string tableContent = tempTable.Cell(i, 9).Range.Text.Trim().ToString();
                    if (tableContent.Length < 2)
                    {
                        tempTable.Cell(i, 9).Range.Text = null;
                    }
                    else if (tableContent.Length >= 8)
                    {
                        string rowOfSvStr = null;
                        if (tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfSvStr = tempTable.Cell(i, 7).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfSvStr = null;
                        }

                        string rowOfEIStr = null;
                        if (tempTable.Cell(i, 8).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfEIStr = tempTable.Cell(i, 8).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 8).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfEIStr = null;
                        }
                        int rowIofSV = Convert.ToInt16(rowOfSvStr) * numOfmenqiao;     //处理第7列的门桥数量
                        int rowIofEI = Convert.ToInt16(rowOfEIStr) * numOfbiankong;    //处理第8列的边孔数量
                        tempTable.Cell(i, 9).Range.Text = (rowIofSV + rowIofEI).ToString() + "\n" + "(" + (Convert.ToInt16((rowIofSV + rowIofEI) * numofbeiyong)).ToString() + ")";
                    }
                    else
                    {
                        string rowOfSvStr = null;
                        if (tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfSvStr = tempTable.Cell(i, 7).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfSvStr = null;
                        }

                        string rowOfEIStr = null;
                        if (tempTable.Cell(i, 8).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfEIStr = tempTable.Cell(i, 8).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 8).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfEIStr = null;
                        }
                        int rowIofSV = Convert.ToInt16(rowOfSvStr) * numOfmenqiao;     //处理第7列的门桥数量
                        int rowIofEI = Convert.ToInt16(rowOfEIStr) * numOfbiankong;    //处理第8列的边孔数量
                        // tableContent = tableContent.Remove(tableContent.Length - 2, 2);
                        // int num = Convert.ToInt16(tableContent) * K;
                        tempTable.Cell(i, 9).Range.Text = (rowIofSV + rowIofEI).ToString();
                    }
                }

                #endregion                 //第五个表格处理完毕


                #region                 //处理第六个表格
                tempTable = doc.Tables[6];
                //处理表格数据,3-12行,第9行特殊,单独处理
                for (int i = 3; i <= 12; i++)
                {
                    if (i == 9)
                    {
                        i = i + 1;
                        //单独处理第六个表格的第9行
                        string sixTableNineRowS    = tempTable.Cell(9, 7).Range.Text.ToString();                         //获得第七列的字符串
                        string sixTableNineRowENum = (getNumBeforeBracket(sixTableNineRowS) * numOfbiankong).ToString(); //计算出第八列的结果
                        tempTable.Cell(9, 8).Range.Text = sixTableNineRowENum + "\r" + sixTableNineRowENum + "\r" + sixTableNineRowENum + "\r";
                    }

                    string tableContent = tempTable.Cell(i, 8).Range.Text.Trim().ToString();
                    if (tableContent.Length < 2)
                    {
                        tempTable.Cell(i, 8).Range.Text = null;
                    }
                    else if (tableContent.Length >= 8)
                    {
                        string rowOfSvStr = tempTable.Cell(i, 7).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length - 2, 2);
                        int    rowIofSV   = Convert.ToInt16(rowOfSvStr) * numOfmenqiao; //处理第7列的门桥数量
                        tempTable.Cell(i, 8).Range.Text = (rowIofSV * numOfbiankong).ToString() + "\n" + "(" + (Convert.ToInt16((rowIofSV * numOfbiankong) * numofbeiyong)).ToString() + ")";
                    }
                    else
                    {
                        string rowOfSvStr = tempTable.Cell(i, 7).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length - 2, 2);
                        int    rowIofSV   = Convert.ToInt16(rowOfSvStr) * numOfmenqiao; //处理第7列的门桥数量
                        // tableContent = tableContent.Remove(tableContent.Length - 2, 2);
                        // int num = Convert.ToInt16(tableContent) * K;
                        tempTable.Cell(i, 8).Range.Text = (rowIofSV * numOfbiankong).ToString();
                    }
                }

                #endregion      //第六个表格处理完毕


                #region      //处理第7个表格
                //获取第七个表格
                tempTable = doc.Tables[7];
                //处理表头
                tempTable.Cell(1, 6).Range.Text = "架设" + meter.ToString() + "米舟桥数量";
                //3-32行
                //单独处理18,19,21,23,24
                for (int i = 3; i <= 32; i++)
                {
                    //先处理18,19,21,23,24行
                    if (i == 18 || i == 19 || i == 21 || i == 23 || i == 24)
                    {
                        string rowstrSix   = tempTable.Cell(i, 6).Range.Text.ToString();
                        double rowNumSix   = Convert.ToDouble(rowstrSix.Remove(rowstrSix.Length - 2, 2));
                        string rowstrSeven = tempTable.Cell(i, 7).Range.Text.ToString();
                        int    rowNumSeven = Convert.ToInt16(rowstrSeven.Remove(rowstrSeven.Length - 2, 2));
                        tempTable.Cell(i, 8).Range.Text = (rowNumSix * numOfmenqiao + rowNumSeven * numOfbiankong).ToString();
                    }

                    if (i == 18 || i == 21 || i == 23)
                    {
                        i = i + 1;                                    //18行
                    }
                    if (i == 19 || i == 24)
                    {
                        i = i + 1;
                    }
                    string tableContent = tempTable.Cell(i, 8).Range.Text.Trim().ToString();
                    if (tableContent.Length < 2)
                    {
                        tempTable.Cell(i, 8).Range.Text = null;
                    }
                    else if (tableContent.Length >= 8)
                    {
                        string rowOfSixStr = null;
                        if (tempTable.Cell(i, 6).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfSixStr = tempTable.Cell(i, 6).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 6).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfSixStr = null;
                        }

                        string rowOfSvStr = null;
                        if (tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfSvStr = tempTable.Cell(i, 7).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfSvStr = null;
                        }
                        int rowIofSix = Convert.ToInt16(rowOfSixStr) * numOfmenqiao;    //处理第6列的门桥数量
                        int rowIofSv  = Convert.ToInt16(rowOfSvStr) * numOfbiankong;    //处理第7列的边孔数量
                        tempTable.Cell(i, 8).Range.Text = (rowIofSix + rowIofSv).ToString() + "\n" + "(" + (Convert.ToInt16((rowIofSix + rowIofSv) * numofbeiyong)).ToString() + ")";
                    }
                    else
                    {
                        string rowOfSixStr = null;
                        if (tempTable.Cell(i, 6).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfSixStr = tempTable.Cell(i, 6).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 6).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfSixStr = null;
                        }

                        string rowOfSvStr = null;
                        if (tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length > 2)
                        {
                            rowOfSvStr = tempTable.Cell(i, 7).Range.Text.Trim().ToString().Remove(tempTable.Cell(i, 7).Range.Text.Trim().ToString().Length - 2, 2);
                        }
                        else
                        {
                            rowOfSvStr = null;
                        }
                        int rowIofSix = Convert.ToInt16(rowOfSixStr) * numOfmenqiao;    //处理第6列的门桥数量
                        int rowIofSv  = Convert.ToInt16(rowOfSvStr) * numOfbiankong;    //处理第7列的边孔数量
                        // tableContent = tableContent.Remove(tableContent.Length - 2, 2);
                        // int num = Convert.ToInt16(tableContent) * K;
                        tempTable.Cell(i, 8).Range.Text = (rowIofSix + rowIofSv).ToString();
                    }
                }
                #endregion     //第七张表处理结束


                doc.Save();
                this.textBox1.Enabled   = true;
                this.btnHeZhong.Enabled = true;

                finalFileName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + meter.ToString() + "米铁路舟桥河中部分器材数量表.doc";

                File.Copy(Application.StartupPath + "\\PrimData\\temp.doc",
                          finalFileName,
                          true);

                MessageBox.Show(meter.ToString() + "米铁路舟桥河中部分器材数量表生成成功,已保存至桌面");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }


            finally
            {
                if (doc != null)
                {
                    doc.Close(ref missing, ref missing, ref missing);
                    doc = null;
                }

                if (appCreate != null)
                {
                    appCreate.Quit(ref missing, ref missing, ref missing);
                    appCreate = null;
                }
            }
        }
Example #14
0
 public void Save()
 {
     oDoc.Save();
 }
Example #15
0
        /// <summary>
        /// 表格处理
        /// </summary>
        /// <param name="filePath">word文件名</param>
        /// <returns></returns>
        public static bool AddTable(string filePath)
        {
            try
            {
                Object oMissing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word._Application WordApp = new Application();
                WordApp.Visible = true;
                object filename = filePath;
                Microsoft.Office.Interop.Word._Document 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);

                //插入表格
                Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref oMissing, ref oMissing);
                //设置表格
                newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
                newTable.Borders.InsideLineStyle  = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                newTable.Columns[1].Width         = 100f;
                newTable.Columns[2].Width         = 220f;
                newTable.Columns[3].Width         = 105f;

                //填充表格内容
                newTable.Cell(1, 1).Range.Text = "我的简历";
                //设置单元格中字体为粗体
                newTable.Cell(1, 1).Range.Bold = 2;

                //合并单元格
                newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));

                //垂直居中
                WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                //水平居中
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;

                //填充表格内容
                newTable.Cell(2, 1).Range.Text = "座右铭:...";
                //设置单元格内字体颜色
                newTable.Cell(2, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;
                //合并单元格
                newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
                WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

                //填充表格内容
                newTable.Cell(3, 1).Range.Text = "姓名:";
                newTable.Cell(3, 2).Range.Text = "雷鑫";
                //纵向合并单元格
                newTable.Cell(3, 3).Select();
                //选中一行
                object moveUnit   = Microsoft.Office.Interop.Word.WdUnits.wdLine;
                object moveCount  = 3;
                object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
                WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
                WordApp.Selection.Cells.Merge();

                //表格中插入图片
                string pictureFileName  = System.IO.Directory.GetCurrentDirectory() + @"\picture.jpg";
                object LinkToFile       = false;
                object SaveWithDocument = true;
                object Anchor           = WordDoc.Application.Selection.Range;
                WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(pictureFileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                //图片宽度
                WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;
                //图片高度
                WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;
                //将图片设置为四周环绕型
                Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
                s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;

                newTable.Cell(12, 1).Range.Text = "备注:";
                newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
                //在表格中增加行
                WordDoc.Content.Tables[1].Rows.Add(ref oMissing);

                //保存
                WordDoc.Save();
                WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                return(false);
            }
        }