读取具有相同样式的doc,docx文件 [英] Read doc,docx files with the same style

查看:98
本文介绍了读取具有相同样式的doc,docx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码读取doc,docx文件并在richtextbox中显示它们,但是这种方式无法显示具有相同样式的文件.例如,如果文本为红色,那么richtextbox将其显示为黑色
我该怎么办?

我希望

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;

推荐答案

与它一起生活.我怀疑是否有任何方法可以改变Word给您的内容.除非它以更可靠的方式为您提供HTML,并且您找到其他可以从那里转换它的东西?
Live with it, I expect. I doubt there''s any way to change what Word gives you. Unless it gives you HTML in a more reliable way and you find something else to convert it from there ?


您无能为力. RichTextBox不是Word,也不了解有关Word如何设置文档格式的一切.您没有将格式化的文本复制并粘贴到剪贴板,而是复制了Word Range对象.可以像RTB当前所做的那样提取文本,但是格式化无能为力.
There''s nothing you can do. The RichTextBox is NOT Word and doesn''t know everything about how Word formats documents. You didn''t copy and paste formatted text to the clipboard, you copied a Word Range object. The text can be extracted, as the RTB is currently doing, but there is nothing you can do about the formatting.


这篇关于读取具有相同样式的doc,docx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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