0
我正在使用C#和Microsoft Word 12.0對象庫從.doc文件讀取數據,然後將這些內容保存到文本文件(這是我的項目需要)。我的.doc文件有一些表格,我需要讀取這些表格中的每一行和每列。 讀取操作成功執行,但該數據包含一些奇怪的字符(如方形的)作爲附加的圖像刪除奇怪的字符?
在這裏,是我使用的代碼:
private void btnRead_Click(object sender, EventArgs e)
{
try
{
Microsoft.Office.Interop.Word.ApplicationClass wordObject = new ApplicationClass();
object file = textBox1.Text; //this is the path
object nullobject = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Document docs = wordObject.Documents.Open
(ref file, ref nullobject, ref nullobject, ref nullobject,
ref nullobject, ref nullobject, ref nullobject, ref nullobject,
ref nullobject, ref nullobject, ref nullobject, ref nullobject,
ref nullobject, ref nullobject, ref nullobject, ref nullobject);
docs.ActiveWindow.Selection.WholeStory();
docs.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
String allData = "";
for (int t = 1; t < docs.Tables.Count; t++)
{
Table tbl = docs.Tables[t];
for (int r = 1; r < tbl.Rows.Count; r++)
{
for (int c = 1; c < 3; c++)
{
allData += tbl.Cell(r, c).Range.FormattedText.Text.Trim() + Environment.NewLine;
}
}
}
txtData.Text = allData;
saveTextFile(allData);
docs.Close(ref nullobject, ref nullobject, ref nullobject);
}
catch (Exception j)
{
MessageBox.Show(j.Message);
}
}
private void saveTextFile(String data)
{
try
{
StreamWriter sw = new StreamWriter(txtOutput.Text.Trim());
sw.WriteLine(data);
sw.Flush();
sw.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace);
}
}
有誰有什麼想法,我該如何刪除這些奇怪的字符?