skip to Main Content

I have very simple example to export some data to Excel file, in some reason I see the bytes in the response but no file downloaded why?

public async Task<IActionResult> exportRecordsToExcel()
{
    var file = await ServiceRequestBL.ExportFO_SrviceRequestToExcel();
  
    return file;
}    

public async Task<FileStreamResult> ExportFO_SrviceRequestToExcel()
{
    DataTable dt = new DataTable("Grid");
    dt.Columns.AddRange(new DataColumn[13] {
        new DataColumn("1"), new DataColumn("2"), new DataColumn("g"), new DataColumn("j"), new DataColumn("k"), new DataColumn("l"), new DataColumn("x"), new DataColumn("m"), new DataColumn("9"), new DataColumn("8"), new DataColumn("7"), new DataColumn("6"), new DataColumn("5")});
    byte[] data = null;
    using (MemoryStream stream = new MemoryStream())
    {
        IFormatter bf = new BinaryFormatter();
        dt.RemotingFormat = SerializationFormat.Binary;
        bf.Serialize(stream, dt);
        data = stream.ToArray();
    }
    var memoryStream = new MemoryStream();
    memoryStream.Seek(0, SeekOrigin.Begin);

    string filename = "Report.xlsx";

    return new FileStreamResult(memoryStream, "application/ms-excel") { FileDownloadName = filename };
}

2

Answers


  1. You do not need a using block when using FileStreamResult, as this wrapper will take care of disposing the stream when it is no longer needed (it already uses using internally).

    Simply serialize your data into a stream with Serialize(stream, ...) as you have, and pass that stream over to FileStreamResult. Let it take of care of the rest.

    Login or Signup to reply.
  2. You are passing an empty MemoryStream rather than the one you stored the data in

    public Task<FileStreamResult> ExportFO_SrviceRequestToExcel()
    {
        var dt = new DataTable("Grid")
        {
            RemotingFormat = SerializationFormat.Binary,
            Columns =
            {
                {"1"}, {"2"}, {"g"}, {"j"}, {"k"}, {"l"}, {"x"},
                {"m"}, {"9"}, {"8"}, {"7"}, {"6"}, {"5"}
            },
        };
        var stream = new MemoryStream();
        var bf = new BinaryFormatter();
        bf.Serialize(stream, dt);
        memoryStream.Seek(0, SeekOrigin.Begin);
    
        string filename = "Report.xlsx";
    
        return Task.FromResult(new FileStreamResult(memoryStream, "application/ms-excel") { FileDownloadName = filename });
    }
    

    The stream does not need a using because FileStreamResult will dispose it, and in any case it’s a MemoryStream which is backed only by an array.

    Also, in this case there is no need for async as you are not awaiting anything.

    I note that BinaryFormatter is deprecated and has security issues. Consider using another serializer if possible.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search