我正在将数据表导出到Excel,并且一列在数据表中有电话号码,但在Excel中导出后,电话号码列显示为指数。
我需要数字,如何解决这个问题?
string fileName = "File" + DateTime.Now.ToString("MMddyyyy_HHmmss") + ".xls";
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
//Response.AddHeader("content-disposition", "attachment;filename=File.xls");
Response.ContentType = "application/vnd.ms-excel";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
DataGrid dataExportExcel = new DataGrid();
dataExportExcel.ItemDataBound += new DataGridItemEventHandler(dataExportExcel_ItemDataBound);
dataExportExcel.DataSource = dt;
dataExportExcel.DataBind();
dataExportExcel.RenderControl(htmlWrite);
System.Text.StringBuilder sbResponseString = new System.Text.StringBuilder();
sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:xlExcel8\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head></head> <body>");
sbResponseString.Append(stringWriter + "</body></html>");
Response.Write(sbResponseString.ToString());
Response.End();
答案 0 :(得分:4)
将以下 STYLE 添加到您的HTML代码中,它会对您有帮助..
<style> table { mso-number-format:'0'; } </style>
喜欢这样:
sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:xlExcel8\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><style> td { mso-number-format:'0'; } </style></head> <body>");
完整代码:
protected void btnExcel_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("Phone");
dt.Columns.Add("Name");
DataRow Sample = dt.NewRow();
Sample["Phone"] = 125316245612456124;
Sample["Name"] = "Pandian";
dt.Rows.Add(Sample);
GetExcel(dt);
}
public void GetExcel(DataTable dt)
{
string fileName = "File" + DateTime.Now.ToString("MMddyyyy_HHmmss") + ".xls";
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.ContentType = "application/vnd.ms-excel";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
DataGrid dataExportExcel = new DataGrid();
dataExportExcel.DataSource = dt;
dataExportExcel.DataBind();
dataExportExcel.RenderControl(htmlWrite);
System.Text.StringBuilder sbResponseString = new System.Text.StringBuilder();
sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:xlExcel8\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><style> table { mso-number-format:'0'; } </style></head> <body>");
sbResponseString.Append(stringWriter + "</body></html>");
Response.Write(sbResponseString.ToString());
Response.End();
}
Excel输出:
答案 1 :(得分:2)
您可以使用NumberValue and NumberFormat properties
这是一个示例,设置您的范围
yourSheet.Range[".."].NumberValue = 1234.5678;
yourSheet.Range[".."].NumberFormat = "0.00";
答案 2 :(得分:2)
使用它 td {mso-number-format:“@”; }
答案 3 :(得分:1)
在将电话号码写入电话号码的单元格之前,您可以在电话号码前添加单引号。
喜欢“'1234567890”而不是“1234567890”
感谢