使用C#读取符号时出现问题 [英] Problem in reading a symbols using C#

查看:97
本文介绍了使用C#读取符号时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#读取集成符号和其他一些符号。我有一个.doc文件。我读的是∫,Σ。我没有在C#变量中拥有它。我正在开发化学问题论文。那我该怎么办?我将不得不将此值保存到sql server数据库中。代码低于 -



I am trying to read a integration symbols and some other using C#. I have a .doc file . i am reading like ∫,∑. I don''t have it in C# variable. I am developing a Question Paper Generation of Chemistry.Then what do i do? i will have to save this value into sql server database also.Code is below-

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:\Users\shri\Desktop\EXTRA\question\TnA\demo_swarit.doc";
   // 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();
   }

   try
   {
      // create word application
     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);

     // 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
     richTextBox1.Text = data.GetData(DataFormats.Text).ToString();

     // read bitmap image from clipboard with help of iddataobject interface
     Image img = (Image)data.GetData(DataFormats.Bitmap);

     // close the document 
     docs.Close(ref miss, ref miss, ref miss);

    }
    catch (Exception ex) { MessageBox.Show(ex.ToString()); }
}

推荐答案

您好,



您需要对输入字符进行编码以正确读取。你可以把它转换成UTF-8来得到它应该是。



你可以使用,

Hi,

You need to encode your input character to read it correctly. you can convert it into UTF-8 to get it as it should be.

you can use,
var result = Encoding.UTF8.GetString("your byte array")



希望这对你有用。


hope this may work for you.


你可以得到一个Unicode版本的文本使用:

You can get an Unicode version of text using :
data.GetData(DataFormats.UnicodeText).ToString()



但是如果你使用不同的话您的Word文档中的字体(如符号)您无法获得正确的字符串。



可能,在这种情况下,您可以尝试使用DataFormats获取Rtf数据。 RTF格式。我认为将Rtf数据放入RichTextBox应该很容易。


But if you used different font in your Word document (like Symbols) you cannot get the right string.

May be, in this case, you can try to get the Rtf data using DataFormats.Rtf. And I think it should be easy to put Rtf data into the RichTextBox.


这篇关于使用C#读取符号时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