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);
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();
}