您应该能够使用 Express 版本来完成此操作。我改编了一个答案这个问题 https://stackoverflow.com/questions/607669/how-do-i-convert-word-files-to-pdf-programmatically。改编后的代码如下。您需要添加对 Microsoft.Office.Interop.Word 的引用才能使其正常工作。如果你缺少这个库,请看看MSDN 上的这篇文章 http://msdn.microsoft.com/en-us/library/15s06t57(v=vs.100).aspx.
看着保存格式 http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.wdsaveformat.aspx您还可以将其另存为格式过滤 HTML (wdFormatFilteredHTML)。
namespace Sample {
using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
class Program {
public static void Main()
{
Convert("C:\\Documents", WdSaveFormat.wdFormatHTML);
}
private static void Convert(string path, WdSaveFormat format)
{
DirectoryInfo dirInfo = new DirectoryInfo(path);
FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");
if (wordFiles.Length == 0) {
return;
}
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
try {
word.Visible = false;
word.ScreenUpdating = false;
foreach (FileInfo wordFile in wordFiles) {
Object filename = (Object)wordFile.FullName;
Document doc = word.Documents.Open(ref filename, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
try {
doc.Activate();
object outputFileName = wordFile.FullName.Replace(".doc", ".html");
object fileFormat = format;
doc.SaveAs(ref outputFileName,
ref fileFormat, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
}
finally {
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
doc = null;
}
}
}
finally {
((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;
}
}
}
}