示例#1
0
        public byte[] CreateWordDocument2()
        {
            //创建一个Document实例并添加section
            Spire.Doc.Document doc     = new Spire.Doc.Document();
            Spire.Doc.Section  section = doc.AddSection();

            //添加指向网址的超链接
            Spire.Doc.Documents.Paragraph para1 = section.AddParagraph();
            para1.AppendHyperlink("www.baidu.com", "www.baidu.com",
                                  Spire.Doc.Documents.HyperlinkType.WebLink);

            //添加指向邮件地址的超链接
            Spire.Doc.Documents.Paragraph para2 = section.AddParagraph();
            para2.AppendHyperlink("mailto:[email protected]", "mailto:[email protected]",
                                  Spire.Doc.Documents.HyperlinkType.EMailLink);


            //设置段落之间的间距
            para1.Format.AfterSpacing = 15f;
            para2.Format.AfterSpacing = 15f;

            MemoryStream memoryStream = new MemoryStream();

            StreamWriter wr = new StreamWriter(memoryStream);


            //保存文档
            doc.SaveToStream(memoryStream, Spire.Doc.FileFormat.Docx2013);

            return(memoryStream.ToArray());
        }
示例#2
0
        private void button1_Click(object sender, EventArgs e)
        {
            string filename = Application.StartupPath + "\\bin\\" + Global.templateName + ".doc";

            Spire.Doc.Document document = new Spire.Doc.Document(filename, FileFormat.Docx);

            Spire.Doc.Fields.TextBox      textBox   = document.TextBoxes[12];
            Spire.Doc.Documents.Paragraph paragraph = textBox.Body.AddParagraph();
            Spire.Doc.Fields.TextRange    textRange = paragraph.AppendText(textBox1.Text);
            document.SaveToFile(filename, FileFormat.Docx);

            object filename1     = Environment.CurrentDirectory.ToString() + "\\bin\\" + Global.templateName + ".doc";
            string ImagePath     = pictureBox1.ImageLocation;
            string strKey        = "7.6.2 开门测试期间温度变化曲线图";
            object MissingValue  = Type.Missing;
            bool   isFindSealLoc = false;

            Microsoft.Office.Interop.Word.Application wp = null;
            Microsoft.Office.Interop.Word.Document    wd = null;
            try
            {
                wp = new Microsoft.Office.Interop.Word.Application();
                wd = wp.Documents.Open(ref filename1, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue);
                wp.Selection.Find.ClearFormatting();
                wp.Selection.Find.Replacement.ClearFormatting();
                wp.Selection.Find.Text = strKey;
                object objReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceNone;
                if (wp.Selection.Find.Execute(ref MissingValue, ref MissingValue, ref MissingValue,
                                              ref MissingValue, ref MissingValue, ref MissingValue,
                                              ref MissingValue, ref MissingValue, ref MissingValue,
                                              ref MissingValue, ref objReplace, ref MissingValue,
                                              ref MissingValue, ref MissingValue, ref MissingValue))
                {
                    object Anchor           = wp.Selection.Range;
                    object LinkToFile       = false;
                    object SaveWithDocument = true;
                    Microsoft.Office.Interop.Word.InlineShape Inlineshape = wp.Selection.InlineShapes.AddPicture(
                        ImagePath, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                    Inlineshape.Select();
                    Microsoft.Office.Interop.Word.Shape shape = Inlineshape.ConvertToShape();
                    shape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;

                    isFindSealLoc = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (wd != null)
                {
                    wd.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wd);
                    wd = null;
                }
                if (wp != null)
                {
                    wp.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wp);
                    wp = null;
                }
                MessageBox.Show("导入成功!");
            }
            this.Close();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            object filename1 = Application.StartupPath + "\\bin\\" + Global.templateName;

            object G_Missing = System.Reflection.Missing.Value;

            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document    wordDoc;
            wordDoc = wordApp.Documents.Open(filename1);
            wordDoc.ActiveWindow.Visible = false;//打开word

            Microsoft.Office.Interop.Word.Range myRange = wordDoc.Range();
            Microsoft.Office.Interop.Word.Find  f       = myRange.Find;
            f.Text = "布点数量:";
            f.ClearFormatting();

            bool finded = f.Execute(ref G_Missing, ref G_Missing, ref G_Missing,
                                    ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                    ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                    ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing
                                    );

            myRange      = wordDoc.Range(myRange.End, myRange.End + 10);
            myRange.Text = textBox1.Text;

            Microsoft.Office.Interop.Word.Range myRange1 = wordDoc.Range();
            Microsoft.Office.Interop.Word.Find  f1       = myRange1.Find;
            f1.Text = "仪表编号:";
            f1.ClearFormatting();

            bool finded1 = f1.Execute(ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing
                                      );

            myRange1      = wordDoc.Range(myRange1.End, myRange1.End + 13);
            myRange1.Text = textBox1.Text;


            Microsoft.Office.Interop.Word.Range myRange2 = wordDoc.Range();
            Microsoft.Office.Interop.Word.Find  f2       = myRange2.Find;
            f2.Text = "检测状态:空载(半载或满载)";
            f2.ClearFormatting();

            bool finded2 = f2.Execute(ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing
                                      );

            myRange2      = wordDoc.Range(myRange2.End - 9, myRange2.End);
            myRange2.Text = comboBox1.Text;


            wordDoc.Save();
            wordDoc.Close(ref G_Missing, ref G_Missing, ref G_Missing);
            wordApp.Quit(ref G_Missing, ref G_Missing, ref G_Missing);
            wordApp = null;

            string ImagePath     = pictureBox1.ImageLocation;
            string strKey        = "示意图:(上层,中层,下层)";
            object MissingValue  = Type.Missing;
            bool   isFindSealLoc = false;

            Microsoft.Office.Interop.Word.Application wp = null;
            Microsoft.Office.Interop.Word.Document    wd = null;
            try
            {
                wp = new Microsoft.Office.Interop.Word.Application();
                wd = wp.Documents.Open(ref filename1, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue);
                wp.Selection.Find.ClearFormatting();
                wp.Selection.Find.Replacement.ClearFormatting();
                wp.Selection.Find.Text = strKey;
                object objReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceNone;
                if (wp.Selection.Find.Execute(ref MissingValue, ref MissingValue, ref MissingValue,
                                              ref MissingValue, ref MissingValue, ref MissingValue,
                                              ref MissingValue, ref MissingValue, ref MissingValue,
                                              ref MissingValue, ref objReplace, ref MissingValue,
                                              ref MissingValue, ref MissingValue, ref MissingValue))
                {
                    object Anchor           = wp.Selection.Range;
                    object LinkToFile       = false;
                    object SaveWithDocument = true;
                    Microsoft.Office.Interop.Word.InlineShape Inlineshape = wp.Selection.InlineShapes.AddPicture(
                        ImagePath, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                    Inlineshape.Select();
                    Microsoft.Office.Interop.Word.Shape shape = Inlineshape.ConvertToShape();
                    shape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;

                    isFindSealLoc = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (wd != null)
                {
                    wd.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wd);
                    wd = null;
                }
                if (wp != null)
                {
                    wp.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wp);
                    wp = null;
                }
            }

            string filename = Environment.CurrentDirectory.ToString() + "\\bin\\" + Global.templateName + ".doc";

            Spire.Doc.Document document = new Spire.Doc.Document(filename, FileFormat.Docx);

            Spire.Doc.Fields.TextBox      textBox   = document.TextBoxes[0];
            Spire.Doc.Documents.Paragraph paragraph = textBox.Body.AddParagraph();
            Spire.Doc.Fields.TextRange    textRange = paragraph.AppendText(textBox3.Text);
            document.SaveToFile(filename, FileFormat.Docx);
            this.Close();
        }
        private void CreateDocument(object o)
        {
            ExcelInfo ei = o as ExcelInfo;

            dgv_task.Rows[ei._id - 1].Cells[4].Value = "正在准备";
            //获得 table
            DataTable dt_split = ei._data;

            //读取excel,得到行数
            /*生成一个新的word*/
            Spire.Doc.Document spdoc = new Spire.Doc.Document();
            /*循环把list中的mycu内容添加到word文档*/
            for (int rowindex = 0; rowindex < dt_split.Rows.Count; rowindex++)
            {
                dgv_task.Rows[ei._id - 1].Cells[4].Value = (100 * Convert.ToDecimal(rowindex + 1) / Convert.ToDecimal(dt_split.Rows.Count)).ToString("00.00") + "%";
                //更新状态栏文字
                //string str_statue = "正在生成第{0}个,共{1}个……";
                //UpdateStatue(string.Format(str_statue, rowindex - 1, lastrow - 1));
                ///载入一个空白文档
                spdoc.LoadFromFile(Environment.CurrentDirectory + @"\newdoc.docx");
                for (int i = Global.list_myuc.Count - 1; i >= 0; i--)
                {
                    //设置文本
                    string str_n = string.Empty;
                    //设置空行
                    for (int ji = 0; ji < Global.list_myuc[i].mysetter.konghang; ji++)
                    {
                        str_n += "\n";
                    }
                    //添加一个段落

                    Spire.Doc.Documents.Paragraph parainsert = spdoc.LastSection.AddParagraph();
                    //Spire.Doc.Documents.Paragraph parainsert = spdoc.CreateParagraph();
                    TextRange tx = parainsert.AppendText(GetContent(Global.list_myuc[i].mysetter.listcolumn, rowindex, dt_split) + str_n);
                    //字体名称
                    tx.CharacterFormat.FontName = Global.list_myuc[i].mysetter.fontname;
                    //字体大小
                    tx.CharacterFormat.FontSize = Global.list_myuc[i].mysetter.fontsize;
                    //设置行距
                    switch (Global.list_myuc[i].mysetter.hangjustyle)
                    {
                    case "单倍行距":
                        parainsert.Format.LineSpacingRule = Spire.Doc.LineSpacingRule.AtLeast;
                        break;

                    case "1.5倍行距":
                        parainsert.Format.LineSpacingRule = Spire.Doc.LineSpacingRule.Exactly;
                        break;

                    case "2倍行距":
                        parainsert.Format.LineSpacingRule = Spire.Doc.LineSpacingRule.Multiple;
                        break;

                    default:
                        parainsert.Format.LineSpacingRule = Spire.Doc.LineSpacingRule.Exactly;
                        parainsert.Format.LineSpacing     = Global.list_myuc[i].mysetter.hangjuvalue;
                        break;
                    }
                    //首行缩进
                    parainsert.Format.SetFirstLineIndent(Global.list_myuc[i].mysetter.suojin); //首行缩进
                                                                                               //粗体
                    tx.CharacterFormat.Bold = Global.list_myuc[i].mysetter.bold == 1 ? true : false;
                    switch (Global.list_myuc[i].mysetter.juzhong)
                    {
                    case "左对齐":
                        parainsert.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left;
                        break;

                    case "居中":
                        parainsert.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
                        break;

                    case "右对齐":
                        parainsert.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
                        break;
                    }
                }

                SectionCollection sections = spdoc.Sections;
                //创建一个HeaderFooter类实例,添加页脚
                for (int i = 0; i < sections.Count; i++)
                {
                    sections[i].PageSetup.Margins.Top    = 72f;
                    sections[i].PageSetup.Margins.Left   = 90f;
                    sections[i].PageSetup.Margins.Bottom = 72f;
                    sections[i].PageSetup.Margins.Right  = 90f;
                    Spire.Doc.HeaderFooter        footer     = sections[i].HeadersFooters.Footer;
                    Spire.Doc.Documents.Paragraph footerPara = footer.AddParagraph();

                    //添加字段类型为页码,添加当前页、分隔线以及总页数
                    footerPara.AppendField("页码", FieldType.FieldPage);
                    footerPara.AppendText(" / ");
                    footerPara.AppendField("总页数", FieldType.FieldNumPages);
                    footerPara.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
                }
                /*保存文档*/
                //组成文件名
                List <string> strfilename = new List <string>();
                for (int i = 0; i < Global.listfilename.Count; i++)
                {
                    string filename_element = Regex.Replace(dt_split.Rows[rowindex][Global.listfilename[i] + 1].ToString(), @"[\s/\:*?''<>|]", "");
                    strfilename.Add(filename_element);
                }
                spdoc.BuiltinDocumentProperties.Author = "潜挖智库";
                spdoc.SaveToFile(tbcunfang.Text + @"\" + string.Join("-", strfilename) + @".docx");
                spdoc.Close();
            }
        }
示例#5
0
        private void button1_Click(object sender, EventArgs e)
        {
            object filename1 = Application.StartupPath + "\\bin\\" + Global.templateName;

            object G_Missing = System.Reflection.Missing.Value;

            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document    wordDoc;
            wordDoc = wordApp.Documents.Open(filename1);
            wordDoc.ActiveWindow.Visible = false;//打开word

            Microsoft.Office.Interop.Word.Range myRange = wordDoc.Range();

            Microsoft.Office.Interop.Word.Find f = myRange.Find;
            f.Text = "布点数量:";
            f.ClearFormatting();

            bool finded = f.Execute(ref G_Missing, ref G_Missing, ref G_Missing,
                                    ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                    ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                    ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing
                                    );

            myRange      = wordDoc.Range(myRange.End, myRange.End + 6);
            myRange.Text = textBox1.Text;

            Microsoft.Office.Interop.Word.Range myRange1 = wordDoc.Range();
            Microsoft.Office.Interop.Word.Find  f1       = myRange1.Find;
            f1.Text = "仪表编号:";
            f1.ClearFormatting();

            bool finded1 = f1.Execute(ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing
                                      );

            myRange1      = wordDoc.Range(myRange1.End, myRange1.End + 9);
            myRange1.Text = textBox2.Text;

            Microsoft.Office.Interop.Word.Range myRange2 = wordDoc.Range();
            Microsoft.Office.Interop.Word.Find  f2       = myRange2.Find;
            f2.Text = "测量点数量:";
            f2.ClearFormatting();

            bool finded2 = f2.Execute(ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing
                                      );

            myRange2      = wordDoc.Range(myRange2.End, myRange2.End + 6);
            myRange2.Text = textBox6.Text;

            Microsoft.Office.Interop.Word.Range myRange3 = wordDoc.Range();
            Microsoft.Office.Interop.Word.Find  f3       = myRange3.Find;
            f3.Text = "仪表编号: ";
            f3.ClearFormatting();

            bool finded3 = f3.Execute(ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing
                                      );

            myRange3      = wordDoc.Range(myRange3.End, myRange3.End + 8);
            myRange3.Text = textBox5.Text;

            Microsoft.Office.Interop.Word.Range myRange4 = wordDoc.Range();
            Microsoft.Office.Interop.Word.Find  f4       = myRange4.Find;
            f4.Text = "测量点数量: ";
            f4.ClearFormatting();

            bool finded4 = f4.Execute(ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing
                                      );

            myRange4      = wordDoc.Range(myRange4.End, myRange4.End + 5);
            myRange4.Text = textBox9.Text;

            Microsoft.Office.Interop.Word.Range myRange5 = wordDoc.Range();
            Microsoft.Office.Interop.Word.Find  f5       = myRange5.Find;
            f5.Text = "仪表编号:  ";
            f5.ClearFormatting();

            bool finded5 = f5.Execute(ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing
                                      );

            myRange5      = wordDoc.Range(myRange5.End, myRange5.End + 7);
            myRange5.Text = textBox8.Text;

            Microsoft.Office.Interop.Word.Range myRange6 = wordDoc.Range();
            Microsoft.Office.Interop.Word.Find  f6       = myRange6.Find;
            f6.Text = "测量点数量:  ";
            f6.ClearFormatting();

            bool finded6 = f6.Execute(ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing
                                      );

            myRange6      = wordDoc.Range(myRange6.End, myRange6.End + 4);
            myRange6.Text = textBox12.Text;

            Microsoft.Office.Interop.Word.Range myRange7 = wordDoc.Range();
            Microsoft.Office.Interop.Word.Find  f7       = myRange7.Find;
            f7.Text = "仪表编号:   ";
            f7.ClearFormatting();

            bool finded7 = f7.Execute(ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing
                                      );

            myRange7      = wordDoc.Range(myRange7.End, myRange7.End + 6);
            myRange7.Text = textBox11.Text;

            Microsoft.Office.Interop.Word.Range myRange8 = wordDoc.Range();
            Microsoft.Office.Interop.Word.Find  f8       = myRange8.Find;
            f8.Text = "测量点数量:   ";
            f8.ClearFormatting();

            bool finded8 = f8.Execute(ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing
                                      );

            myRange8      = wordDoc.Range(myRange8.End, myRange8.End + 3);
            myRange8.Text = textBox15.Text;

            Microsoft.Office.Interop.Word.Range myRange9 = wordDoc.Range();
            Microsoft.Office.Interop.Word.Find  f9       = myRange9.Find;
            f9.Text = "仪表编号:    ";
            f9.ClearFormatting();

            bool finded9 = f9.Execute(ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                      ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing
                                      );

            myRange9      = wordDoc.Range(myRange9.End, myRange9.End + 5);
            myRange9.Text = textBox14.Text;

            Microsoft.Office.Interop.Word.Range myRange10 = wordDoc.Range();
            Microsoft.Office.Interop.Word.Find  f10       = myRange10.Find;
            f10.Text = "测量点数量:    ";
            f10.ClearFormatting();

            bool finded10 = f10.Execute(ref G_Missing, ref G_Missing, ref G_Missing,
                                        ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                        ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                        ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing
                                        );

            myRange10      = wordDoc.Range(myRange10.End, myRange10.End + 2);
            myRange10.Text = textBox18.Text;

            Microsoft.Office.Interop.Word.Range myRange11 = wordDoc.Range();
            Microsoft.Office.Interop.Word.Find  f11       = myRange11.Find;
            f11.Text = "仪表编号:     ";
            f11.ClearFormatting();

            bool finded11 = f11.Execute(ref G_Missing, ref G_Missing, ref G_Missing,
                                        ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                        ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                        ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing
                                        );

            myRange11      = wordDoc.Range(myRange11.End, myRange11.End + 4);
            myRange11.Text = textBox17.Text;

            Microsoft.Office.Interop.Word.Range myRange12 = wordDoc.Range();
            Microsoft.Office.Interop.Word.Find  f12       = myRange12.Find;
            f12.Text = "测量点数量:     ";
            f12.ClearFormatting();

            bool finded12 = f12.Execute(ref G_Missing, ref G_Missing, ref G_Missing,
                                        ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                        ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                        ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing
                                        );

            myRange12      = wordDoc.Range(myRange12.End, myRange12.End + 1);
            myRange12.Text = textBox21.Text;

            Microsoft.Office.Interop.Word.Range myRange13 = wordDoc.Range();
            Microsoft.Office.Interop.Word.Find  f13       = myRange13.Find;
            f13.Text = "仪表编号:      ";
            f13.ClearFormatting();

            bool finded13 = f13.Execute(ref G_Missing, ref G_Missing, ref G_Missing,
                                        ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                        ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing,
                                        ref G_Missing, ref G_Missing, ref G_Missing, ref G_Missing
                                        );

            myRange13      = wordDoc.Range(myRange13.End, myRange13.End + 3);
            myRange13.Text = textBox20.Text;


            wordDoc.Save();
            wordDoc.Close();
            wordApp.Quit();
            wordApp = null;



            string ImagePath     = pictureBox1.ImageLocation;
            string strKey        = "示意图:(上层,中层,下层)";
            object MissingValue  = Type.Missing;
            bool   isFindSealLoc = false;

            Microsoft.Office.Interop.Word.Application wp = null;
            Microsoft.Office.Interop.Word.Document    wd = null;
            try
            {
                wp = new Microsoft.Office.Interop.Word.Application();
                wd = wp.Documents.Open(ref filename1, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue);
                wp.Selection.Find.ClearFormatting();
                wp.Selection.Find.Replacement.ClearFormatting();
                wp.Selection.Find.Text = strKey;
                object objReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceNone;
                if (wp.Selection.Find.Execute(ref MissingValue, ref MissingValue, ref MissingValue,
                                              ref MissingValue, ref MissingValue, ref MissingValue,
                                              ref MissingValue, ref MissingValue, ref MissingValue,
                                              ref MissingValue, ref objReplace, ref MissingValue,
                                              ref MissingValue, ref MissingValue, ref MissingValue))
                {
                    object Anchor           = wp.Selection.Range;
                    object LinkToFile       = false;
                    object SaveWithDocument = true;
                    Microsoft.Office.Interop.Word.InlineShape Inlineshape = wp.Selection.InlineShapes.AddPicture(
                        ImagePath, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                    Inlineshape.Select();
                    Microsoft.Office.Interop.Word.Shape shape = Inlineshape.ConvertToShape();
                    shape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;

                    isFindSealLoc = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (wd != null)
                {
                    wd.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wd);
                    wd = null;
                }
                if (wp != null)
                {
                    wp.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wp);
                    wp = null;
                }
            }

            string ImagePath1     = pictureBox2.ImageLocation;
            string strKey1        = "6.1.2风机出风口布点";
            object MissingValue1  = Type.Missing;
            bool   isFindSealLoc1 = false;

            Microsoft.Office.Interop.Word.Application wp1 = null;
            Microsoft.Office.Interop.Word.Document    wd1 = null;
            try
            {
                wp1 = new Microsoft.Office.Interop.Word.Application();
                wd1 = wp1.Documents.Open(ref filename1, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue);
                wp1.Selection.Find.ClearFormatting();
                wp1.Selection.Find.Replacement.ClearFormatting();
                wp1.Selection.Find.Text = strKey1;
                object objReplace1 = Microsoft.Office.Interop.Word.WdReplace.wdReplaceNone;
                if (wp1.Selection.Find.Execute(ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref objReplace1, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue))
                {
                    object Anchor1           = wp1.Selection.Range;
                    object LinkToFile1       = false;
                    object SaveWithDocument1 = true;
                    Microsoft.Office.Interop.Word.InlineShape Inlineshape1 = wp1.Selection.InlineShapes.AddPicture(
                        ImagePath1, ref LinkToFile1, ref SaveWithDocument1, ref Anchor1);
                    Inlineshape1.Select();
                    Microsoft.Office.Interop.Word.Shape shape1 = Inlineshape1.ConvertToShape();
                    shape1.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;

                    isFindSealLoc1 = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (wd1 != null)
                {
                    wd1.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wd1);
                    wd1 = null;
                }
                if (wp1 != null)
                {
                    wp1.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wp1);
                    wp1 = null;
                }
            }

            string ImagePath2     = pictureBox3.ImageLocation;
            string strKey2        = "6.1.3出入口布点";
            object MissingValue2  = Type.Missing;
            bool   isFindSealLoc2 = false;

            Microsoft.Office.Interop.Word.Application wp2 = null;
            Microsoft.Office.Interop.Word.Document    wd2 = null;
            try
            {
                wp2 = new Microsoft.Office.Interop.Word.Application();
                wd2 = wp2.Documents.Open(ref filename1, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue);
                wp2.Selection.Find.ClearFormatting();
                wp2.Selection.Find.Replacement.ClearFormatting();
                wp2.Selection.Find.Text = strKey2;
                object objReplace2 = Microsoft.Office.Interop.Word.WdReplace.wdReplaceNone;
                if (wp2.Selection.Find.Execute(ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref objReplace2, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue))
                {
                    object Anchor2           = wp2.Selection.Range;
                    object LinkToFile2       = false;
                    object SaveWithDocument2 = true;
                    Microsoft.Office.Interop.Word.InlineShape Inlineshape2 = wp2.Selection.InlineShapes.AddPicture(
                        ImagePath2, ref LinkToFile2, ref SaveWithDocument2, ref Anchor2);
                    Inlineshape2.Select();
                    Microsoft.Office.Interop.Word.Shape shape2 = Inlineshape2.ConvertToShape();
                    shape2.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;

                    isFindSealLoc2 = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (wd2 != null)
                {
                    wd2.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wd2);
                    wd2 = null;
                }
                if (wp2 != null)
                {
                    wp2.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wp2);
                    wp2 = null;
                }
            }

            string ImagePath3     = pictureBox4.ImageLocation;
            string strKey3        = "6.1.4死角布点";
            object MissingValue3  = Type.Missing;
            bool   isFindSealLoc3 = false;

            Microsoft.Office.Interop.Word.Application wp3 = null;
            Microsoft.Office.Interop.Word.Document    wd3 = null;
            try
            {
                wp3 = new Microsoft.Office.Interop.Word.Application();
                wd3 = wp3.Documents.Open(ref filename1, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue);
                wp3.Selection.Find.ClearFormatting();
                wp3.Selection.Find.Replacement.ClearFormatting();
                wp3.Selection.Find.Text = strKey3;
                object objReplace3 = Microsoft.Office.Interop.Word.WdReplace.wdReplaceNone;
                if (wp3.Selection.Find.Execute(ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref objReplace3, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue))
                {
                    object Anchor3           = wp3.Selection.Range;
                    object LinkToFile3       = false;
                    object SaveWithDocument3 = true;
                    Microsoft.Office.Interop.Word.InlineShape Inlineshape3 = wp3.Selection.InlineShapes.AddPicture(
                        ImagePath3, ref LinkToFile3, ref SaveWithDocument3, ref Anchor3);
                    Inlineshape3.Select();
                    Microsoft.Office.Interop.Word.Shape shape3 = Inlineshape3.ConvertToShape();
                    shape3.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;

                    isFindSealLoc3 = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (wd3 != null)
                {
                    wd3.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wd3);
                    wd3 = null;
                }
                if (wp3 != null)
                {
                    wp3.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wp3);
                    wp3 = null;
                }
            }

            string ImagePath4     = pictureBox5.ImageLocation;
            string strKey4        = "6.1.5死角布点";
            object MissingValue4  = Type.Missing;
            bool   isFindSealLoc4 = false;

            Microsoft.Office.Interop.Word.Application wp4 = null;
            Microsoft.Office.Interop.Word.Document    wd4 = null;
            try
            {
                wp4 = new Microsoft.Office.Interop.Word.Application();
                wd4 = wp4.Documents.Open(ref filename1, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue);
                wp4.Selection.Find.ClearFormatting();
                wp4.Selection.Find.Replacement.ClearFormatting();
                wp4.Selection.Find.Text = strKey4;
                object objReplace4 = Microsoft.Office.Interop.Word.WdReplace.wdReplaceNone;
                if (wp4.Selection.Find.Execute(ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref objReplace4, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue))
                {
                    object Anchor4           = wp4.Selection.Range;
                    object LinkToFile4       = false;
                    object SaveWithDocument4 = true;
                    Microsoft.Office.Interop.Word.InlineShape Inlineshape4 = wp4.Selection.InlineShapes.AddPicture(
                        ImagePath4, ref LinkToFile4, ref SaveWithDocument4, ref Anchor4);
                    Inlineshape4.Select();
                    Microsoft.Office.Interop.Word.Shape shape4 = Inlineshape4.ConvertToShape();
                    shape4.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;

                    isFindSealLoc4 = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (wd4 != null)
                {
                    wd4.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wd4);
                    wd4 = null;
                }
                if (wp4 != null)
                {
                    wp4.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wp4);
                    wp4 = null;
                }
            }

            string ImagePath5     = pictureBox6.ImageLocation;
            string strKey5        = "6.1.6货架布点";
            object MissingValue5  = Type.Missing;
            bool   isFindSealLoc5 = false;

            Microsoft.Office.Interop.Word.Application wp5 = null;
            Microsoft.Office.Interop.Word.Document    wd5 = null;
            try
            {
                wp5 = new Microsoft.Office.Interop.Word.Application();
                wd5 = wp5.Documents.Open(ref filename1, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue);
                wp5.Selection.Find.ClearFormatting();
                wp5.Selection.Find.Replacement.ClearFormatting();
                wp5.Selection.Find.Text = strKey5;
                object objReplace5 = Microsoft.Office.Interop.Word.WdReplace.wdReplaceNone;
                if (wp5.Selection.Find.Execute(ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref objReplace5, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue))
                {
                    object Anchor5           = wp5.Selection.Range;
                    object LinkToFile5       = false;
                    object SaveWithDocument5 = true;
                    Microsoft.Office.Interop.Word.InlineShape Inlineshape5 = wp5.Selection.InlineShapes.AddPicture(
                        ImagePath5, ref LinkToFile5, ref SaveWithDocument5, ref Anchor5);
                    Inlineshape5.Select();
                    Microsoft.Office.Interop.Word.Shape shape5 = Inlineshape5.ConvertToShape();
                    shape5.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;

                    isFindSealLoc5 = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (wd5 != null)
                {
                    wd5.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wd5);
                    wd5 = null;
                }
                if (wp5 != null)
                {
                    wp5.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wp5);
                    wp5 = null;
                }
            }

            string ImagePath6     = pictureBox7.ImageLocation;
            string strKey6        = "6.1.7监控系统探头及验证环境布点";
            object MissingValue6  = Type.Missing;
            bool   isFindSealLoc6 = false;

            Microsoft.Office.Interop.Word.Application wp6 = null;
            Microsoft.Office.Interop.Word.Document    wd6 = null;
            try
            {
                wp6 = new Microsoft.Office.Interop.Word.Application();
                wd6 = wp6.Documents.Open(ref filename1, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue);
                wp6.Selection.Find.ClearFormatting();
                wp6.Selection.Find.Replacement.ClearFormatting();
                wp6.Selection.Find.Text = strKey6;
                object objReplace6 = Microsoft.Office.Interop.Word.WdReplace.wdReplaceNone;
                if (wp6.Selection.Find.Execute(ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref objReplace6, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue))
                {
                    object Anchor6           = wp6.Selection.Range;
                    object LinkToFile6       = false;
                    object SaveWithDocument6 = true;
                    Microsoft.Office.Interop.Word.InlineShape Inlineshape6 = wp6.Selection.InlineShapes.AddPicture(
                        ImagePath6, ref LinkToFile6, ref SaveWithDocument6, ref Anchor6);
                    Inlineshape6.Select();
                    Microsoft.Office.Interop.Word.Shape shape6 = Inlineshape6.ConvertToShape();
                    shape6.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;

                    isFindSealLoc6 = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (wd6 != null)
                {
                    wd6.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wd6);
                    wd6 = null;
                }
                if (wp6 != null)
                {
                    wp6.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wp6);
                    wp6 = null;
                }
                MessageBox.Show("导入成功!");
            }


            #region  除后台word占用。
            System.Diagnostics.Process myproc = new System.Diagnostics.Process();
            //得到所有打开的进程
            try
            {
                foreach (Process thisproc in Process.GetProcessesByName("WINWORD"))
                {
                    if (!thisproc.CloseMainWindow())
                    {
                        thisproc.Kill();
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("杀死" + "WINWORD" + "失败!");
            }
            #endregion


            string             filename = Environment.CurrentDirectory.ToString() + "\\bin\\" + Global.templateName + ".doc";
            Spire.Doc.Document document = new Spire.Doc.Document(filename, FileFormat.Docx);

            Spire.Doc.Fields.TextBox      textBox   = document.TextBoxes[2];
            Spire.Doc.Documents.Paragraph paragraph = textBox.Body.AddParagraph();
            Spire.Doc.Fields.TextRange    textRange = paragraph.AppendText(textBox3.Text);
            document.SaveToFile(filename, FileFormat.Docx);

            Spire.Doc.Fields.TextBox      textBox111 = document.TextBoxes[3];
            Spire.Doc.Documents.Paragraph paragraph1 = textBox111.Body.AddParagraph();
            Spire.Doc.Fields.TextRange    textRange1 = paragraph1.AppendText(textBox4.Text);
            document.SaveToFile(filename, FileFormat.Docx);

            Spire.Doc.Fields.TextBox      textBox22  = document.TextBoxes[4];
            Spire.Doc.Documents.Paragraph paragraph2 = textBox22.Body.AddParagraph();
            Spire.Doc.Fields.TextRange    textRange2 = paragraph2.AppendText(textBox7.Text);
            document.SaveToFile(filename, FileFormat.Docx);

            Spire.Doc.Fields.TextBox      textBox33   = document.TextBoxes[5];
            Spire.Doc.Documents.Paragraph paragraph33 = textBox33.Body.AddParagraph();
            Spire.Doc.Fields.TextRange    textRange33 = paragraph33.AppendText(textBox10.Text);
            document.SaveToFile(filename, FileFormat.Docx);

            Spire.Doc.Fields.TextBox      textBox55  = document.TextBoxes[6];
            Spire.Doc.Documents.Paragraph paragraph5 = textBox55.Body.AddParagraph();
            Spire.Doc.Fields.TextRange    textRange5 = paragraph5.AppendText(textBox13.Text);
            document.SaveToFile(filename, FileFormat.Docx);

            Spire.Doc.Fields.TextBox      textBox66  = document.TextBoxes[7];
            Spire.Doc.Documents.Paragraph paragraph6 = textBox66.Body.AddParagraph();
            Spire.Doc.Fields.TextRange    textRange6 = paragraph6.AppendText(textBox16.Text);
            document.SaveToFile(filename, FileFormat.Docx);

            Spire.Doc.Fields.TextBox      textBox88  = document.TextBoxes[8];
            Spire.Doc.Documents.Paragraph paragraph8 = textBox88.Body.AddParagraph();
            Spire.Doc.Fields.TextRange    textRange8 = paragraph8.AppendText(textBox19.Text);
            document.SaveToFile(filename, FileFormat.Docx);

            MessageBox.Show("导入成功!");
            this.Close();
        }
示例#6
0
        private void button3_Click(object sender, EventArgs e)
        {
            object filename1     = Application.StartupPath + "\\bin\\" + Global.templateName;
            string ImagePath     = pictureBox1.ImageLocation;
            string strKey        = "8.4.1空载温度曲线图";
            object MissingValue  = Type.Missing;
            bool   isFindSealLoc = false;

            Microsoft.Office.Interop.Word.Application wp = null;
            Microsoft.Office.Interop.Word.Document    wd = null;
            try
            {
                wp = new Microsoft.Office.Interop.Word.Application();
                wd = wp.Documents.Open(ref filename1, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue,
                                       ref MissingValue, ref MissingValue);
                wp.Selection.Find.ClearFormatting();
                wp.Selection.Find.Replacement.ClearFormatting();
                wp.Selection.Find.Text = strKey;
                object objReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceNone;
                if (wp.Selection.Find.Execute(ref MissingValue, ref MissingValue, ref MissingValue,
                                              ref MissingValue, ref MissingValue, ref MissingValue,
                                              ref MissingValue, ref MissingValue, ref MissingValue,
                                              ref MissingValue, ref objReplace, ref MissingValue,
                                              ref MissingValue, ref MissingValue, ref MissingValue))
                {
                    object Anchor           = wp.Selection.Range;
                    object LinkToFile       = false;
                    object SaveWithDocument = true;
                    Microsoft.Office.Interop.Word.InlineShape Inlineshape = wp.Selection.InlineShapes.AddPicture(
                        ImagePath, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                    Inlineshape.Select();
                    Microsoft.Office.Interop.Word.Shape shape = Inlineshape.ConvertToShape();
                    shape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;

                    isFindSealLoc = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (wd != null)
                {
                    wd.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wd);
                    wd = null;
                }
                if (wp != null)
                {
                    wp.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wp);
                    wp = null;
                }
            }

            string ImagePath1     = pictureBox2.ImageLocation;
            string strKey1        = "8.4.3满载温度曲线图";
            object MissingValue1  = Type.Missing;
            bool   isFindSealLoc1 = false;

            Microsoft.Office.Interop.Word.Application wp1 = null;
            Microsoft.Office.Interop.Word.Document    wd1 = null;
            try
            {
                wp1 = new Microsoft.Office.Interop.Word.Application();
                wd1 = wp1.Documents.Open(ref filename1, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue,
                                         ref MissingValue, ref MissingValue);
                wp1.Selection.Find.ClearFormatting();
                wp1.Selection.Find.Replacement.ClearFormatting();
                wp1.Selection.Find.Text = strKey1;
                object objReplace1 = Microsoft.Office.Interop.Word.WdReplace.wdReplaceNone;
                if (wp1.Selection.Find.Execute(ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue,
                                               ref MissingValue, ref objReplace1, ref MissingValue,
                                               ref MissingValue, ref MissingValue, ref MissingValue))
                {
                    object Anchor1           = wp1.Selection.Range;
                    object LinkToFile1       = false;
                    object SaveWithDocument1 = true;
                    Microsoft.Office.Interop.Word.InlineShape Inlineshape1 = wp1.Selection.InlineShapes.AddPicture(
                        ImagePath1, ref LinkToFile1, ref SaveWithDocument1, ref Anchor1);
                    Inlineshape1.Select();
                    Microsoft.Office.Interop.Word.Shape shape1 = Inlineshape1.ConvertToShape();
                    shape1.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;

                    isFindSealLoc1 = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (wd1 != null)
                {
                    wd1.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wd1);
                    wd1 = null;
                }
                if (wp1 != null)
                {
                    wp1.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wp1);
                    wp1 = null;
                }
            }

            {
                string ImagePath2     = pictureBox3.ImageLocation;
                string strKey2        = "8.4.5热穿透测试温度曲线图";
                object MissingValue2  = Type.Missing;
                bool   isFindSealLoc2 = false;
                Microsoft.Office.Interop.Word.Application wp2 = null;
                Microsoft.Office.Interop.Word.Document    wd2 = null;
                try
                {
                    wp2 = new Microsoft.Office.Interop.Word.Application();
                    wd2 = wp2.Documents.Open(ref filename1, ref MissingValue,
                                             ref MissingValue, ref MissingValue,
                                             ref MissingValue, ref MissingValue,
                                             ref MissingValue, ref MissingValue,
                                             ref MissingValue, ref MissingValue,
                                             ref MissingValue, ref MissingValue,
                                             ref MissingValue, ref MissingValue,
                                             ref MissingValue, ref MissingValue);
                    wp2.Selection.Find.ClearFormatting();
                    wp2.Selection.Find.Replacement.ClearFormatting();
                    wp2.Selection.Find.Text = strKey2;
                    object objReplace1 = Microsoft.Office.Interop.Word.WdReplace.wdReplaceNone;
                    if (wp2.Selection.Find.Execute(ref MissingValue, ref MissingValue, ref MissingValue,
                                                   ref MissingValue, ref MissingValue, ref MissingValue,
                                                   ref MissingValue, ref MissingValue, ref MissingValue,
                                                   ref MissingValue, ref objReplace1, ref MissingValue,
                                                   ref MissingValue, ref MissingValue, ref MissingValue))
                    {
                        object Anchor1           = wp2.Selection.Range;
                        object LinkToFile1       = false;
                        object SaveWithDocument1 = true;
                        Microsoft.Office.Interop.Word.InlineShape Inlineshape1 = wp2.Selection.InlineShapes.AddPicture(
                            ImagePath2, ref LinkToFile1, ref SaveWithDocument1, ref Anchor1);
                        Inlineshape1.Select();
                        Microsoft.Office.Interop.Word.Shape shape1 = Inlineshape1.ConvertToShape();
                        shape1.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapBehind;

                        isFindSealLoc2 = true;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    if (wd2 != null)
                    {
                        wd2.Close();
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(wd2);
                        wd2 = null;
                    }
                    if (wp2 != null)
                    {
                        wp2.Quit();
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(wp2);
                        wp2 = null;
                    }
                }
            }


            string filename = Application.StartupPath.ToString() + "\\bin\\" + Global.templateName + ".doc";

            Spire.Doc.Document document = new Spire.Doc.Document(filename, FileFormat.Docx);

            Spire.Doc.Fields.TextBox      textBox   = document.TextBoxes[1];
            Spire.Doc.Documents.Paragraph paragraph = textBox.Body.AddParagraph();
            Spire.Doc.Fields.TextRange    textRange = paragraph.AppendText(textBox1.Text);
            document.SaveToFile(filename, FileFormat.Docx);

            Spire.Doc.Fields.TextBox      textBox111 = document.TextBoxes[2];
            Spire.Doc.Documents.Paragraph paragraph1 = textBox111.Body.AddParagraph();
            Spire.Doc.Fields.TextRange    textRange1 = paragraph1.AppendText(textBox2.Text);
            document.SaveToFile(filename, FileFormat.Docx);

            Spire.Doc.Fields.TextBox      textBox1111 = document.TextBoxes[3];
            Spire.Doc.Documents.Paragraph paragraph11 = textBox1111.Body.AddParagraph();
            Spire.Doc.Fields.TextRange    textRange11 = paragraph11.AppendText(textBox3.Text);
            document.SaveToFile(filename, FileFormat.Docx);

            MessageBox.Show("导入成功!");

            this.Close();
        }
示例#7
0
        /// <summary>
        /// 对Word文档进行拆分
        /// </summary>
        /// <param name="wordPath">要拆分的Word文档路径</param>
        /// <param name="savePath">拆分后的Word存储路径</param>
        private void SplitWord(string wordPath, string savePath)
        {
            object missing = System.Reflection.Missing.Value; //作为方法传参中的缺省值

            if (!Directory.Exists(savePath))                  //判断Word存储路径是否存在
            {
                Directory.CreateDirectory(savePath);          //如果路径不存在,则创建
            }
            try
            {
                //判断拆分方式:0表示按书签拆分,1表示按分页符拆分,默认为按书签拆分
                switch (flag)
                {
                    #region  书签拆分
                case 0:
                default:
                    Document doc = null;                    //声明一个Word文档对象
                    if (doc == null)                        //判断Word文档对象是否为空
                    {
                        doc = ReadDocument(wordPath);       //调用方法读取指定Word内容,并实例化Word文档对象
                    }
                    int[,] positions = GetPosition(doc);    //获取指定Word中的所有书签的开始和结束位置
                    object oStart = 0;                      //定义变量,表示要拆分的开始位置
                    object oEnd   = 0;                      //定义变量,表示要拆分的结束位置
                    int    row    = positions.GetLength(0); //获取二维数组的行数
                    for (int i = 0; i < row; i++)           //遍历二维数组的所有行
                    {
                        if (i != row - 1)                   //判断是否为最后一行
                        {
                            oEnd = positions[i, 1];         //如果不是最后一行,记录值,作为拆分的结束位置
                        }
                        else
                        {
                            oEnd = doc.Content.End;                     //如果是最后一行,则直接将Word文档的最后位置作为拆分的结束位置
                        }
                        Range tocopy = doc.Range(ref oStart, ref oEnd); //使用开始和结束位置锁定要拆分的范围
                        tocopy.Copy();                                  //复制要拆分范围内容的所有内容
                        Document docto = CreateDocument(textBox1.Text); //调用自定义方法创建一个新的Word文档
                        docto.Content.Paste();                          //将复制的内容粘贴到新创建的Word文档中
                        //设置Word文档的保存路径及文件名(以编号命名)
                        object filename = savePath + "\\" + i.ToString("000") + ".docx";
                        //保存Word文档
                        docto.SaveAs(ref filename, 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);
                        docto.Close(ref missing, ref missing, ref missing); //关闭Word文档
                        oStart = oEnd;                                      //将本次拆分的结束位置作为下一次拆分的开始位置
                    }
                    break;
                    #endregion

                    #region  分页符拆分
                case 1:
                    Spire.Doc.Document original = new Spire.Doc.Document();             //使用Spire插件创建Word文档对象
                    original.LoadFromFile(wordPath);                                    //加载要拆分的Word文档
                    Spire.Doc.Document newWord = new Spire.Doc.Document(textBox1.Text); //使用选择的模板创建一个新的Word文档对象
                    Spire.Doc.Section  section = newWord.AddSection();                  //为新创建的Word文档添加一节
                    int index = 0;                                                      //根据分页来拆分文档
                    foreach (Spire.Doc.Section sec in original.Sections)                //遍历文档所有节
                    {
                        //遍历文档所有子对象
                        foreach (Spire.Doc.DocumentObject obj in sec.Body.ChildObjects)
                        {
                            if (obj is Spire.Doc.Documents.Paragraph)    //判断是否为段落
                            {
                                //创建Word文档中的段落对象
                                Spire.Doc.Documents.Paragraph para = obj as Spire.Doc.Documents.Paragraph;
                                //复制并添加原有段落对象到新文档
                                section.Body.ChildObjects.Add(para.Clone());
                                //遍历所有段落子对象
                                foreach (Spire.Doc.DocumentObject parobj in para.ChildObjects)
                                {
                                    //判断是否为分页符
                                    if (parobj is Spire.Doc.Break && (parobj as Spire.Doc.Break).BreakType == Spire.Doc.Documents.BreakType.PageBreak)
                                    {
                                        //获取段落分页并移除,保存新文档到文件夹
                                        int i = para.ChildObjects.IndexOf(parobj);
                                        section.Body.LastParagraph.ChildObjects.RemoveAt(i);
                                        newWord.SaveToFile(savePath + "\\" + index.ToString("000") + ".docx", Spire.Doc.FileFormat.Docx);
                                        index++;
                                        //实例化Document类对象,添加section,将原文档段落的子对象复制到新文档
                                        newWord = new Spire.Doc.Document(textBox1.Text);
                                        section = newWord.AddSection();
                                        section.Body.ChildObjects.Add(para.Clone());
                                        if (section.Paragraphs[0].ChildObjects.Count == 0)
                                        {
                                            section.Body.ChildObjects.RemoveAt(0);    //移除第一个空白段落
                                        }
                                        else
                                        {
                                            //删除分页符前的子对象
                                            while (i >= 0)
                                            {
                                                section.Paragraphs[0].ChildObjects.RemoveAt(i);
                                                i--;
                                            }
                                        }
                                    }
                                }
                            }
                            //若对象为表格,则添加表格对象到新文档
                            if (obj is Spire.Doc.Table)
                            {
                                section.Body.ChildObjects.Add(obj.Clone());
                            }
                        }
                    }
                    //拆分后的新文档保存至指定文档
                    newWord.SaveToFile(savePath + "\\" + index.ToString("00") + ".docx", Spire.Doc.FileFormat.Docx);
                    original.Close();
                    original.Dispose();
                    newWord.Close();
                    newWord.Dispose();
                    foreach (string file in Directory.GetFiles(savePath)) //遍历拆分完的所有Word文档
                    {
                        deletePagesInFile(file, 1, 1);                    //删除第一页
                    }
                    break;
                    #endregion
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }//异常跳过
        }