用C# 把word文档转换为txt

我承认代码是抄的。

因为实在没有几行代码可写。

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace doc2txt
{
class Program
{
static void Main(string[] args)
{
System.IO.FileInfo fi = new System.IO.FileInfo(args[0]);
if (fi.Exists)
{
try
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
app.Visible = false;
string strFile = fi.FullName;
object obj = System.Reflection.Missing.Value;
Object infile = (object)strFile;
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(ref infile, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj);
string strnew = fi.Directory.FullName + “\\” + fi.Name.Substring(0, fi.Name.Length – fi.Extension.Length) + “.txt”;
Object outfile = (object)strnew;
Object fmt = (object)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText;
doc.SaveAs(ref outfile, ref fmt, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj);
doc.Close(ref obj, ref obj, ref obj);
app.Quit();
app = null;
System.Console.WriteLine(“file: ” + fi.Name + ” converted.”);
}
catch
{
System.Console.WriteLine(“file: ” + fi.Name + ” has error.”);
}
}
else
{
System.Console.WriteLine(“file: ” + fi.Name + ” is not exist.”);
}
}
}
}
需要使用office组件microsoft word 12 object library

关于xmsg

技术面前人人平等.同时技术也不分高低贵贱.正所谓学无大小,达者为尊.
此条目发表在C#, Windows分类目录,贴了, 标签。将固定链接加入收藏夹。