-2

I'm working on a project in .NET Core 1.1, and now I have to give the possibility to the user of downloading an Excel file which data is dependant on parameters chosen by the user, so the Excel should be created in the moment when the user clicks the "Export to Excel" button and downloaded.

I've been searching on the internet but I haven't gotten any clear answers to be honest. I guess I will have to use the Open XML SDK, but in order to create it in memory and such, I don't have enough knowledge.

To sum up, I have data in arrays, and I would like to be able, in the moment the user clicks a button, create the excel virtually with the data previously stored in arrays and then download it in the users browser.

Miguel Cordero
  • 141
  • 1
  • 9
  • 3
    It might be worth asking something more specific as it's not clear if you're struggling with creating and downloading the file in memory (see the answer [here](https://stackoverflow.com/questions/29887503/how-to-create-excel-file-using-openxml-without-creating-a-local-file/29950024#29950024)) or if you're struggling to create the contents of the file (there are lots of samples for that, e.g. [this one](https://stackoverflow.com/questions/44572018/using-open-xml-to-create-excel-file/44588074#44588074) so to help you we'd need a specific problem). – petelids Mar 28 '19 at 09:45

2 Answers2

1

Do you also want to show your data before download? Maybe you can use DataTables:

DataTables is a plug-in for the jQuery Javascript library. It is a highly flexible tool, built upon the foundations of progressive enhancement, that adds all of these advanced features to any HTML table.

Pagination Previous, next and page navigation. Instant search Filter results by text search.

Massimo Variolo
  • 4,669
  • 6
  • 38
  • 64
0

using NPOI https://github.com/tonyqus/npoi something in the line of this:

void safearrayAsExcel(object[,] rows,string filename){
    var workbook = new HSSFWorkbook();
    var sheet = workbook.CreateSheet("New Sheet");

    for(int i = 0; i < rows.length;i++)
    {
        var row sheet.CreateRow(i);
        for(int j = 0; j < row.length;j++)
            row .CreateCell(j).SetCellValue(rows[i,j]);
    }
    FileStream fileOut = new FileStream(fileName, FileMode.Create);
    workbook.Write(fileOut);
}

you can ofcourse use a memorystream instead of filestream and do whatever you want with the generated excel

use HSSFWorkbook for xls format. and XSSFWorkbook for xlsx format.

i dont completely know about compatibliy issues with .net core 1.1 and .net standard 2.0 but there should be a way go get it work

Bacon
  • 473
  • 3
  • 9