【发布时间】:2014-02-07 15:40:49
【问题描述】:
我希望将我的数据表导出到 excel。
我有一列的值类似于 2/5 1/5 ... 将此数据导出到 excel 后,它看起来像 02.May 01.May(日期格式)
如何导出此列,以便 excel 将其视为字符串而不是将其转换为日期。?
这是我的 ExportToExcel 方法
protected void ExportToExcel()
{
DataTable dt = new DataTable();
dt = Session["aaa"] as DataTable;
if (dt.Rows.Count > 0)
{
string filename = "DownloadMobileNoExcel.xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
//Response.ContentType = application/vnd.ms-excel;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
【问题讨论】:
-
见这里:aspsnippets.com/Articles/… - 它基本上使用文本模式 css 类将列格式设置为文本格式。
标签: c# asp.net excel export-to-excel