将数据表导出到excel,值3E2变成300,应该是文本3E2
我使用以下代码将数据表转换为 Excel: 公共静态无效 ExportDataTabletoExcel(String fileName, DataTable thetable) { if (表!= null) {
//clear anything in io buffer
HttpContext.Current.Response.Clear();
//set to excel for to save file.
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.BufferOutput = true;
//write columns
string tab = "";
foreach (DataColumn dc in thetable.Columns)
{
HttpContext.Current.Response.Write(tab + dc.ColumnName);
tab = "\t";
}
HttpContext.Current.Response.Write("\n");
//write rows
int i;
foreach (DataRow dr in thetable.Rows)
{
tab = "";
for (i = 0; i < thetable.Columns.Count; i++)
{
//if ( i != 1 )
HttpContext.Current.Response.Write(tab + dr[i].ToString());
//else
//HttpContext.Current.Response.Write(tab + "'" + dr[i].ToString());
tab = "\t";
}
HttpContext.Current.Response.Write("\n");
}
HttpContext.Current.Response.End();
}
}
但是,对于值 3E2 或 0E2 或 4E3 ,它会转换为数字。其他值(如 1D2)会正确转换为字符串。
例如 3E2 变为 300 等。
I am converting datatable to excel using the following code:
public static void ExportDataTabletoExcel(String fileName, DataTable thetable)
{
if (thetable != null)
{
//clear anything in io buffer
HttpContext.Current.Response.Clear();
//set to excel for to save file.
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.BufferOutput = true;
//write columns
string tab = "";
foreach (DataColumn dc in thetable.Columns)
{
HttpContext.Current.Response.Write(tab + dc.ColumnName);
tab = "\t";
}
HttpContext.Current.Response.Write("\n");
//write rows
int i;
foreach (DataRow dr in thetable.Rows)
{
tab = "";
for (i = 0; i < thetable.Columns.Count; i++)
{
//if ( i != 1 )
HttpContext.Current.Response.Write(tab + dr[i].ToString());
//else
//HttpContext.Current.Response.Write(tab + "'" + dr[i].ToString());
tab = "\t";
}
HttpContext.Current.Response.Write("\n");
}
HttpContext.Current.Response.End();
}
}
However for value 3E2 or 0E2 or 4E3 , it is converted to number.. other value like 1D2 is correctly converted to string.
for example 3E2 become 300 etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将值放在双引号中,以便 Excel 可以明显看出它是字符串而不是科学记数法中的数字。
Put double quotes around the value so that it is obvious to Excel that it is a string and not a number in scientific notation.