示例#1
0
        public void SaveAs(string strFileName)
        {
            object missing  = System.Reflection.Missing.Value;
            object fileName = strFileName;

            oDoc.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);
        }
示例#2
0
        public void InsertBT()
        {
            object missing = System.Reflection.Missing.Value;

            Word._Application app  = new Word.Application();
            object            path = RandomPath;

            //object fileName = @"\tmp\show.doc";
            // Word.Document doc = app.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

            //doc.Activate();
            Word.Document doc = app.Documents.Add(ref missing, ref missing, ref missing, ref missing);
            doc.Activate();
            Word.Range range = doc.Paragraphs.Last.Range;                                       //定位到段尾
            range.Text = "脚手架专项施工方案";
            range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //文字居中
            //object styleName = "标题 2";
            // range.set_Style(ref styleName);
            range.Font.Size = 20;
            range.Font.Name = "黑体";
            range.Font.Bold = 1;

            doc.SaveAs(ref path, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            app.Quit(ref missing, ref missing, ref missing);
            InsertFile(path.ToString());

            //range.InsertParagraphAfter();
        }
示例#3
0
    public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks)
    {
        Word._Application wordApplication = new Word.Application();
        object            pageBreak       = Word.WdBreakType.wdSectionBreakContinuous;
        object            outputFile      = outputFilename;
        object            fileName        = @"S:\ODS\Insight 2 Oncology\Quality Division Project\Visual Review Scores\Scoring Template\MergeTemplate.docx";
        object            end             = Word.WdCollapseDirection.wdCollapseEnd;

        try
        {
            wordApplication.Visible = false;
            Word.Document  wordDocument = wordApplication.Documents.Add(ref fileName);
            Word.Selection selection    = wordApplication.Selection;

            foreach (string file in filesToMerge)
            {
                if (file.Contains("Scores.docx"))
                {
                    selection.PageSetup.PaperSize   = Word.WdPaperSize.wdPaper11x17;
                    selection.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;
                    selection.InsertFile(file);
                    selection.Collapse(ref end);
                }
                if (!file.Contains("Scores.docx") && insertPageBreaks)
                {
                    selection.InsertFile(file);
                    selection.InsertBreak(pageBreak);
                }
            }
            wordApplication.Selection.Document.Content.Select();
            wordDocument.PageSetup.TopMargin          = (float)50;
            wordDocument.PageSetup.RightMargin        = (float)50;
            wordDocument.PageSetup.LeftMargin         = (float)50;
            wordDocument.PageSetup.BottomMargin       = (float)50;
            selection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle;
            selection.ParagraphFormat.SpaceAfter      = 0.0F;
            selection.ParagraphFormat.SpaceBefore     = 0.0F;
            wordDocument.SaveAs(outputFile + ".docx");
            wordDocument.Close();
            wordDocument = wordApplication.Documents.Open(outputFile + ".docx");
            wordDocument.ExportAsFixedFormat(outputFile + ".pdf", Word.WdExportFormat.wdExportFormatPDF);
            wordDocument = null;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            wordApplication.Quit();
        }
    }
示例#4
0
 public void Save(string path)
 {
     if (WordDoc != null)
     {
         WordDoc.SaveAs(path);
         //Process[] tProcess = Process.GetProcessesByName("WINWORD");
         //foreach (var pid in tProcess)
         //{
         //    Process ps = Process.GetProcessById(pid.Id);
         //    ps.Kill();
         //}
     }
 }
示例#5
0
        public void InsertFile(string path)
        {
            Word.ApplicationClass applicationClass = new Word.ApplicationClass();
            object fileName = showPath;

            Word.Document doct = applicationClass.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            doct.Activate();
            object confirmConversion = false;
            object link       = false;
            object attachment = false;

            applicationClass.Selection.InsertFile(path, ref missing, ref confirmConversion, ref link, ref attachment);
            object pBreak = (int)Word.WdBreakType.wdPageBreak;

            applicationClass.Selection.InsertBreak(ref pBreak);
            doct.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            ((Word._Document)doct).Close(ref missing, ref missing, ref missing);
        }
