namespace WordToXml
{
class Program
{
public static void Main(string[] args)
{
IEnumerable<string> temp = Directory.EnumerateFiles(@"", "*.doc?");
foreach (string path in temp)
{
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
string inputFile = fileStream.Name;
string fileName = System.IO.Path.GetFileNameWithoutExtension(inputFile);
string outputFile = @"" + fileName + ".xml";
string outpuFileTxt = @"*.txt";
if (Directory.Exists(outputFile) == true)
{
File.Delete(outputFile);
}
if (Directory.Exists(outputFile) == false)
{
Directory.CreateDirectory(@"");
if (!File.Exists(outpuFileTxt))
{
using (FileStream fs = new FileStream(outpuFileTxt, FileMode.Create, FileAccess.Write))
{
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
sw.WriteLine();
}
}
String fileType = System.IO.Path.GetExtension(inputFile);
try
{
WordToXml(inputFile, outputFile);
}
catch (Exception e)
{
File.AppendAllText(outpuFileTxt, fileName + e.Message + "\r\n");
continue;
}
}
}
}
public static void WordToXml(string strFileName, string strSaveFileName)
{
Microsoft.Office.Interop.Word.Document WordDoc;
Object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word._Application WordApp = new Application();
object fileName = strFileName;
WordDoc = WordApp.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);
Type wordType = WordApp.GetType();
// 打开文件
Type docsType = WordApp.Documents.GetType();
// 转换格式,另存为
Type docType = WordDoc.GetType();
object saveFileName = strSaveFileName;
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, WordDoc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXML });
#region 其它格式:
///wdFormatHTML
///wdFormatDocument
///wdFormatDOSText
///wdFormatDOSTextLineBreaks
///wdFormatEncodedText
///wdFormatRTF
///wdFormatTemplate
///wdFormatText
///wdFormatTextLineBreaks
///wdFormatUnicodeText
//-----------------------------------------------------------------------------------
#endregion
WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
}
}
}
http://www.cnblogs.com/Tiffany_2008/archive/2010/12/22/1914160.html