I wanted to write to a specific sheet of an excel file having multiple sheets. How can I do so using C#. This is the method that I have:
public void WriteCSV<T>(IEnumerable<T> items, string path, StringBuilder stringBuilder)
{
var csvFileLength = new System.IO.FileInfo(path).Length;
var csv = stringBuilder;
Type itemType = typeof(T);
var props = itemType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
//
using (StreamWriter sw = new StreamWriter(path, true))
{
if (csvFileLength <= 2)
{
sw.WriteLine(string.Join(", ", props.Select(p => p.Name)));
}
foreach (var item in items)
{
sw.WriteLine(string.Join(", ", props.Select(p => p.GetValue(item, null))));
//var newLine = string.Format(write, Environment.NewLine);
//csv.Append(newLine);
//File.WriteAllText(path, csv.ToString());
}
}