#region 读取word
/// <summary>
/// 读取word所有文字内容(不包含表格)
/// </summary>
/// <returns>word中的字符内容(纯文本)</returns>
public
string
ReadAllFromWord()
{
Word.ApplicationClass app =
null
;
Word.Document doc =
null
;
object
missing = System.Reflection.Missing.Value;
object
FileName = m_FilePath;
object
readOnly =
true
;
object
isVisible =
false
;
try
{
app =
new
Word.ApplicationClass();
doc = app.Documents.Open(
ref
FileName,
ref
missing,
ref
readOnly,
ref
missing,
ref
missing,
ref
missing,
ref
missing,
ref
missing,
ref
missing,
ref
missing,
ref
missing,
ref
isVisible,
ref
missing,
ref
missing,
ref
missing,
ref
missing);
string
textString =
""
;
textString = doc.Content.Text.Trim();
textString = textString.Replace(
"/a"
,
""
);
textString = textString.Replace(
"/r"
,
"/n"
);
return
textString;
}
catch
(Exception ex)
{
throw
ex;
}
finally
{
if
(doc !=
null
)
{
try
{
doc.Close(
ref
missing,
ref
missing,
ref
missing);
}
catch
{}
doc =
null
;
}
if
(app !=
null
)
{
try
{
app.Quit(
ref
missing,
ref
missing,
ref
missing);
}
catch
{}
app =
null
;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
#endregion
#region 追加写入word /// <summary>
/// 追加写入word
/// </summary>
/// <param name="InsertText">需要写入的字符串</param>
public
void
WriteToWord(
string
InsertText)
{
Word.ApplicationClass app =
null
;
Word.Document doc =
null
;
object
missing = System.Reflection.Missing.Value;
object
FileName = m_FilePath;
object
readOnly =
false
;
object
isVisible =
false
;
try
{
app =
new
Word.ApplicationClass();
doc = app.Documents.Open(
ref
FileName,
ref
missing,
ref
readOnly,
ref
missing,
ref
missing,
ref
missing,
ref
missing,
ref
missing,
ref
missing,
ref
missing,
ref
missing,
ref
isVisible,
ref
missing,
ref
missing,
ref
missing,
ref
missing);
doc.Activate();
doc.Paragraphs.Last.Range.Text = InsertText +
"/n"
;
doc.Save();
}
catch
(Exception ex)
{
throw
ex;
}
finally
{
if
(doc !=
null
)
{
try
{
doc.Close(
ref
missing,
ref
missing,
ref
missing);
}
catch
{}
doc =
null
;
}
if
(app !=
null
)
{
try
{
app.Quit(
ref
missing,
ref
missing,
ref
missing);
}
catch
{}
app =
null
;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
#endregion