I already have the excel-sheet named test.xls which has 3 worksheets (Files1, Files2 and Results). Now I need to copy the text file content and write it to the 'Results' worksheet.
Below is the C# code that I worked on.
System.IO.StreamReader file = new System.IO.StreamReader("D:\\test.txt");
string line = "";
int counter = 0;
while ((line = file.ReadLine()) != null)
{
if (counter == 0)
{
lstColumnNames.AddRange(line.Split(' '));
}
else
{
List<string> tempRowData = new List<string>();
tempRowData.AddRange(line.Split(' '));
lstRowData.Add(tempRowData);
}
counter++;
}
System.IO.TextWriter tw = new System.IO.StreamWriter("D:\\test.xlsx");
if (lstColumnNames.Count != 0)
{
string temp = "";
foreach (string str in lstColumnNames)
{
if (temp != "")
{
temp += ";";
}
temp += str;
}
tw.WriteLine(temp);
foreach (List<string> lstRow in lstRowData)
{
temp = "";
foreach (string str in lstRow)
{
if (temp != "")
{
temp += ";";
}
temp += str;
}
tw.WriteLine(temp);
}
tw.Close();
The above code deleted all the worksheets and write the text file content into new worksheet.