I have a method that creates an excel sheet from a list of strings. The strings are details that are like:
Name: Obi Wan Kenobi
Location: Tattooine
So far, when creating an excel sheet in c#, I'm able to add it to all to individual rows. However, I would like to split each string at the colon and add them to separate columns. I want it to look something like this:
| A | B |
|---|---|
| Name | Obi Wan Kenobi |
| Location | Tattooine |
So far I've tried this:
public static void CreateExcelSheet(List<string> list)
{
var wb = new XLWorkbook();
var ws = wb.Worksheets.Add("Data_Test_Worksheet");
foreach (var item in list)
{
for (int i = 0; i < list.Count; i++)
{
var detail = item.Split(':');
ws.Cell(i + 1, 1).InsertData(detail[0]);
ws.Cell(i + 1, 2).InsertData(detail[1]);
}
}
wb.SaveAs(@"c:\temp\Data_Text.xlsx");
}
This is how I though the logic would play out but when I execute it, it errors.
Can someone please point me in the right direction? Thanks