Export page in Excel with logo

I need to export some data from an asp.net page in order to succeed, so I mainly use the table to create the custom headers I need, and then the GridView. All data is displayed correctly when exporting to excel, but when I added the logo image in html, it does not appear in the Excel file when exporting.

Since I need to export it to Excel 2007 or later, I know that I can use the Open XML material from Microsoft to export data, the problem is that I already have everything that is done for the code, and I would like to know this another way to do this, and not do it all over again with Open XML.

If there is no way to do this without using Open XML, can someone show me how I can export data to it? I tried once, but I did not have much success. = /

By the way, I am using C #.

Thanks in advance!

I updated the code and now it looks like this, but I still do not see the image ... = /

    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    frmPlanoAcao.RenderControl(htmlWrite);

    StringWriter w = new StringWriter();
    HtmlTextWriter t = new HtmlTextWriter(w);
    imgLogo.RenderControl(t);
    var byte_array = System.Text.Encoding.ASCII.GetBytes(w.ToString());
    Response.Write(stringWrite.ToString());

    this.EnableViewState = false;

    Response.Clear();
    Response.Buffer = false;
    Response.Charset = "ISO-8859-1";
    Response.ContentEncoding = System.Text.Encoding.GetEncoding(1252);
    Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", "plano_acao.xls"));
    Response.ContentType = "application/vnd.ms-excel";
    Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
    Response.Write(getExcelStyling());
    Response.OutputStream.Write(byte_array, 0, byte_array.Length);
    Response.Write(stringWrite.ToString());
    Response.Write("</body>");
    Response.Write("</html>");
    Response.End();
+3
source share
3 answers

Try using the following code. I tested in local IISand it works fine. Including an image similar Header Image/Logoon top of the grid data.

Response.ContentType = "application/vnd.ms-excel";        
Response.AddHeader("Content-Disposition", "attachment; filename=test.xls;");                
StringWriter stringWrite = new StringWriter();        
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);        
dgrExport.DataSource = dtExport;        
dgrExport.DataBind();
dgrExport.RenderControl(htmlWrite);
string headerTable = @"<Table><tr><td><img src=""D:\\Folder\\1.jpg"" \></td></tr></Table>";
Response.Write(headerTable);
Response.Write(stringWrite.ToString());        
Response.End();

You can adjust the height and width of your image to suit your requirements. The tag <TD>will require the same height and width settings.

+2
source

StringWriter System.Text.Encoding.ASCII.GetBytes(stringwriter string) . outputStream.Write(byte_array, 0, byte_array.Length); outputstream - HttpContext Response.

, Grid, Images, Excel, . , , Excel .

0

If you use C # and want to export to Excel, I would recommend EPPLUS

http://epplus.codeplex.com/

Save you a lot of trouble now and in the future.

0
source

Source: https://habr.com/ru/post/1792796/


All Articles