rabanti-github / PicoXLSX

PicoXLSX is a small .NET / C# library to create XLSX files (Microsoft Excel 2007 or newer) in an easy and native way
MIT License
52 stars 13 forks source link

Webapi how return workbook #17

Closed visualczm closed 2 years ago

visualczm commented 2 years ago

Hi, I'm come in , again~ See title,thank you for your help~

rabanti-github commented 2 years ago

Hi, Can you quickly describe a little bit more, what you want to do? Do you try to create workbooks on a web server (e.g. ASP.NET) and then to provide them by browser download? Thank you in advance for the information.

visualczm commented 2 years ago

hi, Do you try to create workbooks on a web server (e.g. ASP.NET) and then to provide them by browser download? Yeah,but is .net5 webapi in web server ,i want return picoxlsx create file. how did you do that?

rabanti-github commented 2 years ago

Unfortunately, until now, I have not really worked with web servers in .net (had more to deal with Node and recently Java). However, from my Java experience, I could imagine that it should work like that:

Maybe I can do some research for this topic in the evening. I hope this helps in the meantime.

rabanti-github commented 2 years ago

Maybe this could be the solution (if it is the right .net version): https://codeburst.io/download-files-using-web-api-ae1d1025f0a9 In this case, you could convert the MemoryStream into a byte array (https://docs.microsoft.com/en-us/dotnet/api/system.io.memorystream.toarray?view=net-5.0) and define the MIME type, as earlier described:

MemoryStream ms = new MemoryStream();
workbook.SaveAsStream(ms, true);
ms.Seek(0, SeekOrigin.Begin); // not sure whether this is necessary
byte[] bytes = ms.ToArray();
return File(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "workbook.xlsx");

Disclaimer: This is not tested code (just written down here in this comment)

I hope, this helps

visualczm commented 2 years ago

Thank you so much~ Is worked