• 出力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;
                }
            }
    

      

  • 相关阅读:
    iOS开发之Xcode8兼容适配iOS 10资料整理笔记
    C#流概述
    C#回调实现的一般过程
    ASP.Net MVC的学习
    RAID基本知识
    Infiniband基本知识
    [转]开源实时视频码流分析软件:VideoEye
    [转]高分一号的落后与特色
    [转]MVC,MVP 和 MVVM 的图示
    图文助你打开MS SQL Serever的ldf和mdf文件
  • 原文地址:https://www.cnblogs.com/wodegui/p/4876422.html
Copyright © 2020-2023  润新知