Download Excel File generated.

Hi, how are you? I hope that you are good. I need your help to the next trouble:

Using the syncfusion library could generate a excel file that return FileStreamResult in a function, but I can´t download the excel generated from the browser. I tried many ways but i failed.

As detail, i don't like use javascript, jquery or other frontend library to download the excel generated.


I put my Controller and Service code to generate the Excel

Controller code:

public async Task ExportarTransaccionesBritech(int idEstadoBritech)

{

return await _TBService.ExportarTransaccionesBritech(idEstadoBritech);

}


Service code:

public FileStreamResult ExportarExcelTransaccionesBritech(List<object> transacciones, string nomEstado)

{

//Initialize ExcelEngine.

using var excelEngine = new ExcelEngine();

//Initialize Application.

IApplication application = excelEngine.Excel;

//Set the default version as Xlsx.

application.DefaultVersion = ExcelVersion.Xlsx;

//Create a new workbook with one worksheet.

IWorkbook workbook = application.Workbooks.Create(1);

//Get the worksheet into IWorksheet.

IWorksheet worksheet = workbook.Worksheets[0];


IStyle headerStyle = workbook.Styles.Add("HeaderStyle");

headerStyle.BeginUpdate();

headerStyle.Font.Bold = true;

headerStyle.EndUpdate();


//Set a value to Excel cell.

worksheet.Range["A2"].Value = "REPORTE DE TRANSACCIONES BRITECH";

worksheet.Range["A2:F2"].Merge();

worksheet.Range["A2:F2"].CellStyle = headerStyle;

worksheet.Range["A2:F2"].CellStyle.Font.Size = 14.0f;

worksheet.Range["A3"].Value = "ESTADO:";

worksheet.Range["A3"].CellStyle = headerStyle;

worksheet.Range["B3"].Value = nomEstado;

worksheet.Range["B3"].CellStyle = null;

worksheet.Range["A5"].Value = "CÓDIGO/FONDO INVERSION";

worksheet.Range["A5"].ColumnWidth = 10;

worksheet.Range["B5"].ColumnWidth = 40;

worksheet.Range["A5:B5"].Merge();

worksheet.Range["A5:B5"].CellStyle = headerStyle;

worksheet.Range["C5"].Value = "CÓDIGO/VEHICULO INVERSION";

worksheet.Range["C5"].ColumnWidth = 10;

worksheet.Range["D5"].ColumnWidth = 40;

worksheet.Range["C5:D5"].Merge();

worksheet.Range["C5:D5"].CellStyle = headerStyle;

worksheet.Range["E5"].Value = "FEC. VENCIMIENTO";

worksheet.Range["E5"].ColumnWidth = 20;

worksheet.Range["E5"].CellStyle = headerStyle;

worksheet.Range["F5"].Value = "NETO";

worksheet.Range["F5"].ColumnWidth = 10;

worksheet.Range["F5"].CellStyle = headerStyle;

worksheet.ImportData(transacciones, 6, 1, false);

var stream = new MemoryStream();

//Saving the Excel to the MemoryStream

workbook.SaveAs(stream);

//Set the position as '0'.

stream.Position = 0;


//Download the Excel file in the browser

var fileStreamResult = new FileStreamResult(stream, "application/excel")

{

FileDownloadName = "TransaccionesBritech_" + DateTime.Now.ToString("yyyy-MM-dd") + ".xlsx"


};

return fileStreamResult;

}

If you can answer me, I will glad you.

Greetings.

Juandy.


PD: I forgot to say that I use ASP.NET Core 3 to the code.


1 Reply

RS Ramya Sivakumar Syncfusion Team July 15, 2022 09:12 AM UTC

Hi Juandy,


Greetings from Syncfusion.


We have prepared the sample to create and download the Excel file. Kindly try this and let us know if it helps.

Sample link - https://www.syncfusion.com/downloads/support/directtrac/general/ze/F-1762221316231856


Please refer the following link to know more about how to create and edit Excel file.

https://help.syncfusion.com/file-formats/xlsio/create-read-edit-excel-files-in-asp-net-core-c-sharp


Regards,

Ramya.


Loader.
Up arrow icon