zoukankan      html  css  js  c++  java
  • c# datatable 如何转CSV文件

     public void DataTableToCSV(DataTable dtCSV, string csvFileFullName, bool writeHeader, 
                string delimeter)
            {
                if ((null != dtCSV) && (dtCSV.Rows.Count > 0))
                {
                    //Delete the old one
                    if (File.Exists(csvFileFullName))
                    {
                        File.Delete(csvFileFullName);
                    }
    
                    string tmpLineText = "";
    
                    //Write header
                    if (writeHeader)
                    {
                        tmpLineText = "";
                        for (int i = 0; i < dtCSV.Columns.Count; i++)
                        {
                            string tmpColumnValue = dtCSV.Columns[i].ColumnName;
                            if (tmpColumnValue.Contains(delimeter))
                            {
                                tmpColumnValue = """ + tmpColumnValue + """;
                            }
    
                            if (i == dtCSV.Columns.Count - 1)
                            {
                                tmpLineText += tmpColumnValue;
                            }
                            else
                            {
                                tmpLineText += tmpColumnValue + delimeter;
                            }
                        }
                        WriteFile(csvFileFullName, tmpLineText);
                    }
    
                    //Write content
                    for (int j = 0; j < dtCSV.Rows.Count; j++)
                    {
                        tmpLineText = "";
                        for (int k = 0; k < dtCSV.Columns.Count; k++)
                        {
                            string tmpRowValue = dtCSV.Rows[j][k].ToString();
                            if (tmpRowValue.Contains(delimeter))
                            {
                                tmpRowValue = """ + tmpRowValue + """;
                            }
    
                            if (k == dtCSV.Columns.Count - 1)
                            {
                                tmpLineText += tmpRowValue;
                            }
                            else
                            {
                                tmpLineText += tmpRowValue + delimeter;
                            }
                        }
                        WriteFile(csvFileFullName, tmpLineText);
                    }
                }
            }
    
            private void WriteFile(string fileFullName, string message)
            {
                using (StreamWriter sw = new StreamWriter(fileFullName, true, Encoding.UTF8))
                {
                    sw.WriteLine(message);
                }
            }
  • 相关阅读:
    HTC G7 搜索和感光按键修改
    Delphi开源组件SynEdit
    (转)Delphi获取windows系统版本信息
    TDateTime转UTC的时间差
    Windows7 C盘无法读写文件
    Convert UTC string to TDatetime in Delphi
    delphi抓全屏图,游戏窗口,游戏Client窗口
    ADO Table Locate
    Delphi与管道操作
    Delphi从UTC (GMT)返回时差
  • 原文地址:https://www.cnblogs.com/mibing/p/6144176.html
Copyright © 2011-2022 走看看