从数据库中检索Word文档到richtextbox [英] retrieve word document from database into richtextbox

查看:118
本文介绍了从数据库中检索Word文档到richtextbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从SQL Server数据库richtextbox中检索word和excel文档,或者通过另一种方式使用c#.net查看表单中的文档内容


我使用了此代码

I want to retrieve word and excel documents from sql server database it richtextbox or by another way to view document contents in form using c#.net


I used this code

 con.Open();
            SqlCommand cmd1 = new SqlCommand("select Document from documents where [Account no]='" + textBox23.Text + "'", con);

            SqlDataAdapter dpp = new SqlDataAdapter(cmd1);
            DataSet dss = new DataSet("Mydocs");
            byte[] MyData = new byte[0];
            dpp.Fill(dss, "Mydocs");
            DataRow myRow;
            myRow = dss.Tables["Mydocs"].Rows[0];
            MyData = (byte[])myRow["Document"];
           MemoryStream stream = new MemoryStream(MyData);

con.close();




注意:文档是二进制保存的文档


我不知道如何从数据库在Richtextbox中显示文档的问题,我无法完成此代码以在Richtextbox中显示文档

有人可以帮我吗?

谢谢您.




note: Document is the saved document in binary


the problem I don''t know how to display document in richtextbox from database , I cannot complete this code to display the document in the richtextbox

can any body help me ??

thank you

推荐答案

据我所知,Word文档与希望使用RTF格式的RichTextBox不兼容,请参见:

http://msdn.microsoft.com/en-us/library/system. windows.forms.richtextbox.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.controls.richtextbox.aspx [ ^ ],
http://en.wikipedia.org/wiki/Windows_Word#Binary_formats_.28Word_97.E2. 80.932003.29 [ ^ ],
http://en.wikipedia.org/wiki/Rich_Text_Format [
To best of my knowledge, word document is not compatible with RichTextBox which expects Rich Text Format, see:

http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx[^],
http://en.wikipedia.org/wiki/Windows_Word#Binary_formats_.28Word_97.E2.80.932003.29[^],
http://en.wikipedia.org/wiki/Rich_Text_Format[^].

—SA


moon_shine写道:
moon_shine wrote:

那么,您能帮我怎样以任何方式从数据库中打开保存的Word文档(以二进制形式),而不必在Windows窗体中打开.

So, can you help me how open saved word document(as binary) from database with any way, not necessary open in windows forms.



不幸的是,答案将是非常不愉快的:下一次,在以专有格式保存数据之前,请三思而后行".真的.让自己陷入死胡同太容易了,走出去可能也太困难了,即使可能的话.

因此,解决方案是将Microsoft Word文档的专有格式重写为一种公共格式.首批候选者将是HTML和RTF. RTF只能呈现Word文档功能的一小部分子集,但是HTML更接近于全面的解决方案,但仍不能100%保证.如果仅在HTML5中完全支持MathML,那么如何使用公式编辑器.两种格式都很容易在您的应用程序中显示文档.

更高级的格式可能包括基于XML的OpenDocument(ODF),
http://en.wikipedia.org、/wiki/OpenDocument [ ^ ],不要与Microsoft Office Open XML混淆.

当然,您可以访问Word文档格式,但是我建议您不要立即执行此操作,即使许多产品都可以这样做.你有策略上的错误;所以我建议先修复它.您可以自动转换数据库中的所有文档.编写一个特殊的客户端应用程序来进行修复.但是,您可以即时转换文档,但是却冒着使自己陷入另一个死胡同的风险:您可能需要在服务器上保留办公室,其版本可能过时,等等,诸如此类的许多问题.最好不要同时使用Word和所有文档.

现在,问题被简化为一个问题:如何读取/解析或转换Word文档?这个问题是可以解决的.

我至少可以看到两种方式:使用Office Automation或使用Open Office的开源代码.

Open Office可以将任何上述格式转换为HTML,RTF和ODF(其本机格式).因此,您肯定可以在其源代码中找到转换器.参见:
http://en.wikipedia.org/wiki/Open_Office [ http://www.openoffice.org/ [ ^ ].

对于Office Automation,请从 http://msdn.microsoft.com/开始zh-cn/library/aa188489%28v = office.10%29.aspx [您将使用的专业HTML渲染器 [



Unfortunately, the answer would be pretty unpleasant: "Next time, think twice before saving data in proprietary formats". Really. It''s way too easy to put yourself in a dead end, going out could be way too difficult, if even possible.

So, the solution would be re-writing the Microsoft Word documents proprietary formats into one of public formats. The first candidates would be HTML and RTF. RTF can render only a narrow sub-set of Word document features, but HTML is much closer to a comprehensive solution while still not a 100% guarantee. How about formula editor, if MathML is only fully supported in HTML5. Both formats are easy enough to show the documents in your application.

More advanced formats may include XML-based OpenDocument (ODF), http://en.wikipedia.org,/wiki/OpenDocument[^], not to be confused with Microsoft Office Open XML.

Of course you can access Word Document formats, but I would suggest you don''t do it on the fly, even though many products do it. You have a strategic mistake; so I would advice to fix it first. You can convert all document in the database automatically. Write a special client application to do the fix. However, you can convert the document on the fly, but then you take a risk of putting yourself in another dead end: you may need to keep office on the server, its version can become obsolete, etc., a number of problems like that. Better go away from Word, all documents at once.

Now, the problem is reduced to the one: how to read/parse or convert a Word document? This problem is quite solvable.

I can see at least two ways: using Office Automation or using the Open-Source code of Open Office.

Open Office can convert any of the mentioned formats into HTML, RTF and ODF which is its native format. So, in its source code you will certainly be able to find the converters. See:
http://en.wikipedia.org/wiki/Open_Office[^],
http://www.openoffice.org/[^].

For Office Automation, start with http://msdn.microsoft.com/en-us/library/aa188489%28v=office.10%29.aspx[^].

More or less adequate conversion to HTML can be done only by the Word 2007 or later.

Showing RTF or HTML in your application is easy. Use read-only RichTextBox in first case or WebBrowser control in second. Also, there is a very elegant solution — HTML renderer; see this CodeProject article: A Professional HTML Renderer You Will Use[^].

—SA


这篇关于从数据库中检索Word文档到richtextbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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