Srikanth Reddy
Srikanth Reddy

Reputation: 545

Download Excel File in .net core web api

I'm trying to create & download the excel file in asp .net core web api. Whenever I hit the service from postman or Advanced Rest Client, Instead of downloading the file I'm getting the JSON data of the file content as response. My code is like below :

        [HttpGet]
        [Route("GetLogsPdfORExcel")]
        public IActionResult GetLogsPdfORExcel()
        {


            var result = _dataContext.GetLogsPdfORExcel();
            if (result != null)
            {
                this.ExportExcel(result);
            }

            return Ok(result);
        }

        public FileStreamResult ExportExcel(DataTable dt)
        {
            string excelName = "LogsRecord";

            ExcelPackage package = new ExcelPackage();
            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("sheet1");
            int currentRowNo = 5;
            int totalRows = dt.Rows.Count;
            int k = 0;
            foreach (DataColumn column in dt.Columns)
            {
                worksheet.Cells[currentRowNo, k + 1].Value = column.ColumnName;
                k++;
            }
            currentRowNo++;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    worksheet.Cells[currentRowNo, j + 1].Value = Convert.ToString(dt.Rows[i][j]);
                }
                currentRowNo++;
            }

            int columnCount = dt.Columns.Count;
            for (int i = 1; i <= columnCount; i++)
                worksheet.Column(i).AutoFit();
            worksheet.Row(1).Height = 55;
            worksheet.SelectedRange["A1"].Style.Font.Size = 14;
            worksheet.Row(5).Style.Font.Bold = true;



            using (var memoryStream = new MemoryStream())
            {
                memoryStream.Position = 0;
                var contentType = "application/octet-stream";
                var fileName = "fileName.xlsx";
                return File(memoryStream, contentType, fileName);
            }
        }

Here whenever I hit my service, I need directly my file needs to get download.

Upvotes: 1

Views: 15973

Answers (1)

Nkosi
Nkosi

Reputation: 247088

Return the file stream result and not the datatable.

[HttpGet]
[Route("GetLogsPdfORExcel")]
public IActionResult GetLogsPdfORExcel() {
    var result = _dataContext.GetLogsPdfORExcel();
    if (result != null) {
        return this.ExportExcel(result);
    }
    return BadRequest(); //Or some other relevant response.
}

Also no need to dispose of the stream since the FileStreamResult will dispose of it when done

//...

var memoryStream = new MemoryStream();
using (var package = new ExcelPackage(memoryStream)) { //<<< pass stream

    //...populate package

    package.Save();
}

memoryStream.Position = 0;
var contentType = "application/octet-stream";
var fileName = "fileName.xlsx";
return File(memoryStream, contentType, fileName);

Upvotes: 1

Related Questions