Exemplo n.º 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            string xmlDefinitionPath = @"E:\ibts.definition.xml";
            string dataFilePath = @"E:\data.json";

            dataDoc = getDocData(dataFilePath);
            definitionDTO = getDocDefinition(xmlDefinitionPath);

            XmlNode node = dataDoc.SelectSingleNode("/data/pagingDTO.check2");
            XmlNode node2 = dataDoc.SelectSingleNode("/data/companyDTO/companyCode");

            // return;

            object path; //文件路径变量
            object destPath; //文件路径变量
            MSWord.Application wordApp = null; //Word 应用程序变量
            MSWord.Document wordDoc = null; //Word文档变量

             path = @"E:\T22.docm"; //路径
             destPath = @"E:\T25.docx"; //路径

             //由于使用的是COM库,因此有许多变量需要用Missing.Value代替
             Object Nothing = Missing.Value;

             try
             {
                 wordApp = new MSWord.ApplicationClass(); //初始化

                 wordDoc = wordApp.Documents.Open(ref path,
                    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);

                 foreach (MSWord.InlineShape ishape in wordDoc.InlineShapes)
                 {
                     MSWord.Field f = ishape.Field;

                     if (ishape.Type != MSWord.WdInlineShapeType.wdInlineShapeOLEControlObject)
                         continue;

                     String controlName = ishape.OLEFormat.Object.GetType().InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, ishape.OLEFormat.Object, null).ToString();
                     DocItemDefinitionDTO item = definitionDTO.getItem(controlName);
                     if (item == null)
                         continue;

                     // 判断为 CheckBox
                     if (f.Code.Text.Contains("Forms.CheckBox.1"))
                     {
                         Microsoft.Vbe.Interop.Forms.CheckBox control = (Microsoft.Vbe.Interop.Forms.CheckBox)ishape.OLEFormat.Object;
                         setCheckBoxControl(item, control);
                     } if (f.Code.Text.Contains("Forms.Label.1"))
                     {
                         Microsoft.Vbe.Interop.Forms.Label control = (Microsoft.Vbe.Interop.Forms.Label)ishape.OLEFormat.Object;
                         setLabelControl(item, control);
                     }
                     else
                         continue;
                 }

                 MSWord.ContentControls contentControls = wordDoc.ContentControls;
                 foreach (MSWord.ContentControl control in contentControls)
                 {
                     String controlName = control.Tag;

                     DocItemDefinitionDTO item = definitionDTO.getItem(controlName);
                     if (item == null)
                         continue;

                     if (control.Type == MSWord.WdContentControlType.wdContentControlRichText)
                     {
                         setRichTextContentBoxControl(item, control);
                     }
                 }

                 //WdSaveFormat 为Word 文档的保存格式
                 object format = MSWord.WdSaveFormat.wdFormatDocumentDefault;

                 //将wordDoc文档对象的内容保存为DOC文档
                 wordDoc.SaveAs(ref destPath, ref format, ref Nothing, ref Nothing,
                        ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                        ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                        ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                 MessageBox.Show("Success!");
             }
             catch (Exception e1)
             {
                 MessageBox.Show("Error:" + e1.Message);
             }
             finally
             {
                 //关闭wordDoc文档对象
                 if (wordDoc != null) wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);

                 //关闭wordApp组件对象
                 if (wordApp != null) wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
             }
        }