How do i return an '.xlsx' file from a XLWorkbook object?

Viewed 14

I'm actually making a routine for a project that has to generate a table and then an '.xlsx' file from it and then pass it as return value of my method:
based on parameters passed from a POST method, i'm performing a filtered select from my database getting a list of data and store it into a list of object.

List<myTable> mySearch = new List<myTable>();
using (MyDBEntities mydb = new MyDBEntities())
{
    mySearch = mydb.myTable.Where(o=>o.price > 5000).ToList();
}

I then convert this List into table:

DataTable dataTable = new DataTable(typeof(myTable).Name);
PropertyInfo[] Props = typeof(myTable).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
    dataTable.Columns.Add(prop.Name);
}
foreach (var item in myTable)
{
    var values = new object[Props.Length];
    for (int i = 0; i < Props.Length; i++)
    {
        values[i] = Props[i].GetValue(item, null);
    }
    dataTable.Rows.Add(values);
}

Then i found a NuGet package (ClosedXML) to convert my 'dataTable' into a '.xlsx' file and save it:

XLWorkbook wb = new XLWorkbook();
wb.Worksheets.Add(dataTable, "New Table");
wb.SaveAs(myPath + ".xlsx");

Everything works fine, but i want to change this last saving part, without saving any file in my server, but just passing a result to my front to make it download an xlsx file from client without having to store it in my server.(maybe base64 encoding?)...I'm stuck here!

0 Answers
Related