public string WordReader(string filename)
{
filename = DocPath+filename;
Word.ApplicationClass wordapp = null;
Word.Document worddoc = null;
object fileobj = filename;
object nullobj = System.Reflection.Missing.Value;
object Readonly = true;
object noSaveChange = false;
string doc = "";
try
{
wordapp = new Word.ApplicationClass();
worddoc = wordapp.Documents.Open(ref fileobj, ref nullobj, ref Readonly, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
doc = worddoc.Content.Text;
doc.Replace("/a", ""); //替换空串为空。(word中/a代表空串,但在C#中,代表响铃 晕~~)否则显示控制台程序时会响
doc.Replace("/r", "/n"); //替换回车为回车换行
}
catch(Exception ex)
{
throw ex;
}
finally
{
if (worddoc != null)
{
try
{
worddoc.Close(ref nullobj, ref nullobj, ref nullobj);
}
catch
{ }
}
if (wordapp != null)
{
try
{
wordapp.Quit(ref noSaveChange, ref nullobj, ref nullobj);
}
catch
{
}
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordapp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(worddoc);
worddoc = null;
wordapp = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
return doc;
}