Export ASP.Net GridView with image to Word Error: The picture can't be displayed

Last Reply 11 months ago By dharmendr

Posted 11 months ago

Hi. I have a logo/image in the doc file which gives error message "The picture can't be displayed". The Doc file was exported using itextsharp in Asp.net Webforms. I was able to get the complete file path of the image. I saw one of the post (https://www.aspsnippets.com/Articles/Export-GridView-with-Images-to-Word-Excel-and-PDF-Formats-in-ASP.Net.aspx) in which the problem was due to the complete path, I am getting complete path.

I am using below code to fetch the data from Sql Server

MemoryStream memoryStream = new MemoryStream();
byte[] file = (byte[])list[0].CompanyLogo;

memoryStream.Write(file, 0, file.Length);
context.Response.Buffer = true;
context.Response.BinaryWrite(file);
memoryStream.Dispose();

It's showing the image perfectly in the report and other areas of the application but giving error message while exporting to doc file.

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                int companyId = int.Parse(context.Request.QueryString["id"]); //get the querystring value that was pass on the ImageURL (see GridView MarkUp in Page1.aspx)
 
                if (companyId != null)
                {
 
                    CompanyDefaultsBLL srv = new ApplicationBLL.CompanyDefaultsBLL();
                    var list = srv.GetCompanyDefaultsByCompanyID(Convert.ToInt16(companyId));
 
                    if (!DBNull.Value.Equals(list[0].CompanyLogo))
                    {
                        MemoryStream memoryStream = new MemoryStream();
                        byte[] file = (byte[])list[0].CompanyLogo;
 
                        memoryStream.Write(file, 0, file.Length);
                        context.Response.Buffer = true;
                        context.Response.BinaryWrite(file);
                        memoryStream.Dispose();
                    }
                }
            }
            catch
            {
            }
        }

Called the above file from a different file

 url = appPath + "Handler1.ashx?id=" + companyID;

 Here i am getting the complete file path.

Aspx file is simple which has been shown below:

    <form id="form1" runat="server">
    <center>
        <input type="button" id="btnPrint" onclick="printButton();" value="Print" style="color: White;
            background-color: Black; text-align: center; float: inherit" />
    </center>   
    <asp:PlaceHolder ID="PlaceHolder1" runat="server" />
    </form>

Used this code while exporting to doc

Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=ExportToDoc.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-word ";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
//PlaceHolder1.AllowPaging = false;
PlaceHolder1.DataBind();
PlaceHolder1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();

May be i am missing something while exporting to doc file that's why the image is not showing up and giving error message.

Let me know if you want any other thing as pasting the complete code here is difficult due to the length of the code.

Posted 11 months ago

Use the below code to save the file in server folder.

Response.ClearContent();
Response.Buffer = true;
Response.ContentType = "application/ms-word";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
string strPath = Server.MapPath("~/Files/Test.doc");
StreamWriter sWriter = new StreamWriter(strPath);
sWriter.Write(sw.ToString());
sWriter.Close();