当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - 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..??

网友答案:

rewind it:

stream.Position = 0;
return stream;
分享给朋友:
您可能感兴趣的文章:
随机阅读: