从SQL Server将图像插入Word Doc文件

时间:2014-11-22 13:57:47

标签: javascript c# jquery asp.net vb.net

我想导出网页上显示的图片。该图像存储在Sql图像数据类型中。在显示图像时,我将图像转换为Base64String。

Image1.ImageUrl = "data:image/jpg;base64," & Convert.ToBase64String(imgPhoto)    

导出页面时,图像未显示在word文档文件中。

由于安全原因,我无法在服务器上存储图像。 下面的代码用于导出到word文件。

    Response.AddHeader("content-disposition", "attachment;filename=Test.doc")
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.ContentType = "application/ms-word "
    Response.Charset = ""
    Dim stringWrite As New System.IO.StringWriter()
    Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)

    ' Create a form to contain the grid
    ControlID.RenderControl(htmlWrite)

    Dim stringWrite1 As New System.IO.StringWriter()
    Dim htmlWrite1 As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite1)

    ' Create a form to contain the grid
    ImageLogo.RenderControl(htmlWrite1)

    Response.Write(stringWrite1.ToString() & stringWrite.ToString())
    Response.[End]()    

1 个答案:

答案 0 :(得分:0)

您确实没有创建Word文档,只是将HTML呈现为单词MIME类型。我不相信word支持数据:图像定义,因为它比2007年之前的单词格式更新。相反,尝试使用OpenXML SDK。虽然它只创建Word 2007+文档,但大多数人都可以查看它们。 OpenXML SDK将使您能够创建真实的单词doc并从二进制图像数据创建图像。

http://www.microsoft.com/en-us/download/details.aspx?id=30425

http://msdn.microsoft.com/en-us/library/office/bb448854%28v=office.15%29.aspx