예제 #1
0
        private void btnDocMake_Click(object sender, EventArgs e)
        {
            string currentPath = Directory.GetParent(path).FullName;

            Word.Application WordApp;
            Word.Document    WordDoc;
            WordApp = new Word.Application();
            WordDoc = WordApp.Documents.Open(path);

            ReplaceWord("{name}", name, WordDoc);
            ReplaceWord("{age}", age, WordDoc);
            ReplaceWord("{date}", date, WordDoc);
            WordDoc.SaveAs(currentPath + @"/result.docx");
            WordApp.Visible = true;
        }
예제 #2
0
파일: Word.cs 프로젝트: yxx5006/CADAddin
        public static void ToWord()
        {
            Stopwatch watch = new Stopwatch();

            watch.Start();
            ed.WriteMessage("\n获取问题记录,请稍后...");
            IMGLayers = GetIMGLayers();
            // List<DBText> ents = GetLayerDbtxtByLayerName(IMGLayers);
            List <MText> txts = GetLayerDbtxtByLayerName(IMGLayers);

            if (txts.Count == 0)
            {
                watch.Stop();
                ed.WriteMessage("\n未发现任何问题记录,请核对。");
            }
            else
            {
                MSWord.Application WordApp;
                MSWord.Document    WordDoc;
                CreatWordApp(out WordApp, out WordDoc);
                object format   = MSWord.WdSaveFormat.wdFormatDocumentDefault;
                object filePath = Path.GetDirectoryName(acCurDb.Filename) + "\\" +
                                  Path.GetFileNameWithoutExtension(acCurDb.Filename) + "-问题记录.docx";
                object endkeyunit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
                object unit       = Microsoft.Office.Interop.Word.WdUnits.wdLine;
                object count      = 1;

                List <string> txtcontent = (from txt in txts where txt != null select txt.Contents).ToList();



                try
                {
                    ed.WriteMessage("\n设置IMG-BIM问题记录模板.请稍后...");
                    SetPage(WordApp, WordDoc);
                    AddPageHeaderFooter(WordApp, WordDoc);
                    CreatFristTable(WordApp, WordDoc);
                    ed.WriteMessage("\n模板创建完成,正在写入数据.请稍后...");
                }
                catch (Exception)
                {
                    ed.WriteMessage("\n创建Word过程发生错误,程序异常退出!");
                }
                WordDoc.Tables[1].Cell(1, 2).Range.Text = "测试工程名";
                ed.WriteMessage(txtcontent.Count.ToString());
                for (int i = 1; i < txtcontent.Count / 3; i++)
                {
                    WordDoc.Tables[1].Select();
                    WordApp.Selection.Copy();
                    WordApp.Selection.EndKey(ref endkeyunit, ref _nothing);
                    //WordApp.Selection.MoveUp(ref unit, ref count, ref _nothing);
                    object pBreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
                    WordApp.Selection.InsertBreak(ref pBreak);
                    WordApp.Selection.Paste();
                }
                for (int i = 1; i < txtcontent.Count / 3 + 1; i++)
                {
                    for (int j = i * 3 - 3; j < i * 3; j++)
                    {
                        WordDoc.Tables[i].Cell(2, 4).Range.Text = "叶舒帆";
                        switch (j % 3)
                        {
                        case 0:
                            WordDoc.Tables[i].Cell(4, 2).Range.Text = txtcontent[j];
                            break;

                        case 1:
                            WordDoc.Tables[i].Cell(2, 10).Range.Text = txtcontent[j];
                            break;

                        case 2:
                            WordDoc.Tables[i].Cell(2, 6).Range.Text = txtcontent[j];
                            break;
                        }
                    }
                }

                WordDoc.SaveAs(ref filePath, 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);
                WordDoc.Close(ref _nothing, ref _nothing, ref _nothing);
                WordApp.Quit(ref _nothing, ref _nothing, ref _nothing);
                watch.Stop();
                ed.WriteMessage("\n问题导出完成耗时:{0} ms,请到图纸目录下查看。", watch.ElapsedMilliseconds.ToString());
            }
        }