Vivendi
Vivendi

Reputation: 21017

Reading memory stream

I'm trying to create an excel file and make that available as download. But i have some trouble with the download part.

I'm using EPPlus to create an xml file. I've got that working. But just local. I'm not sure how to force the download of the file.

This is my code:

public Stream GetXlsxDocument(IQueryable data)
{
    const string sheetName = "Sheet1";
    var localFile = new FileInfo(@"C:\test2.xlsx");
    var file = new FileInfo("test2.xlsx");
    // Used for local creation
    //ExcelPackage p = new ExcelPackage();
    MemoryStream stream = new MemoryStream();
    using (ExcelPackage p = new ExcelPackage(stream))
    {
        p.Workbook.Worksheets.Add("Sheet1");
        ExcelWorksheet ws = p.Workbook.Worksheets[1];
        ws.Name = sheetName;
        ws.Cells.Style.Font.Size = 11;
        ws.Cells.Style.Font.Name = "Calibri";
        ws.SetValue(1, 1, "aaa"); // Test data
        // Used for local creation
        //p.SaveAs(localFile);
        p.SaveAs(stream);
    }
    return stream;
}

Like i said before. Creating the xlsx file locally on my C:\ disk works. But how can i force the download of the created xlsx file?

Right now its giving me an xlsx file of 0 bytes. I'd need to return a stream which isn't empty. Anyone any idea how i can do this..??

Upvotes: 1

Views: 3211

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062895

rewind it:

stream.Position = 0;
return stream;

Upvotes: 1

Related Questions