示例#6
0
        private void btnYazdır_Click(object sender, EventArgs e)
        {
            try
            {
                Word.Application WordApp = new Word.Application();
                Word.Document    WordDoc = new Word.Document();

                DataSet ds = new DataSet();
                if (baglantı.State == ConnectionState.Closed)
                {
                    baglantı.Open();
                }

                OleDbCommand    komut  = new OleDbCommand("Select YazarAdi,KitapAdi from kitaplar", baglantı);
                OleDbDataReader reader = komut.ExecuteReader();

                WordDoc         = WordApp.Documents.Open(Application.StartupPath + "\\bos_kitaplar.docx");
                WordApp.Visible = true;
                int i = 2;
                while (reader.Read())
                {
                    WordDoc.Range().Tables[1].Cell(i, 1).Range.InsertAfter(reader["YazarAdi"].ToString());
                    WordDoc.Range().Tables[1].Cell(i, 2).Range.InsertAfter(reader["KitapAdi"].ToString());
                    WordDoc.Range().Tables[1].Rows.Add();
                    i++;
                }
                WordDoc.SaveAs(Application.StartupPath + "\\tum_kitaplar" + ".doc");
                WordDoc.Close();
                WordApp.Quit();

                WordDoc = null;
                WordApp = null;

                MessageBox.Show("Sorular WORD'a aktarıldı!");
                baglantı.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

                baglantı.Close();
            }
        }
示例#7
0
        public void ExportData(DataGridView srcDgv, string fileName)           //导出数据,传入一个datagridview和一个文件路径
        {
            string type = fileName.Substring(fileName.IndexOf(".") + 1);       //获得数据类型

            if (type.Equals("xls", StringComparison.CurrentCultureIgnoreCase)) //Excel文档
            {
                SaveAs();
            }
            //保存Word文件
            if (type.Equals("doc", StringComparison.CurrentCultureIgnoreCase))
            {
                object           path     = fileName;
                Object           none     = System.Reflection.Missing.Value;
                Word.Application wordApp  = new Word.Application();
                Word.Document    document = wordApp.Documents.Add(ref none, ref none, ref none, ref none);
                //建立表格
                Word.Table table = document.Tables.Add(document.Paragraphs.Last.Range, srcDgv.Rows.Count + 1, srcDgv.Columns.Count, ref none, ref none);
                try
                {
                    for (int i = 0; i < srcDgv.Columns.Count; i++)//设置标题
                    {
                        table.Cell(1, i + 1).Range.Text = srcDgv.Columns[i].HeaderText;
                    }

                    for (int i = 0; i < srcDgv.Rows.Count; i++)//填充数据
                    {
                        for (int j = 0; j < srcDgv.Columns.Count; j++)
                        {
                            table.Cell(i + 2, j + 1).Range.Text = srcDgv[j, i].Value.ToString();
                        }
                    }
                    document.SaveAs(ref path, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none);
                }
                finally
                {
                    wordApp.Quit(ref none, ref none, ref none);
                }
            }
        }
