将 gridview 导出到 Excel 时,我不知道如何“强制”列全部左对齐。有些似乎自动执行此操作,而列中的其他字段将向右对齐。(该栏全是数字)我做错了什么,或者忘记做什么?
我的 .aspx 中的按钮
<asp:ImageButton ID="Button1" runat="server" Height="30px" ImageUrl="~/images/Logo.jpg" OnClick="Button1_Click" Width="28px" />
我的代码背后
private void ExportToExcel(string strFileName, GridView dg)
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
GridView1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
protected void Button1_Click(object sender, EventArgs e)
{
ExportToExcel("Test.xls", GridView1);
}