Hello guys,
in my job i have a task to export an excel file from our application. This one thing teared me apart because i couldn’t manage to complete because i haven’t work before with that legacy code. I am fairly stuck in my head right now how to acomplish this task.
My thoughts are to import somehow the database table to a DataTable and then…somehow export this DataTable to an excel file. We use Infragistics Workbook for Excel files and we can only use them. I feel overburned for 3 days now as whatever i tried didnt work.
Any Ideas how i will do it?
Reading and manipulating Excel data can be achieved through this wonderful package IExcelDataReader and the only way I can think of importing back to Excel is through a CSV. I must say I’m not familiar with what Infragistics is.
Closedxml my dude.
Thanks for letting me know of this package, but as what i said i can not install any package. I managed to fill the DataTable but after that it is dark for me. Maybe i need time to cool down from this.
Closedxml can read and write excel files.
And directly output a data table. It’s a great library.
Here is the link https://github.com/closedxml/closedxml
What I’ve done in the past is to use the Excel Interop. The interop basically launches excel in the background and provides an API interface for telling excel what to do. It’s not going to be the fastest way to create an excel file. But, if Excel is already present on the systems that will be using this feature, it should meet your “can’t install any additional package” requirement.
Examples of use: https://www.dotnetperls.com/excel
Would a CSV file be acceptable or does it have to be a proper Excel file?
Unfortunately, only excel file. Because this file is also used for importing data. Maybe i reverse engineer the import method i created in order to achieve that.
Install EPPlus and the following code will do the job for you.
public void ExportDatatTableToExcel(DataTable dt, string filepath){//create folder if it doesn't exist.string directoryPath = Path.GetDirectoryName(filepath);if (!Directory.Exists(directoryPath))Directory.CreateDirectory(directoryPath);
}
C# devs
null reference exceptions