示例#8
0
        public void InsertTitle(string title, string level)
        {
            object missing = System.Reflection.Missing.Value;

            Word._Application app  = new Word.Application();
            object            path = RandomPath;

            Word.Document doc = app.Documents.Add(ref missing, ref missing, ref missing, ref missing);
            doc.Activate();
            Word.Range range = doc.Paragraphs.Last.Range;//定位到段尾
            //range.InsertParagraphAfter();



            range.Text        = level + "." + title;
            range.Font.Italic = 1;
            range.Font.Color  = Word.WdColor.wdColorBlue;

            //object styleName = "标题 3";
            //range.set_Style(ref styleName);
            doc.SaveAs(ref path, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            app.Quit(ref missing, ref missing, ref missing);
            InsertFile(path.ToString());
        }
        /// <summary>
        /// 动态创建table到word
        /// </summary>
        protected void CreateTableToExcel()
        {
            Word.Application app = null;
            Word.Document    doc = null;
            try
            {
                //构造数据
                List <Student> datas = new List <Student>();
                datas.Add(new Student {
                    Leader = "小李", Name = "张三", Score = 498, StuClass = "一班"
                });
                datas.Add(new Student {
                    Leader = "陈飞", Name = "李四", Score = 354, StuClass = "二班"
                });
                datas.Add(new Student {
                    Leader = "陈飞", Name = "小红", Score = 502, StuClass = "二班"
                });
                datas.Add(new Student {
                    Leader = "王林", Name = "丁爽", Score = 566, StuClass = "三班"
                });
                var cate = datas.GroupBy(s => s.StuClass);

                int    rows     = cate.Count() + 1; //表格行数加1是为了标题栏
                int    cols     = 5;                //表格列数
                object oMissing = System.Reflection.Missing.Value;
                app = new Word.Application();       //创建word应用程序
                doc = app.Documents.Add();          //添加一个word文档

                //输出大标题加粗加大字号水平居中
                app.Selection.Font.Bold = 700;
                app.Selection.Font.Size = 16;
                app.Selection.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                app.Selection.Text = "班级成绩统计单";

                //换行添加表格
                object line = Word.WdUnits.wdLine;
                app.Selection.MoveDown(ref line, oMissing, oMissing);
                app.Selection.TypeParagraph();//换行
                Word.Range range = app.Selection.Range;
                Word.Table table = app.Selection.Tables.Add(range, rows, cols, ref oMissing, ref oMissing);

                //设置表格的字体大小粗细
                table.Range.Font.Size = 10;
                table.Range.Font.Bold = 0;

                //设置表格标题
                int rowIndex = 1;
                table.Cell(rowIndex, 1).Range.Text = "班级";
                table.Cell(rowIndex, 2).Range.Text = "姓名";
                table.Cell(rowIndex, 3).Range.Text = "成绩";
                table.Cell(rowIndex, 4).Range.Text = "人数";
                table.Cell(rowIndex, 5).Range.Text = "班主任";

                //循环数据创建数据行
                rowIndex++;
                foreach (var i in cate)
                {
                    table.Cell(rowIndex, 1).Range.Text = i.Key;                //班级
                    table.Cell(rowIndex, 4).Range.Text = i.Count().ToString(); //人数
                    table.Cell(rowIndex, 5).Range.Text = i.First().Leader;     //班主任
                    table.Cell(rowIndex, 2).Split(i.Count(), 1);               //分割名字单元格
                    table.Cell(rowIndex, 3).Split(i.Count(), 1);               //分割成绩单元格

                    //对表格中的班级、姓名,成绩单元格设置上下居中
                    table.Cell(rowIndex, 1).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    table.Cell(rowIndex, 4).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                    table.Cell(rowIndex, 5).VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

                    //构建姓名,成绩数据
                    foreach (var x in i)
                    {
                        table.Cell(rowIndex, 2).Range.Text = x.Name;
                        table.Cell(rowIndex, 3).Range.Text = x.Score.ToString();
                        rowIndex++;
                    }
                }

                //导出到文件
                string newFile       = DateTime.Now.ToString("yyyyMMddHHmmssss") + ".doc";
                string physicNewFile = Server.MapPath(newFile);
                doc.SaveAs(physicNewFile,
                           oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
                           oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close();//关闭文档
                }
                if (app != null)
                {
                    app.Quit();//退出应用程序
                }
            }
        }
示例#10
0
        /// <summary>
        /// cover file word to body hmtl of mail,which embed fictures
        /// </summary>
        /// <param name="PathWordTemplate">path to word template file</param>
        /// <param name="Mail">Mail is embed body</param>
        /// <returns></returns>
        private static bool HTMLBody(DataSet DataFill, string PathWordTemplate, ref Email Mail)
        {
            string path = FrameworkParams.TEMP_FOLDER + @"\WordTemp";
            if (!System.IO.Directory.Exists(path))
                System.IO.Directory.CreateDirectory(path);
            path += PathWordTemplate.Substring(PathWordTemplate.LastIndexOf("\\"));
            WordDocument WordDoc = new WordDocument();
            try
            {
                WordDoc.Open(PathWordTemplate);
                for (int i = 0; i < DataFill.Tables.Count; i++)
                {
                    WordDoc.MailMerge.ExecuteGroup(DataFill.Tables[i]);
                }
                WordDoc.Save(path);

                Word.ApplicationClass wd = new Word.ApplicationClass();
                Word.Document document = new Word.Document();
                object fileName = (object)path;
                object newTemplate = false;
                object docType = 0;
                object isVisible = true;
                object missing = System.Reflection.Missing.Value;

                document = wd.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);

                object oFileName = (object)(path.Substring(0, path.LastIndexOf(".")) + ".html");
                object oSaveFormat = (object)(Word.WdSaveFormat.wdFormatHTML);

                if (System.IO.File.Exists(oFileName.ToString()))
                    System.IO.File.Delete(oFileName.ToString());

                document.SaveAs(ref oFileName,
                                ref oSaveFormat,
                                ref missing,
                                ref missing,
                                ref missing,
                                ref missing,
                                ref missing,
                                ref missing,
                                ref missing,
                                ref missing,
                                ref missing);
                document.Close(ref missing, ref missing, ref missing);
                wd.Quit(ref missing, ref missing, ref missing);

                Mht mailbody = new Mht();
                mailbody.UnlockComponent("MHT-TEAMBEAN_1E1F4760821H");
                mailbody.UseCids = true;
                Mail = mailbody.GetEmail(oFileName.ToString());

                return true;
            }
            catch
            {
                return false;
            }
        }
