C #: Datagrid to Excel. Missing Cell border in generated excel file

I am trying to create an excel file from a datagrid in my asp.net page using the code below. I can create an excel file. But the created excel file has no cell borders. Without cell borders, it looks like a document with text.

alt text

My code

        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("content-disposition", "attachment;filename=asas.xls");
        Response.Charset = "";
        this.EnableViewState = false;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        this.ClearControls(dgShipping);
        dgShipping.AllowPaging = false;
        DisplayRecords();
        dgShipping.RenderControl(oHtmlTextWriter);
        Response.Write(oStringWriter.ToString());
        Response.End();
        dgShipping.AllowPaging = true;

Any workarounds for this? thanks for the help

+3
source share
1 answer

Add a line 2 Linesafter the "RenderControl" line. It will solve your problem and grid lines will be added.

string style = @"<style> TABLE { border: 1px solid red; } TD { border: 1px solid red; } </style> ";
Response.Write(style);

Change the color and line thickness according to your requirements.

+1
source

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


All Articles