从不同的csv文件的源到目标的转换 [英] Converting from source to destination of differnt csv files

查看:91
本文介绍了从不同的csv文件的源到目标的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个csv文件转换为另一种格式不同的csv文件.源和目标列是不同的.如果您有任何建议,参考或指示,我将不胜感激.

I want to convert one csv file to another csv file that is different format. The columns of source and destination are different. If you have any suggestion or reference or instructions, I would appreciate it.

推荐答案

read your first csv, and write with appropriate format with second csv


        public static DataTable ParseCSV(string path)
        {
            //Unicode Characterset=Unicode
            //UTF-8 Characterset=65001
            //ANSI Characterset=1252
            if (!File.Exists(path))
                return null;
            string full = Path.GetFullPath(path);
            string file = Path.GetFileName(full);
            string dir = Path.GetDirectoryName(full);
            //create the "database" connection string             
            string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
           + "Data Source=\"" + dir + "\\\";"
           + "Extended Properties=\"Text;Characterset=1252;HDR=Yes;FMT=Delimited('')\"";
            //create the database query            
            string query = "SELECT * FROM " + "[" + file + "]";
            //create a DataTable to hold the query results            
            DataTable dTable = new DataTable();
            //create an OleDbDataAdapter to execute the query            
            OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);


            try
            {
                //fill the DataTable                
                dAdapter.Fill(dTable);
            }
            catch (InvalidOperationException /*e*/)
            { }
            dAdapter.Dispose();
            string strcellData = "";
            foreach (DataRow dr in dTable.Rows)
            {

                foreach (var fieldValue in dr.ItemArray)
                {
                    object cellData = fieldValue;
                    strcellData += cellData.ToString();
                }
                strcellData += Environment.NewLine;
            }



            System.Windows.Forms.MessageBox.Show(strcellData);
            return dTable;
        }

        public void ExportCSV(string filepathName, string fileOut)
        {
           
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Name,Mobile,Group");//set your headers here, used comma and delimeter, change to ; if you want semicolon

            DataTable dt = ParseCSV(filepathName);

            foreach (DataRow row in dt.Rows)
            {
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    sb.Append(row[i].ToString() + ","); //used comma and delimeter, change to ; if you want semicolon
                }
                sb.Remove(sb.Length - 1, 1);
                sb.Append(Environment.NewLine);
            }

            File.WriteAllText(fileOut, sb.ToString(), Encoding.Default);


        }


您将DataTable用作缓冲区.以编程方式创建一个数据表以匹配源格式.将源文件读入数据表.现在遍历数据表,以所需格式写入目标文件.
You use a DataTable as a buffer. Programmatically create a datatable to match the format of the source. Read source file into the datatable. Now iterate through the datatable writing the destination file in the required format.




要创建此功能,您需要学习文件处理 [ ^ ]

在C#中读写CSV文件 [ ^ ]本文将也可以帮助您,

一方面读取您的csv文件,另一方面创建新的csv文件并将其写入.

谢谢
-Amit Gajjar
Hi,

To create this functionality you need to learn FileHandling[^]

Reading and Writing CSV Files in C#[^] this article will also help you,

on one side read your csv file and on other side create new csv file and write into it.

Thanks
-Amit Gajjar


这篇关于从不同的csv文件的源到目标的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