c#读取word内容,c#写入word内容,C#操作word
来源:互联网 发布:centos vsftpd 端口 编辑:程序博客网 时间:2024/05/18 23:52
一个项目报表相对比较简单,所以报表打印采用VBA引擎,通过定制Word模版,然后根据模版需要填充数据,然后OK,打印即可。
实现方法:首先需要引用VBA组建,我用的是Office2003 Professional,Dll版本号为Microsoft Word11.0,
另外当然还需要引用Interop.Word.Dll.
代码如下:
#region 打开Word文档,并且返回对象wDoc,wDoc
/// <summary>
/// 打开Word文档,并且返回对象wDoc,wDoc
/// </summary>
/// <param name="FileName">完整Word文件路径+名称</param>
/// <param name="wDoc">返回的Word.Document wDoc对象</param>
/// <param name="WApp">返回的Word.Application对象</param>
public static void CreateWordDocument(string FileName,ref Word.Document wDoc,ref Word.Application WApp)
{
if(FileName == "") return;
Word.Document thisDocument = null;
Word.FormFields formFields = null;
Word.Application thisApplication = new Word.ApplicationClass();
thisApplication.Visible = true;
thisApplication.Caption = "";
thisApplication.Options.CheckSpellingAsYouType = false;
thisApplication.Options.CheckGrammarAsYouType = false;
Object filename = FileName;
Object ConfirmConversions = false;
Object ReadOnly = true;
Object AddToRecentFiles = false;
Object PasswordDocument = System.Type.Missing;
Object PasswordTemplate = System.Type.Missing;
Object Revert = System.Type.Missing;
Object WritePasswordDocument = System.Type.Missing;
Object WritePasswordTemplate = System.Type.Missing;
Object Format = System.Type.Missing;
Object Encoding = System.Type.Missing;
Object Visible = System.Type.Missing;
Object OpenAndRepair = System.Type.Missing;
Object DocumentDirection = System.Type.Missing;
Object NoEncodingDialog = System.Type.Missing;
Object XMLTransform = System.Type.Missing;
try
{
Word.Document wordDoc =
thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
ref Revert,ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
ref NoEncodingDialog, ref XMLTransform );
thisDocument = wordDoc;
wDoc = wordDoc;
WApp = thisApplication;
formFields = wordDoc.FormFields;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
调用上面静态方法,打开目标文件并且把DataGrid中数据填充到对应Word标签中去
#region Word填充数据(For Example)
/// <summary>
/// Word填充数据
/// </summary>
private void WordLoadData()
{
Word.Document wDoc=null;
Word.Application wApp=null;
sysFun.CreateWordDocument("E://监测报告(new).dot",ref wDoc,ref wApp);
//对标签"C"进行填充
object bkmC="C";
if(wApp.ActiveDocument.Bookmarks.Exists("C") == true)
{
wApp.ActiveDocument.Bookmarks.get_Item
(ref bkmC).Select();
}
wApp.Selection.TypeText(this.txt1.Text);
object bkmG = "TWaterTable3";
object unit;
object count; //移动数
object extend;
extend = Word.WdMovementType.wdExtend;
unit = Word.WdUnits.wdCell;
//把DataGrid中数据填充到标签TWaterTable3上
if(wApp.ActiveDocument.Bookmarks.Exists("TWaterTable3") == true)
{
wApp.ActiveDocument.Bookmarks.get_Item
(ref bkmG).Select();
for(int i=0;i<this.gridEX1.RecordCount;i++)
{
if(i==0)
{
count=1;
}
else
{
count=0;
}
//需填充5列数据
wApp.Selection.Move(ref unit,ref count);
wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[0].Text);
count=1;
wApp.Selection.Move(ref unit,ref count);
wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[1].Text);
wApp.Selection.Move(ref unit,ref count);
wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[2].Text);
wApp.Selection.Move(ref unit,ref count);
wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[3].Text);
wApp.Selection.Move(ref unit,ref count);
wApp.Selection.TypeText(gridEX1.GetRow(i).Cells[4].Text);
//换行
wApp.Selection.MoveRight(ref unit,ref count,ref extend);
}
}
}
#endregion
- c#读取word内容,c#写入word内容,C#操作word
- C#读取Word内容
- C#如何读取word内容
- c#读取word内容,c#提取word内容
- c#读取word内容,c#提取word内容
- C#将数据库内容写入Word中
- C#读取Word指定页的内容
- C#读取word文档内容并显示
- C#读取和写入Word
- ASP.NET C#如何读取word,写入word,复制内容到另一个word文档,批量修改文件名
- c# 抽取word文档内容
- C#读word 追加内容
- C#读word 追加内容
- C#修改Word文档内容
- C#读写内容到WORD
- C# 写入Word
- C#操作word word打开和word读取
- 通过C#从数据库中读取内容生成WORD文档
- 超级实用且不花哨的js代码大全 (四) -----JavaScript[对象.属性]集锦
- MiddlegenIDE的使用
- (zz)Firefox和IE书签互导
- 超级实用且不花哨的js代码大全 (五) -----弹窗代码汇总
- OpenBravo 2.35 安装
- c#读取word内容,c#写入word内容,C#操作word
- 提升JSP应用程序的七大绝招
- 超级实用且不花哨的js代码大全 (六) ----代码判断,幻灯片播放器,广告效果
- “Internet来宾帐户”的设置的问题
- J2EE项目:整合SSH开发『新闻发布系统』
- C#创建、打开、读取、写入、保存Excel,Excel打印输出
- OpenBravo 在eclipse下项目的建立
- 用了半天时间做的一个简单的递归下降分析器
- 一个程序员如何月进万金的?