示例#11
0
        // ---------------------------------------------
        //             Copy Documents
        // ---------------------------------------------
        public static object CopyDocumentReplaceContents(
            object fromFileName,
            object destinationFileName,
            List <WordDocumentTasks.TagStructure> tag
            )
        {
            Word.Application vkWordApp =
                new Word.Application();

            object saveFile = destinationFileName;

            object vkReadOnly = false;
            object vkVisible  = true;
            object vkFalse    = false;
            object vkTrue     = true;
            object vkDynamic  = 2;


            object vkMissing = System.Reflection.Missing.Value;

            // Let's make the word application visible
            vkWordApp.Visible = true;
            vkWordApp.Activate();

            // Let's open the document
            Word.Document vkMyDoc = vkWordApp.Documents.Open(
                ref fromFileName, ref vkMissing, ref vkReadOnly,
                ref vkMissing, ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkMissing, ref vkVisible);

            // Let's create a new document
            Word.Document vkNewDoc = vkWordApp.Documents.Add(
                ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkVisible);

            // Select and Copy from the original document
            vkMyDoc.Select();
            vkWordApp.Selection.Copy();

            // Paste into new document as unformatted text
            vkNewDoc.Select();
            vkWordApp.Selection.PasteSpecial(ref vkMissing, ref vkFalse,
                                             ref vkMissing, ref vkFalse, ref vkDynamic,
                                             ref vkMissing, ref vkMissing);

            // Save the new document
            vkNewDoc.SaveAs(ref saveFile, ref vkMissing,
                            ref vkMissing, ref vkMissing, ref vkMissing,
                            ref vkMissing, ref vkMissing, ref vkMissing,
                            ref vkMissing, ref vkMissing, ref vkMissing);


            // Copy elements
            // FromString => toString
            //
            // underdevelopment
            //
            vkNewDoc.Select();

            foreach (var t in tag)
            {
                FindAndReplace(t.Tag, t.TagValue, 1, vkWordApp, vkMyDoc);
            }

            vkNewDoc.Save();

            // close the new document
            vkNewDoc.Close(ref vkFalse, ref vkMissing, ref vkMissing);

            // close the original document
            vkMyDoc.Close(ref vkFalse, ref vkMissing, ref vkMissing);

            // close word application
            vkWordApp.Quit(ref vkFalse, ref vkMissing, ref vkMissing);

            return(saveFile);
        }
