gridview操作excel导出时解决显示成科学计数法的问题

博客介绍了单元格数据的常见格式化类型,包括文本、日期、数字、货币、百分比等格式,除文本格式测试通过外,日期格式似乎存在问题,其他未测试。若格式化不行,还给出在导出Excel方法中添加代码的操作,并提供了转载链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对单元格数据进行格式化:

常见的格式如下: (除文本格式测试通过外,日期好像不行,其他未测试)

1) 文本:vnd.ms-excel.numberformat:@
2) 日期:vnd.ms-excel.numberformat:yyyy/mm/dd
3) 数字:vnd.ms-excel.numberformat:#,##0.00
4) 货币:vnd.ms-excel.numberformat:¥#,##0.00
5) 百分比:vnd.ms-excel.numberformat: #0.00%

ContractedBlock.gifExpandedBlockStart.gif代码
1 protected void GVExport_RowDataBound(object sender, GridViewRowEventArgs e)
2 {

8 if( e.Row.RowIndex > -1 )
9 {
10 //for (int i = 0; i < e.Row.Cells.Count; i++)
11 //{
12 // e.Row.Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
13 //}
14   e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
15 e.Row.Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
16 //e.Row.Cells[17].Attributes.Add("style", "vnd.ms-excel.numberformat:yyyy/mm/dd");
20   }
21 }

 实在不行,再加上如下操作试试,在导出excel的方法中加上如下代码:

ContractedBlock.gifExpandedBlockStart.gif代码
1 System.Web.UI.WebControls.GridView gv = new GridView();
2 gv.RowDataBound += new System.Web.UI.WebControls.GridViewRowEventHandler(gv_ItemDataBound); //绑定格式转换方法
3 gv.DataSource = dt.DefaultView;
4 gv.DataBind();
5

转载于:https://www.cnblogs.com/pfs1314/archive/2010/03/29/1699314.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值