注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

xian827991006

专注于DotNet编程,QlikView开发,网站seo,各种软件工具,互联网

 
 
 

日志

 
 
关于我

爱看书、爱探索的人. 目前关注与QlikView开发,推荐一个QlikView资源站。www.qlikviewcn.com

网易考拉推荐

C# 导出CSV文件  

2012-06-28 10:17:22|  分类: C#和VS |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

private void ExportFileToCsv()
        {
            SaveFileDialog saveFileDialog =  new SaveFileDialog();
            saveFileDialog.DefaultExt = "*.csv";
            saveFileDialog.AddExtension = true;
            saveFileDialog.Filter = "csv files|*.csv";
            saveFileDialog.OverwritePrompt = true;
            saveFileDialog.CheckPathExists = true;
            saveFileDialog.FileName = "AnalysisData";

            if (saveFileDialog.ShowDialog() == true && saveFileDialog.FileName != null) //打开保存文件对话框
            {
                string fileName = saveFileDialog.FileName;//文件名字
                using (StreamWriter streamWriter = new StreamWriter(fileName, false, Encoding.Default))
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append(" StartTime ").Append(","); //没一个字段后面都加逗号,表示是一列,因为这是第一行 因此也是列标题
                    sb.Append(" TraceStart ").Append(",");
                    sb.Append(" TraceEnd ").Append(",");
                    sb.Append(" Duration ").Append(",");
                    sb.Append(" Operation ").Append(",");
                    sb.Append(" ColorVal ").Append(",");
                    sb.Append(" Object ").Append(",");
                    sb.Append(" Resource ").Append(",");
                    sb.Append(" TargetResource ");
                    streamWriter.WriteLine(sb.ToString());

//要写的数据源

                    List<data> list = this.DataContext as List<data>;
                    double taskTime = 0 ;
                    foreach (DataAnalysisResultModel model in list)
                    {
                         taskTime = model.TaskTime;
                        sb = new StringBuilder();
                        sb.Append(StringUtil.ReplaceSpecialChars(model.StartTime + "")).Append(",");
                        sb.Append(StringUtil.ReplaceSpecialChars(model.TraceStart + "")).Append(",");
                        sb.Append(StringUtil.ReplaceSpecialChars(model.TraceEnd + "")).Append(",");
                        sb.Append(StringUtil.ReplaceSpecialChars(model.Duration + "")).Append(",");
                        sb.Append(StringUtil.ReplaceSpecialChars(model.Operation + "")).Append(",");
                        sb.Append(StringUtil.ReplaceSpecialChars(model.ColorVal + "")).Append(",");
                        sb.Append(StringUtil.ReplaceSpecialChars(model.Object != null ? model.Object + "" : "")).Append(",");
                        sb.Append(StringUtil.ReplaceSpecialChars(model.Resource != null ? model.Resource + "" : "")).Append(",");
                        sb.Append(StringUtil.ReplaceSpecialChars(model.TargetResource != null ? model.TargetResource + "" : ""));
                        streamWriter.WriteLine(sb.ToString());
                    }

                    sb = new StringBuilder();
                    sb.Append("Trace time:") .Append(taskTime+"");
                    streamWriter.WriteLine("");
                    streamWriter.WriteLine("");
                    streamWriter.WriteLine(sb.ToString());

                    streamWriter.Flush();
                    streamWriter.Close();
                }
            }

}

 

 


public class StringUtil
    {

        /// <summary>
        /// 替换特殊字符
        /// </summary>
        /// <param name="input">字符串</param>
        /// <returns></returns>
        public static string ReplaceSpecialChars(string input)
        {
            // space -> _x0020_ 特殊字符的替换
            // % -> _x0025_
            // # -> _x0023_
            // & -> _x0026_
            // / -> _x002F_
            if (input == null) return "";
            input = input.Replace(" ", "_x0020_")
            .Replace("%", "_x0025_")
            .Replace("#", "_x0023_")
            .Replace("&", "_x0026_")
            .Replace("/", "_x002F_");
            return input;
        }
    }

C# 导出CSV 然后用excel打开,主要是用逗号来分割开每一列


  评论这张
 
阅读(3811)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017