示例#12
0
        public void ExportData(DataGridView srcDgv, string fileName)           //导出数据,传入一个datagridview和一个文件路径
        {
            string type = fileName.Substring(fileName.IndexOf(".") + 1);       //获得数据类型

            if (type.Equals("xls", StringComparison.CurrentCultureIgnoreCase)) //Excel文档
            {
                Excel.Application excel = new Excel.Application();
                try
                {
                    excel.DisplayAlerts = false;
                    excel.Workbooks.Add(true);
                    excel.Visible = false;

                    for (int i = 0; i < srcDgv.Columns.Count; i++)//设置标题
                    {
                        excel.Cells[2, i + 1] = srcDgv.Columns[i].HeaderText;
                    }

                    for (int i = 0; i < srcDgv.Rows.Count; i++)//填充数据
                    {
                        for (int j = 0; j < srcDgv.Columns.Count; j++)
                        {
                            if (srcDgv[j, i].ValueType.ToString() == "System.Byte[]")
                            {
                                excel.Cells[i + 3, j + 1] = "System.Byte[]";
                            }
                            else
                            {
                                excel.Cells[i + 3, j + 1] = srcDgv[j, i].Value;
                            }
                        }
                    }

                    excel.Workbooks[1].SaveCopyAs(fileName);//保存
                }
                finally
                {
                    excel.Quit();
                }
                return;
            }
            //保存Word文件
            if (type.Equals("doc", StringComparison.CurrentCultureIgnoreCase))
            {
                object           path     = fileName;
                Object           none     = System.Reflection.Missing.Value;
                Word.Application wordApp  = new Word.Application();
                Word.Document    document = wordApp.Documents.Add(ref none, ref none, ref none, ref none);
                //建立表格
                Word.Table table = document.Tables.Add(document.Paragraphs.Last.Range, srcDgv.Rows.Count + 1, srcDgv.Columns.Count, ref none, ref none);
                try
                {
                    for (int i = 0; i < srcDgv.Columns.Count; i++)//设置标题
                    {
                        table.Cell(1, i + 1).Range.Text = srcDgv.Columns[i].HeaderText;
                    }

                    for (int i = 0; i < srcDgv.Rows.Count; i++)//填充数据
                    {
                        for (int j = 0; j < srcDgv.Columns.Count; j++)
                        {
                            string a = srcDgv[j, i].ValueType.ToString();
                            if (a == "System.Byte[]")
                            {
                                PictureBox   pp  = new PictureBox();
                                byte[]       pic = (byte[])(srcDgv[j, i].Value); //将数据库中的图片转换成二进制流
                                MemoryStream ms  = new MemoryStream(pic);        //将字节数组存入到二进制流中
                                pp.Image = Image.FromStream(ms);                 //二进制流Image控件中显示
                                pp.Image.Save(@"C:\22.bmp");                     //将图片存入到指定的路径
                                object aaa = table.Cell(i + 2, j + 1).Range;
                                wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                                wordApp.Selection.InlineShapes.AddPicture(@"C:\22.bmp", ref none, ref none, ref aaa);
                                pp.Dispose();
                            }
                            else
                            {
                                table.Cell(i + 2, j + 1).Range.Text = srcDgv[j, i].Value.ToString();
                            }
                        }
                    }
                    document.SaveAs(ref path, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none);
                    document.Close(ref none, ref none, ref none);
                    if (File.Exists(@"C:\22.bmp"))
                    {
                        File.Delete(@"C:\22.bmp");
                    }
                }
                finally
                {
                    wordApp.Quit(ref none, ref none, ref none);
                }
            }
        }
示例#13
0
        public void Export_Data_To_Word(DataGridView DGV, string filename)
        {
            if (DGV.Rows.Count != 0)
            {
                int RowCount    = DGV.Rows.Count;
                int ColumnCount = DGV.Columns.Count;
                Object[,] DataArray = new object[RowCount + 1, ColumnCount + 1];

                //add rows
                int r = 0;
                for (r = 0; r <= RowCount - 1; r++)
                {
                    for (int c = 0; c <= ColumnCount - 1; c++)
                    {
                        DataArray[r, c] = DGV.Rows[r].Cells[c].Value;
                    } //end row loop
                }     //end column loop



                Word.Document oDoc = new Word.Document();
                oDoc.Application.Visible = true;

                //page orintation
                oDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;


                dynamic oRange   = oDoc.Content.Application.Selection.Range;
                string  oTemp    = "";
                Object  oMissing = System.Reflection.Missing.Value;
                for (r = 0; r <= RowCount - 1; r++)
                {
                    for (int c = 0; c <= ColumnCount - 1; c++)
                    {
                        oTemp = oTemp + DataArray[r, c] + "\t";
                    }
                }

                //table format
                oRange.Text = oTemp;

                object Separator       = Word.WdTableFieldSeparator.wdSeparateByTabs;
                object ApplyBorders    = true;
                object AutoFit         = true;
                object AutoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitContent;

                oRange.ConvertToTable(ref Separator, ref RowCount, ref ColumnCount,
                                      Type.Missing, Type.Missing, ref ApplyBorders,
                                      Type.Missing, Type.Missing, Type.Missing,
                                      Type.Missing, Type.Missing, Type.Missing,
                                      Type.Missing, ref AutoFit, ref AutoFitBehavior, Type.Missing);

                oRange.Select();

                oDoc.Application.Selection.Tables[1].Select();
                oDoc.Application.Selection.Tables[1].Rows.AllowBreakAcrossPages = 0;
                oDoc.Application.Selection.Tables[1].Rows.Alignment             = 0;
                oDoc.Application.Selection.Tables[1].Rows[1].Select();
                oDoc.Application.Selection.InsertRowsAbove(1);
                oDoc.Application.Selection.Tables[1].Rows[1].Select();


                //header row style
                oDoc.Application.Selection.Tables[1].Rows[1].Range.Bold      = 1;
                oDoc.Application.Selection.Tables[1].Rows[1].Range.Font.Name = "Tahoma";
                oDoc.Application.Selection.Tables[1].Rows[1].Range.Font.Size = 14;

                //add header row manually
                for (int c = 0; c <= ColumnCount - 1; c++)
                {
                    oDoc.Application.Selection.Tables[1].Cell(1, c + 1).Range.Text = DGV.Columns[c].HeaderText;
                }

                //table style
                oDoc.Application.Selection.Tables[1].set_Style("Grid Table 4 - Accent 5");
                oDoc.Application.Selection.Tables[1].Rows[1].Select();
                oDoc.Application.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

                //header text
                foreach (Word.Section section in oDoc.Application.ActiveDocument.Sections)
                {
                    Word.Range headerRange = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                    headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage);
                    headerRange.Text      = "Course list table";
                    headerRange.Font.Size = 16;
                    headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                }


                //save the file
                oDoc.SaveAs(filename);
            }
        }
