读取相同风格的dox、docx文件
我使用下面的代码读取 doc、docx 文件并在 richtextbox 中显示它们,但这种方式无法显示具有相同样式的文件。例如,如果文本为红色,richtextbox 将显示为黑色 我应该怎么办 ?
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.ApplicationClass();
object nullobj = System.Reflection.Missing.Value;
object file = openFileDialog1.FileName;
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(ref file, 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, ref nullobj, ref nullobj);
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
string text = data.GetData(DataFormats.Text).ToString();
Console.WriteLine(text);
doc.Close(ref nullobj, ref nullobj, ref nullobj);
app.Quit(ref nullobj, ref nullobj, ref nullobj);
richTextBox1.Text = text;
I use below code for read doc,docx files and show them in richtextbox but this way can't show files with the same style.for example if text is with red color richtextbox show it with black color
what should i do ?
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.ApplicationClass();
object nullobj = System.Reflection.Missing.Value;
object file = openFileDialog1.FileName;
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(ref file, 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, ref nullobj, ref nullobj);
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
string text = data.GetData(DataFormats.Text).ToString();
Console.WriteLine(text);
doc.Close(ref nullobj, ref nullobj, ref nullobj);
app.Quit(ref nullobj, ref nullobj, ref nullobj);
richTextBox1.Text = text;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为在这种情况下你运气不好。
Richtextbox 远不如 Word 强大。如果是这样,每个人都会复制 Word,微软就会赔钱。
您可能会使其部分工作(就像您所做的那样)。但永远不够好。
I think you are out of luck in this case.
Richtextbox is nowhere near as powerful as Word. If it were, everybody would make copies of Word and MS would loose money.
You may get it working partially (like you did). But never good enough.
我建议你看看 Telerik's RadRichTextBox 之类的东西,它可以正确读取处理 .docx (我认为是 .doc)文件。否则,您将必须将数据转换为 RichTextBox 控件可以理解的内容(其中您将失去很多功能)。
I would suggest you look at something like Telerik's RadRichTextBox which can read properly handle .docx (and I think .doc) files. Otherwise you are going to have to convert the data to something the RichTextBox control can understand (in which you'll lose a lot of features).