• 出力csv


            public static void ExportResultLog(System.Data.DataTable dt, string fileName, string path)
            {
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
    
                string strPath = path + fileName;
    
                StringBuilder strColu = new StringBuilder();
                StringBuilder strLineValue = new StringBuilder();
                try
                {
                    if (System.IO.File.Exists(strPath))
                    {
                        string[] stringlines = System.IO.File.ReadAllLines(strPath, Encoding.Default);
                        string line = string.Empty;
                        foreach (string readLine in stringlines)
                        {
                            line += readLine + "
    ";
                        }
                        line = line.Remove(line.Length - 2, 2);
    
                        System.IO.StreamWriter sw = new System.IO.StreamWriter(strPath, false, Encoding.GetEncoding("UTF-8"));
    
                        sw.WriteLine(line);
    
                        foreach (DataRow dr in dt.Rows)
                        {
                            strLineValue.Remove(0, strLineValue.Length);
                            for (int i = 0; i <= dt.Columns.Count - 1; i++)
                            {
                                strLineValue.Append(ReplaceChar(dr[i] == DBNull.Value ? "" : dr[i].ToString()));
                                strLineValue.Append(",");
                            }
                            strLineValue.Remove(strLineValue.Length - 1, 1);
                            sw.WriteLine(strLineValue.ToString());
                        }
                        sw.Close();
                    }
                    else
                    {
                        System.IO.StreamWriter sw = new System.IO.StreamWriter(strPath, false, Encoding.GetEncoding("UTF-8"));
    
                        for (int 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)
                        {
                            strLineValue.Remove(0, strLineValue.Length);
                            for (int i = 0; i <= dt.Columns.Count - 1; i++)
                            {
                                strLineValue.Append(ReplaceChar(dr[i] == DBNull.Value ? "" : dr[i].ToString()));
                                strLineValue.Append(",");
                            }
                            strLineValue.Remove(strLineValue.Length - 1, 1);
                            sw.WriteLine(strLineValue.ToString());
                        }
                        sw.Close();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
    
            }
    
    
            public static void ExportLog(System.Data.DataTable dtLogInfo, string fileName, string path)
            {
                string strPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
    
                if (!System.IO.Directory.Exists(strPath))
                {
                    System.IO.Directory.CreateDirectory(strPath);
                }
    
                strPath = path + fileName;
    
                StringBuilder strColu = new StringBuilder();
                StringBuilder strValue = new StringBuilder();
                try
                {
                    if (System.IO.File.Exists(strPath))
                    {
                        string[] stringlines = System.IO.File.ReadAllLines(strPath, Encoding.Default);
                        string line = string.Empty;
                        foreach (string readLine in stringlines)
                        {
                            line += readLine + "
    ";
                        }
    
                        System.IO.StreamWriter sw = new System.IO.StreamWriter(strPath, false, Encoding.GetEncoding("UTF-8"));
                        strValue.Remove(0, strValue.Length);
                        for (int i = 0; i <= dtLogInfo.Columns.Count - 1; i++)
                        {
                            strValue.Append(ReplaceChar(dtLogInfo.Rows[0][i] == DBNull.Value ? "" : dtLogInfo.Rows[0][i].ToString()));
                            strValue.Append(",");
                        }
                        strValue.Remove(strValue.Length - 1, 1);
                        string value = line + strValue.ToString();
                        sw.WriteLine(value);
                        sw.Close();
                    }
                    else
                    {
                        System.IO.StreamWriter sw = new System.IO.StreamWriter(strPath, false, Encoding.GetEncoding("UTF-8"));
    
                        for (int i = 0; i <= dtLogInfo.Columns.Count - 1; i++)
                        {
                            strColu.Append(dtLogInfo.Columns[i].ColumnName);
                            strColu.Append(",");
                        }
                        strColu.Remove(strColu.Length - 1, 1);
                        sw.WriteLine(strColu);
    
                        strValue.Remove(0, strValue.Length);
                        for (int i = 0; i <= dtLogInfo.Columns.Count - 1; i++)
                        {
                            strValue.Append(ReplaceChar(dtLogInfo.Rows[0][i] == DBNull.Value ? "" : dtLogInfo.Rows[0][i].ToString()));
                            strValue.Append(",");
                        }
                        strValue.Remove(strValue.Length - 1, 1);
                        sw.WriteLine(strValue.ToString());
    
                        sw.Close();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
    

      

  • 相关阅读:
    CentOS8下升级Python3.6到3.9
    web service基础知识
    mysql+centos7+主从复制
    saltstack高效运维
    Docker
    python如何配置virtualenv
    Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
    nginx+uWSGI+django+virtualenv+supervisor发布web服务器
    RabbitMQ消息队列-Centos7下安装RabbitMQ3.6.1
    flask-wtforms
  • 原文地址:https://www.cnblogs.com/wodegui/p/4876422.html
Copyright © 2020-2023  润新知