我承认代码是抄的。
因为实在没有几行代码可写。
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.”);
}
}
}
}
{
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