Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have three csv file all having different header name i want change header name how i want code should be written in generic,please help me how to write generic code to change column names from the csv file

example:
File1:column name
input :Server, date,Average license
output rename to: Server details,Period,Avg. license usage

File2:column name
input :Department,detailUSage,overUsage
output Rename to : Dept,Detail Usage, License over usage
below code can change only one file header but i harcoded the header name that should be generic all the csv file column

What I have tried:

public void ExtractDataToCSV(DataTable dtDataTable, string strFilePath)
      {
          StreamWriter sw = new StreamWriter(strFilePath, false);
          //headers
          dtDataTable.Columns.Add("No.", typeof(int));
          dtDataTable.Columns["No."].SetOrdinal(0);
          for (int count = 0; count < dtDataTable.Rows.Count; count++)
          {
              dtDataTable.Rows[count]["No."] = count + 1;
          }
          for (int i = 0; i < dtDataTable.Columns.Count; i++)
          {
              string[] columnames = new string[] { "No.", "Server", "Period", "Dept ", "Alloted License", "Avg.License used" };
              sw.Write(columnames[i].ToString());
              sw.Write(dtDataTable.Columns[i]);
              sw.Write(',');
          }
          sw.Write(sw.NewLine);
          foreach (DataRow dr in dtDataTable.Rows)
          {
              for (int i = 0; i < dtDataTable.Columns.Count; i++)
              {
                  sw.Write(dr[i].ToString());
                  sw.Write(",");
              }
              sw.Write(sw.NewLine);
          }
          sw.Close();
      }
Posted
Updated 18-Mar-21 21:51pm
Comments
Richard MacCutchan 19-Mar-21 6:00am    
sw.Write(columnames[i].ToString());

You have declared (and initialised) columnames as an aray of strings, so why do you think you need to call ToString on them?
Member 15088142 19-Mar-21 7:03am    
yes ur right tostring not required
Member 15088142 19-Mar-21 7:03am    
any other method is there to rename column names by writing generic code

1 solution

Take a look here: I want to rename my column name[^]
 
Share this answer
 
Comments
Member 15088142 19-Mar-21 7:02am    
any other method to do that i tried
foreach (DataColumn column in dtDataTable.Columns)
{
switch (column.ColumnName)
{
//LicenceAvgData
case "AllotedLicense":
column.ColumnName = "Alloted License";
break;
case "AllotedDate":
column.ColumnName = "Period";
break;
case "AvglicenseUsed":
column.ColumnName = "Avg. License used";
break;
}
}
sw.Write(column.ColumnName);
sw.Write(',');
in this also i am getting but i need other good approach to do
Maciej Los 19-Mar-21 7:22am    
Not like this. Use Dictionary object (solution #2) under the link.

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