操作 office word
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://maddish.blog.51cto.com/282288/74197 |
using System; using System.IO; using Microsoft.Office.Interop.Word; using @MSWord=Microsoft.Office.Interop.Word; namespace Exercise.Office { class Program { static void Main() { // 通过反射产生一个函数的默认参数 object Nothing = System.Reflection.Missing.Value; // 保存路径和DOC文件名称] string wordName = "C:\\" + DateTime.Now.ToString("yyyymmddhhmmss") + ".doc"; " 创建新文档 .."#region " 创建新文档 .." Application wordApp = new ApplicationClass(); if (File.Exists(wordName)) File.Delete(wordName); Document wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); #endregion " 设置页眉 .. "#region " 设置页眉 .. " wordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader; wordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]"); wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight; wordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; #endregion " 设置文档内容 .."#region " 设置文档内容 .." // 设置文档的行间距 wordApp.Selection.ParagraphFormat.LineSpacing = 15f; wordDoc.Paragraphs.Last.Range.Text = "[文档内容]"; // 创建一个两行三列 表格.. Table newTable = wordDoc.Tables.Add(wordApp.Selection.Range, 2, 3, ref Nothing, ref Nothing); // 设置宽 newTable.Columns[1].Width = 20f; newTable.Columns[2].Width = 50f; newTable.Columns[3].Width = 100f; // 设置表格内容 newTable.Cell(1, 1).Range.Text = "这是第一行第一列的内容"; newTable.Cell(1, 1).Range.Font.Color = WdColor.wdColorRed; // 设置字体粗体 newTable.Cell(1, 1).Range.Bold = 2; // 设置合并单元格 newTable.Cell(1, 1).Merge(newTable.Cell(1, 3)); // 设置垂直居中 wordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; // 设置水平居中 wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; // 设置第二行内容 newTable.Cell(2, 1).Range.Text = "这是第二行第一列"; newTable.Cell(2, 2).Range.Text = "这是第二行第二列"; newTable.Cell(2, 3).Range.Text = "这是第二行第三列"; wordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now; wordDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphRight; #endregion _Document doc = wordDoc; object objName = wordName; doc.SaveAs ( ref objName, 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 ); doc.Close(ref Nothing, ref Nothing, ref Nothing); _Application app = wordApp; app.Quit(ref Nothing, ref Nothing, ref Nothing); Console.Write("ok.."); Console.Read(); } } } 本文出自 “maddish” 博客,请务必保留此出处http://maddish.blog.51cto.com/282288/74197 本文出自 51CTO.COM技术博客 |


using System;
maddish
博客统计信息
热门文章
最新评论
友情链接