zoukankan      html  css  js  c++  java
  • c# 导出csv

      public void ExportToSvc(System.Data.DataTable dt, string strName)
            {
                string strPath = strName;
                if (File.Exists(strPath))
                {
                    File.Delete(strPath);
                }
                //先打印标头
                StringBuilder strColu = new StringBuilder();
                StringBuilder strValue = new StringBuilder();
                int i = 0;
                try
                {
                    StreamWriter sw = new StreamWriter(new FileStream(strPath, FileMode.CreateNew), Encoding.GetEncoding("GB2312"));
                    for (i = 0; i <= dt.Columns.Count - 1; i++)
                    {
                        strColu.Append(dt.Columns[i].ColumnName);
                        strColu.Append(",");
                    }
                    strColu.Remove(strColu.Length - 1, 1);//移出掉最后一个,字符
                    sw.WriteLine(strColu);

                    foreach (DataRow dr in dt.Rows)
                    {
                        strValue.Remove(0, strValue.Length);//移出
                        for (i = 0; i <= dt.Columns.Count - 1; i++)
                        {
                            strValue.Append(dr[i].ToString().Replace(",", ";"));
                            strValue.Append(",");
                        }
                        strValue.Remove(strValue.Length - 1, 1);//移出掉最后一个,字符
                        sw.WriteLine(strValue);
                    }
                    sw.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                //System.Diagnostics.Process.Start(strPath);
            }

  • 相关阅读:
    Linux的sz和rz命令
    python正则表达式(8)--分组、后向引用、前(后)向断言
    python正则表达式(7)--flag修饰符、match对象属性
    python正则表达式(6)--split、sub、escape方法
    python正则表达式(5)--findall、finditer方法
    python正则表达式(4)--search方法
    python正则表达式(3)--match方法
    python正则表达式(2)--编译正则表达式re.compile
    Go语言开发教程
    zabbix源码编译安装以及添加第一台host监控
  • 原文地址:https://www.cnblogs.com/hanje/p/10909992.html
Copyright © 2011-2022 走看看