前一段时间在搞.net的一个项目,其中要求输出word和Excel,在网上找了找,常用的一些操作都有,不过我们用到了一些不常用的,这里一起总结总结。这里有两个自己认为比较好的小方法:1.这里如果项目中的输出的文档的形式是相对固定的,用模板文件去创建要输出的文档是个好的选择,这样输出的格式和速度都好于用程序动态输出。2.如果有些输出要求找不到可利用的有效代码,不防在office中录制个宏,参照VB的代码去写。
C# 进行Word的操作
C# 操作Word时需要添加的引用
using Microsoft.Office.Interop.Word;
Microsoft.Office.Interop.Word.Application wordApp = null;
Microsoft.Office.Interop.Word.Document wordDoc = null;
打开Word文档
public static bool CreateWordDoc(stringfileName)
{
filePath = fileName;
if(wordApp == null)
{
wordApp = new Microsoft.Office.Interop.Word.Application();
}
objectpath = templateFileName;
objectmissing = System.Reflection.Missing.Value;
objectreadOnly = false;
try
{
wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
wordDoc =wordApp.Documents.Open(ref path, ref missing, refreadOnly, ref missing, refmissing, ref missing, refmissing, ref missing, refmissing, ref missing, refmissing, ref missing, refmissing, ref missing, refmissing, r