Click here to Skip to main content
15,921,941 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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.
Posted

C#
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);


        }
 
Share this answer
 
v2
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.
 
Share this answer
 
v2
Comments
MI12344 23-Aug-12 1:20am    
how can I set the delimiter as pipe when I write to the destination file with File.WriteAllText. How will I remove the wrapped double quotes of each cell before writing with File.WriteAllText?
codejet 23-Aug-12 3:04am    
To remove double quotes use Regex.Replace Check http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx. You are a bit unclearon setting pipe as the delimiter.
MI12344 25-Aug-12 2:53am    
Thanks, now I do not need to use regex for special characters in the columns. I realized that csv file should be opened with notepad and see what is the delimiter and what is wrapped with one value.
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900