So I've have an quite Big DataTable over 10.000 data and I wanted to Export it to an Excel File.
At first I tryd librarys like ClosedXML or Interop.Excel. But for this amount of Data it takes way to long.
So I decided to make it with an FileStream, thats so much faster and the creating of the excel File is working except that when I want to open the File I got an Message from Excel that the File is corrupted, that the FileFormat and File extension not fitting together.
Has someone an Idea how I can create an Excel File without Librarys or getting this Message?
My Code looks like that:
string path = path + "someFilename.xlsx";
using (FileStream sw = File.Create(path))
{
var data = Encoding.Unicode.GetBytes("Artikelnummer" + "\t" + "Hersteller" + "\t" + "Beschreibung" + "\t" + "Nettopreis" + "\t" + "Bruttopreis" + "\t" + "Zustand" + "\t" + "P/N" + "\t" + "Kategorie I" + "\t" + "Kategorie II" + "\t" + "Kategorie III" + "\t" + "Shop-Link" + "\n");
sw.Write(data, 0, data.Length);
foreach (DataRow r in dt.Rows)
{
data = Encoding.Unicode.GetBytes(r["Artikelnummer"].ToString() + "\t" + r["Hersteller"].ToString() + "\t" + r["Bezeichnung"].ToString() + "\t" + r["Nettopreis"].ToString() + "\t" + r["Bruttopreis"].ToString() + "\t" + r["Zustand"].ToString() + "\t" + r["PN"].ToString() + "\t" + r["Kategorie I"].ToString() + "\t" + r["Kategorie II"].ToString() + "\t" + r["Kategorie III"].ToString() + "\t" + r["Link"].ToString() + "\n");
sw.Write(data, 0, data.Length);
}
}
dt is my DataTable