Your issue most likely is not properly writing the header, consider using SpreadSheetLight library which is free.
Here are a few examples.
public class Operations
{
/// <summary>
/// Create a new Excel file, rename the default sheet from
/// Sheet1 to the value in pSheetName
/// </summary>
/// <param name="fileName"></param>
/// <param name="sheetName"></param>
/// <returns></returns>
public bool CreateNewFile(string fileName, string sheetName)
{
using SLDocument document = new();
document.RenameWorksheet("Sheet1", sheetName);
document.SaveAs(fileName);
return true;
}
/// <summary>
/// Create a new Excel file
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public bool CreateNewFile(string fileName)
{
using SLDocument document = new();
document.SaveAs(fileName);
return true;
}
/// <summary>
/// Add a new sheet if it does not currently exists.
/// </summary>
/// <param name="fileName"></param>
/// <param name="sheetName"></param>
/// <returns></returns>
public bool AddNewSheet(string fileName, string sheetName)
{
using SLDocument document = new(fileName);
if (!(document.GetSheetNames(false)
.Any((workSheetName) => string.Equals(workSheetName, sheetName, StringComparison.CurrentCultureIgnoreCase))))
{
document.AddWorksheet(sheetName);
document.Save();
return true;
}
else
{
return false;
}
}
}
Otherwise, try the following
public void CreateNewFile(string pFileName, string pSheetName)
{
try
{
using (var doc = SpreadsheetDocument.Create(pFileName, SpreadsheetDocumentType.Workbook))
{
var wbp = doc.AddWorkbookPart();
wbp.Workbook = new Workbook();
var wsp = wbp.AddNewPart<WorksheetPart>();
wsp.Worksheet = new Worksheet(new SheetData());
var sheets = wbp.Workbook.AppendChild(new Sheets());
var sheet = new Sheet()
{
Id = wbp.GetIdOfPart(wsp),
SheetId = 1,
Name = pSheetName
};
sheets?.Append(sheet);
wbp.Workbook.Save();
}
}
catch (Exception ex)
{
// TODO
}
}