WebBrowse 保存数据库word、

Hi,
This is an extended question to http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_23183926.html.
I have added a memorystream to a webbrowser:


        public WebBrowser createEmbeddedWord(Point point, MemoryStream memoryStream)
        {
            webBrowser = new WebBrowser();
            webBrowser.Location = point;
            Size size = new Size(new Point(200,200));
            webBrowser.Size = size;
           
            webBrowser.DocumentStream = memoryStream;
         
            return webBrowser;
                       
        }

And when I run the program it displays the text in the webbrowser control, but you can't edit the text and it doesn't give the embedded word (word opened in the webbrowser) as if I did : //webBrowser.Navigate(@"C:\Test\MainTest1.doc");
To test it I have written a small function which takes a document and inserts it into a byte[] to simulate the binary field on the server. Do I loose some formatting when I convert the document to byte[] since it doesn't open as a word document?

public class BLDocument
{
public byte[] OpenWord()
{
object missing = System.Reflection.Missing.Value;
bool Visible = false;
ApplicationClass WordApp = new ApplicationClass();
WordApp.Visible = Visible;
object filename = @"C:\MainTest1.doc";
Document adoc = WordApp.Documents.Open(ref filename, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
adoc.Activate();
adoc.ActiveWindow.Selection.WholeStory();
adoc.ActiveWindow.Selection.Copy();
IDataObject temp = Clipboard.GetDataObject();
string dataString = Convert.ToString(temp.GetData(DataFormats.UnicodeText));
UTF8Encoding enc8 = new UTF8Encoding();
byte[] dataArray = enc8.GetBytes(dataString);
//MemoryStream memorystream = new MemoryStream(dataArray);
return dataArray;
}
}
、、
using System;
            using System.Configuration;
            using System.Data;
            using System.Data.SqlClient;
            using System.Windows.Forms;
            using System.IO;
            public class WordDocumentProcessor
            {
            private WebBrowser _webBrowser;
            public WordDocumentProcessor(WebBrowser browser)
            {
            if (browser == null)
            throw new NullReferenceException("Browser cannot be null");
            _webBrowser = browser;
            }
            public void ShowInBrowser(int documentId)
            {
            byte[] buffer = this.GetDocument(documentId);
            _webBrowser.DocumentStream.Write(buffer, 0, buffer.Length);
            }
            private byte[] GetDocument(int documentId)
            {
            string connectionString = ConfigurationManager.ConnectionStrings["MyDocuments"].ConnectionString;
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
            connection.Open();
            using (SqlCommand command = new SqlCommand("spGetDocument"))
            {
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@DocumentID", documentId);
            return (byte[])command.ExecuteScalar();
            }
            }
            }
            }
            
Open in New Window Select All

转载于:https://www.cnblogs.com/lgzh3/archive/2008/12/08/1350708.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值