使用visual studio 2008的互操作词库从word中读取特殊字符,如ρ,λ
本文关键字:特殊字符 读取 word 2008 studio visual 互操作 使用 | 更新日期: 2023-09-27 18:17:03
我正在使用visual studio的Microsoft.Office.Interop.Word.Document
库阅读word文件。问题是文件包含特殊字符,如ρ,λ。当我在c#中阅读时,它们被转换为?问号。
例如,我正在读这样一行A child drinks a liquid of density ρ through a vertical straw.
这条线变成了A child drinks a liquid of density ? through a vertical straw.
所以请告诉我它们是如何保存在原形的。
下面是代码
public void ReadMsWord()
{
// variable to store file path
string filePath = null;
// open dialog box to select file
OpenFileDialog file = new OpenFileDialog();
// dilog box title name
file.Title = "Word File";
// set initial directory of computer system
file.InitialDirectory = "c:''";
// set restore directory
file.RestoreDirectory = true;
// execute if block when dialog result box click ok button
if (file.ShowDialog() == DialogResult.OK)
{
// store selected file path
filePath = file.FileName.ToString();
}
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.ApplicationClass();
// create object of missing value
object miss = System.Reflection.Missing.Value;
// create object of selected file path
object path = filePath;
// set file path mode
object readOnly = false;
// open document
Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref
miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
try
{
// create word application
// select whole data from active window document
docs.ActiveWindow.Selection.WholeStory();
// handover the data to cllipboard
docs.ActiveWindow.Selection.Copy();
// clipboard create reference of idataobject interface which transfer the
data
IDataObject data = Clipboard.GetDataObject();
//set data into richtextbox control in text format
string t = "";
string[] y = {};
t = data.GetData(DataFormats.Text).ToString();
string[] options = { };
y = t.Split(''n');
}
catch(Exception ex)
{
throw ex;
}
}
使用
t = data.GetData(DataFormats.UnicodeText).ToString();
。UnicodeText
代替Text
。请注意,特殊字符仍然会在控制台窗口中显示为?
,但它们在例如MessageBox中会正确显示。显示调试器