时间:2021-05-02
有时我们希望在C#程序中能够直接操作word,实现向word中写入数据打印或呈交给相关人员,下面就以一个例子讲解了具体的操作方法。
本例演示了打开word文档、在word文档中创建表格,合并单元格,保存word文档并退出等功能。
using System; using System.Web; using Microsoft.Office.Interop.Word;
namespace CustomFileAccess { public class WordOperator { public void CreateWord() { Microsoft.Office.Interop.Word.Application wordApp = new Application(); Microsoft.Office.Interop.Word.Document wordDocument = new Document(); Microsoft.Office.Interop.Word.Table wordTable; Microsoft.Office.Interop.Word.Table wordTableCopy;
object myNull = System.Reflection.Missing.Value;
object strPath = HttpContext.Current.Server.MapPath(@"WordTemplete\MyWordTemplete.doc"); object styleName = "Table Grid 8";
try { wordDocument = wordApp.Documents.Open(ref strPath, ref myNull, ref myNull, ref myNull, ref myNull, ref myNull, ref myNull, ref myNull, ref myNull, ref myNull, ref myNull, ref myNull, ref myNull, ref myNull, ref myNull, ref myNull);
object start = 0; object end = 0;
Microsoft.Office.Interop.Word.Range wordRange = wordDocument.Range(ref start, ref end);
wordTable = wordDocument.Tables.Add(wordRange, 3, 13, ref myNull, ref myNull);
wordTable.Borders.OutsideColor = WdColor.wdColorAutomatic; wordTable.Borders.OutsideColorIndex = WdColorIndex.wdAuto; wordTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle; wordTable.Borders.OutsideLineWidth = WdLineWidth.wdLineWidth050pt;
wordTable.Borders.InsideColor = WdColor.wdColorAutomatic; wordTable.Borders.InsideColorIndex = WdColorIndex.wdAuto; wordTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle; wordTable.Borders.InsideLineWidth = WdLineWidth.wdLineWidth050pt;
wordDocument.Tables[1].Cell(1, 1).Merge(wordDocument.Tables[1].Cell(2, 1));
wordDocument.Tables[1].Cell(1, 1).Range.Text = "cell 1, 1"; wordDocument.Tables[1].Cell(2, 2).Range.Text = "cell 2, 2";
wordDocument.Tables[1].Select(); wordApp.Selection.Copy();
wordDocument.Tables[1].Cell(1, 2).Range.Text = "The First Table";
object myunit = Microsoft.Office.Interop.Word.WdUnits.wdStory; wordApp.Selection.EndKey(ref myunit, ref myNull);
wordApp.Selection.TypeParagraph();
wordApp.Selection.Paste();
wordDocument.Tables[2].Cell(1, 1).Range.Text = "The Second Table";
wordDocument.Save(); } catch { wordDocument.Close(ref myNull, ref myNull, ref myNull); wordApp.Quit(ref myNull, ref myNull, ref myNull); if (wordDocument != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDocument); wordDocument = null; } if (wordApp != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp); wordApp = null; } GC.Collect(); throw new Exception("文档生成失败!");
} finally { wordDocument.Close(ref myNull, ref myNull, ref myNull); wordApp.Quit(ref myNull, ref myNull, ref myNull); }
} } }
本文源自:翔宇亭——IT乐园(http://),转载请保留此信息!声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
如何用C#编程实现动态生成Word文档并填充数据的效果呢?要使用C#操作word,首先要添加引用1、添加引用->COM->MicrosoftWord11.0Ob
usingWord;下面的例子中包括C#对Word文档的创建、插入表格、设置样式等操作:(例子中代码有些涉及数据信息部分被省略,重要是介绍一些C#操作word文
本文实例讲述了C#操作word的方法。分享给大家供大家参考,具体如下:#region读取word//////读取word所有文字内容(不包含表格)//////w
委托给了C#操作函数的灵活性,我们可使用委托像操作变量一样来操作函数,其实这个功能并不是C#的首创,早在C++时代就有函数指针这一说法,而在我看来委托就是C#的
针对Word的操作是很多程序都具备的功能,本文即以实例展示使用C#实现在word中插入页眉页脚的方法,供大家参考借鉴,具体方法如下:一、插入页脚的方法:publ