示例#14
0
文件: UIfcm.cs 项目: DR2010/FCM2
        /// <summary>
        /// Get source document. Open a FileDialog window for user to select single/multiple files for
        /// parsing.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butSourceDocument_Click(object sender, System.EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                object fileName = openFileDialog.FileName;
                object saveFile = fileName + "_Vk.doc";


                object vk_read_only = false;
                object vk_visible   = true;
                object vk_false     = false;
                object vk_true      = true;
                object vk_dynamic   = 2;


                object vk_missing = System.Reflection.Missing.Value;

                // Let make the word application visible
                vk_word_app.Visible = true;
                vk_word_app.Activate();

                // Let's open the document
                Word.Document vk_my_doc = vk_word_app.Documents.Open(ref fileName,
                                                                     ref vk_missing, ref vk_read_only, ref vk_missing, ref vk_missing,
                                                                     ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing,
                                                                     ref vk_missing, ref vk_missing, ref vk_visible);

                // Let's create a new document
                Word.Document vk_new_doc = vk_word_app.Documents.Add(ref vk_missing,
                                                                     ref vk_missing, ref vk_missing, ref vk_visible);

                // Select and Copy from the original document
                vk_my_doc.Select();
                vk_word_app.Selection.Copy();

                // Paste into new document as unformatted text
                vk_new_doc.Select();
                vk_word_app.Selection.PasteSpecial(ref vk_missing, ref vk_false,
                                                   ref vk_missing, ref vk_false, ref vk_dynamic, ref vk_missing, ref vk_missing);

                // close the original document
                vk_my_doc.Close(ref vk_false, ref vk_missing, ref vk_missing);

                // Let try to replace Vahe with VAHE in the new document
                object vk_find    = "^l";
                object vk_replace = " ";
                object vk_num     = 1;

                vk_new_doc.Select();

                WordDocumentTasks.FindAndReplace(
                    "<<Client Name>>",
                    "tEST",
                    vk_num,
                    vk_word_app);

                // Save the new document
                vk_new_doc.SaveAs(ref saveFile, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing,
                                  ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing);

                // close the new document
                vk_new_doc.Close(ref vk_false, ref vk_missing, ref vk_missing);

/*
 *                              // Let's get the content from the document
 *                              Word.Paragraphs vk_my_doc_p = vk_new_doc.Paragraphs;
 *                              // Count number of paragraphs in the file
 *                              long p_count = vk_my_doc_p.Count;
 *                              // step through the paragraphs
 *                              for( int i=1; i<=p_count; i++ )
 *                              {
 *                                      Word.Paragraph vk_p = vk_my_doc_p.Item( i );
 *                                      Word.Range vk_r = vk_p.Range;
 *                                      string text = vk_r.Text;
 *
 *                                      MessageBox.Show( text );
 *                              }
 */


                // close word application
                vk_word_app.Quit(ref vk_false, ref vk_missing, ref vk_missing);
            }
        }