我怎么能在C#上的数据表上创建单词的字母 [英] How I could to create letter on word from datatable on C#

查看:66
本文介绍了我怎么能在C#上的数据表上创建单词的字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以创建一个单词模板吗?

然后填写单词模板字段中的所有数据?



请指导我...如果你知道,请提供一些代码。



我尝试过:



Object oMissing = System.Reflection.Missing.Value;

Object oTemplatePath =D:\\MyTemplate.dotx;



应用程序wordApp = new Application();

文档wordDoc = new Document();



wordDoc = wordApp.Documents.Add( ref oTemplatePath,ref oMissing,ref oMissing,ref oMissing);



foreach(字段myMergeField在wordDoc.Fields中)

{





范围rngFieldCode = myMergeField.Code;



String fieldText = rngFieldCode.Text ;







//只获得邮件领域



if(fieldText.StartsWith(MERGEFIELD))

{



//文字格式为



// MERGEFIELD MyFieldName \\ * MERGEFORMAT



//这是为了获得FIELDNAME而被编辑 MyFieldName



Int32 endMerge = fieldText.IndexOf(\\);



Int32 fieldNameLength = fieldText.Length - endMerge;



String fieldName = fieldText.Substring(11,endMerge - 11);



//当用户输入.dot文件时给予FIELDNAMES



fieldName = fieldName.Trim();



// ****字段替换实现在这里**** //



//程序员可以自己实现这里



if(fieldName ==Name)

{



myMergeField.Select();



wordApp.Selection.TypeText(Vibhusha);



}



}



}

wordDoc.SaveAs(myfile.doc);

wordApp.Documents.Open(myFile.doc);

wordApp.Application.Quit();







- >我必须这样做但是在波纹管代码上我得到一些错误,



应用程序wordApp = new Application(); ---> System.web.ui.page .application是一个'Property',但是像'type'一样使用





文件wordDoc = new Document(); --- >找不到类型或命名空间名称'Document'(您是否缺少using指令或程序集引用?

can i create once a word template?
Then fill all data on word template field?

please guide me...and provide some code if you know.

What I have tried:

Object oMissing = System.Reflection.Missing.Value;
Object oTemplatePath = "D:\\MyTemplate.dotx";

Application wordApp = new Application();
Document wordDoc = new Document();

wordDoc = wordApp.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

foreach (Field myMergeField in wordDoc.Fields)
{


Range rngFieldCode = myMergeField.Code;

String fieldText = rngFieldCode.Text;



// ONLY GETTING THE MAILMERGE FIELDS

if (fieldText.StartsWith(" MERGEFIELD"))
{

// THE TEXT COMES IN THE FORMAT OF

// MERGEFIELD MyFieldName \\* MERGEFORMAT

// THIS HAS TO BE EDITED TO GET ONLY THE FIELDNAME "MyFieldName"

Int32 endMerge = fieldText.IndexOf("\\");

Int32 fieldNameLength = fieldText.Length - endMerge;

String fieldName = fieldText.Substring(11, endMerge - 11);

// GIVES THE FIELDNAMES AS THE USER HAD ENTERED IN .dot FILE

fieldName = fieldName.Trim();

// **** FIELD REPLACEMENT IMPLEMENTATION GOES HERE ****//

// THE PROGRAMMER CAN HAVE HIS OWN IMPLEMENTATIONS HERE

if (fieldName == "Name")
{

myMergeField.Select();

wordApp.Selection.TypeText("Vibhusha");

}

}

}
wordDoc.SaveAs("myfile.doc");
wordApp.Documents.Open("myFile.doc");
wordApp.Application.Quit();



--> i have to do this but on bellow code i get some error,

Application wordApp = new Application();--->System.web.ui.page.application is a 'Property' but is used like a 'type'


Document wordDoc = new Document();--->The type or namespace name 'Document' could not be found(are you missing a using directive or an assembly reference?

推荐答案

我认为您收到此错误因为你没有引用任何Microsoft Word对象库。
I think you are getting this error because you have not referenced any "Microsoft Word Object library".
This will automatically add VBIDE, Microsoft.Office.Core along with Microsoft.Office.Interop.Word as references.





之后,你需要使用



After that, you need to use the required namespace like

using Word = Microsoft.Office.Interop.Word;





如需更多参考,请浏览b elow link:



从您的应用程序编辑Microsoft Word文档 [ ^ ]



这可能对您有帮助。



For more reference, go through the below link:

Edit Microsoft Word Document From Your Application[^]

This may help you.


wordApp is不是Word,只是应用程序,请参阅MSDN页面,了解如何使用Word和Excel(Word部分位于底部)。快速阅读你永远不会使用Document,这不是Word文档。



如何:使用Visual C#功能访问Office互操作对象(C#编程指南) [ ^ ]
wordApp is not Word, just application, see the MSDN pages on how to use Word and Excel (the Word part is at the bottom). Also reading quickly you never use Document, which is not a Word document anyway.

How to: Access Office Interop Objects by Using Visual C# Features (C# Programming Guide)[^]


这篇关于我怎么能在C#上的数据表上创建单词的字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