在CSV文件中添加列标题

时间:2016-05-23 12:10:17

标签: c# csv opencsv

我正在开发一个系统,在其中一个进程中,每天从Url下载一个.csv文件,只有这个文件带有没有标题的列,我想每次都在autmaticamenta列中输入标题完成下载文件。

new row ------->   new value     -    new value     -    new value
                 existing value  -  existing value  -  existing value
                 existing value  -  existing value  -  existing value
                 existing value  -  existing value  -  existing value

我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Globalization;

namespace CotacaoMoeda
{
    class Program
    {
        static void Main(string[] args)
        {
            GetUrlCsv();
        }


        static void GetUrlCsv()
        {
            var dateString = DateTime.Now.ToString("yyyyMMdd");

            //string dateString = "20160520";
            WebClient wc = new WebClient();
            wc.DownloadFile("http://www4.bcb.gov.br/Download/fechamento/" + dateString + ".csv", @"c:\temp\" + dateString + ".csv");

            //Console.Write(dateString);

            //String[] Values = File.ReadAllText(@"c:\temp\" + dateString + ".csv").Split(';');

            string Caminho = @"c:\temp\" + dateString + ".csv";
            StreamReader oStreamReader = new StreamReader(Caminho);
            DataTable oDataTable = new DataTable();
            int rowCount = 0;

            string[] columnNames = null;
            string[] oStreamDataValues = null;

            while (!oStreamReader.EndOfStream)
            {
                string oStreamRowData = oStreamReader.ReadLine().Trim();

                if (oStreamRowData.Length > 0)
                {
                    oStreamDataValues = oStreamRowData.Split(';');
                    if (rowCount == 0)
                    {
                        rowCount = 1;
                        columnNames = oStreamDataValues;
                        foreach (string csvHeader in columnNames)
                        {
                            DataColumn oDataColum = new DataColumn(csvHeader.ToUpper(), typeof(string));

                            oDataColum.DefaultValue = string.Empty;
                            oDataTable.Columns.Add(oDataColum);
                        }
                    }
                    else
                    {
                        DataRow oDataRow = oDataTable.NewRow();

                        for (int i = 0; i < columnNames.Length; i++)
                        {
                            oDataRow[columnNames[i]] = oStreamDataValues[i] == null ? string.Empty : oStreamDataValues[i].ToString();

                        }
                        oDataTable.Rows.Add(oDataRow);
                    }
                }
            }
            oStreamReader.Close();
            oStreamReader.Dispose();

            foreach (DataRow Dr in oDataTable.Rows)
            {
                string RowValues = string.Empty;
                foreach (string csvColumns in columnNames)
                {
                    RowValues += csvColumns + "=" + Dr[csvColumns].ToString() + " ";
                }
                Console.Write(RowValues);
            }
            Console.ReadKey();
            Console.ReadLine();
        }


    }
}

1 个答案:

答案 0 :(得分:0)

使用NotePad ++打开您的csv文件。你会看到

existing value  ;  existing value  ;  existing value
existing value  ;  existing value  ;  existing value
existing value  ;  existing value  ;  existing value

您在第1行添加新字符串

new value       ;    new value     ;    new value
existing value  ;  existing value  ;  existing value
existing value  ;  existing value  ;  existing value
existing value  ;  existing value  ;  existing value

并保存文件

C#代码

           var path = @"C:\temp\new 1.csv";
            List<String> strcsv = new List<string>();
            using (var rd = new StreamReader(path))
            {
                while (!rd.EndOfStream)
                {
                    strcsv.Add(rd.ReadLine());
                }
            }

            //new value

            List<String> strcsvNew = new List<string>();
            strcsvNew.Add("new value       ;    new value     ;    new value");
            foreach (var item in strcsv)
            {
                strcsvNew.Add(item);
            }

            if (System.IO.File.Exists(path))
                System.IO.File.Delete(path);
            StreamWriter writer = new StreamWriter(path);
            foreach (var item in strcsvNew)
            {
                writer.WriteLine(item);
            }
            writer.Dispose();