• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

Rusteel Wang's Blog

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

excel对csv的转义

最近遇到一个问题,csv里数字过长会被excel转义为科学计数法显示(经过试验,超过8位就会被转)。在网上搜索到别人的经验,得知可以在前面加\t来禁止excel对csv转义。

 

PS:如果要禁止excel对excel的转义,可以在内容前加上半角单引号'。 

 

原文链接:http://www.zu14.cn/2009/05/07/csharp-export-data-to-csv-xls-avoid-number-auto-transfering/ 

 

附上自己的转义方法:

 private string ExcelEscape(string cell)

        {
            if (string.IsNullOrEmpty(cell))
            {
                return cell;
            }
                        
            if (cell.Contains(','))
            {
                cell = "\"" + cell + "\"";
            }            

            if (cell.StartsWith("0") || cell.Length > 8)
            {
                cell = "\t" + cell;
            }

            return cell;
        }

posted on 2011-10-19 12:19  Rusteel  阅读(1018)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3