-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.

1
  • 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) or if you're struggling to create the contents of the file (there are lots of samples for that, e.g. this one so to help you we'd need a specific problem). Commented Mar 28, 2019 at 9:45

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

the data is being through a graph, and the I give the possibility to get the raw data
Here is exact link to export using Datatables -> link
The OP asked how to generate Excel files, not how to display the data
@MassimoVariolo the title says create and make user download. There's nothing about displaying data
I used datatables, it's great, I just hid the table and left the button from File Export, @MassimoVariolo
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.