日期:2014-05-17  浏览次数:21021 次

C#生成word文档(部分文字内容动态赋值)
用C#把一些数据写到word中,生成一个报告,报告中多数内容是已知的只是某些地方需要从数据库中获取,请各位帮忙!
C# word 动态

------解决方案--------------------
给你一个思路。

1. 把你的文档先用Word写出来,把需要修改的部分用特定的格式的字符代替(比如#DataNo#,保存为一个word模板(*.dot)。
2. 使用VSOT来进行Office Word文档操作,打开这个*.dot文件,然后再查找#DataNo#并替换成真实的数据,然后保存为*doc即可。 

代码大致如下:

Word.Application docApp = new Word.Application();
docApp.Visible = true;
docApp.Activate();

string templatePath = AppDomain.CurrentDomain.BaseDirectory + "checkTemplate.dot";

Word.Document doc = docApp.Documents.Add(templatePath, ref oMissing, ref oMissing, ref oMissing);

StringBuilder sb = new StringBuilder(100);
sb.Append(' ', minLen);
string tag = "#DataNo#";
string str = "真实的数据";
object replaceAll = Word.WdReplace.wdReplaceAll;
object missing = Type.Missing;

//首先清除任何现有的格式设置选项,然后设置搜索字符串 strOldText。
docApp.Selection.Find.ClearFormatting();
docApp.Selection.Find.Text = tag;

docApp.Selection.Find.Replacement.ClearFormatting();
docApp.Selection.Find.Replacement.Text = str;

//执行替换操作
docApp.Selection.Find.Execute(
    ref missing, ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing, ref missing,
    ref replaceAll, ref missing, ref missing, ref missing, ref missing);



------解决方案--------------------
C# 引用 office word  然后 先在word中预定的位置加入书签,c# 往书签写数据就行了
------解决方案--------------------
apose.word 这个是封装好的操作word 的第三方控件 你可以下个破解版的 
------解决方案--------------------
我做证书都是这样弄的 
------解决方案--------------------
这个我刚做过,我不建议你用word自动机

先在word你需要加入资料的地方插入书签

然后用aspose.word 这个控件,把对应的书签替换成你要的内容

例如:
http://www.cnblogs.com/wuhuacong/archive/2012/08/30/2662961.html


------解决方案--------------------
简单的那就是你可以先把要导出的word在复制到一个新页面上布好局,你可以利用js导出所用的区域到word中,这个js很简单,你可以查下,实际上它不是导出它是导出到word软件打开,这样打开后就保存下就可以了,另一种要是你页面不太复杂的话那你就用标签先定义,然后读到文件流中替换标签,然后导出流就行了
------解决方案--------------------