前导零的问题将数据网格导出为ex​​cel

时间:2018-05-30 10:22:42

标签: asp.net

我正在从数据库绑定DataGrid并尝试将其导出到Excel但是我遇到了来自First列的前导零的问题,其中包含策略编号,假设在数据库策略号中。是'012345678',但当我将它导出到excel然后显示'12345678'

我的代码在下面:

public static void ToExcelWithHeader(DataSet dsInput, string filename,HttpResponse response, string HeaderPart, DataTable dt, string Url = "")
{
    DataGrid dg = new DataGrid();
    dg.DataSource = dsInput.Tables[0];
    dg.DataBind();
    response.ClearContent();
    response.Buffer = true;

    response.AddHeader("content-disposition", "attachment; filename=" + filename);
    response.ContentType = "application/vnd.ms-excel";

    System.IO.StringWriter objSW = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter objHTW = new System.Web.UI.HtmlTextWriter(objSW);

    dg.HeaderStyle.Font.Bold = true;     // SET EXCEL HEADERS AS BOLD.

     //STYLE THE SHEET AND WRITE DATA TO IT.
    response.Write("<style>");
    response.Write("TABLE { border:dotted 1px #999; }");
    response.Write("TD { border:dotted 1px #D5D5D5; text-align:center; vertical-align: top; }");
    response.Write(".table { background-color: yellow; text-align: left; vertical-align: top; }");
    response.Write("</style>");

    response.Write(HeaderPart);

    response.Write(objSW.ToString());
    if (Url != "")
        response.AddHeader("Refresh", "3; url=" + Url);
    response.End();


    dg = null;
        }
    }
    catch { }
}

0 个答案:

没有答案