how to convert created excel file using closed xml into bytes format

只谈情不闲聊 提交于 2019-12-07 01:53:33

Closed XML workbooks are saved to a stream. You can use a memory stream. Then call MemoryStream.ToArray() to get its bytes. So...

var wb = new XLWorkbook();
//...
var workbookBytes = new byte[0];
using (var ms = new MemoryStream())
{
    wb.SaveAs(ms);
    workbookBytes = ms.ToArray();
}
kaushik0033

Please do this:-

//Open the File into file stream
FileStream fileStream = new FileStream(Server.MapPath(fileName), FileMode.Open, FileAccess.Read, FileShare.Read);

//Create and populate a memorystream with the contents of the
MemoryStream mstream = StreamToMemory(fileStream);

// delete the file when it is been added to memory stream
File.Delete(Server.MapPath(fileName));

//Convert the memorystream to an array of bytes.
byte[] byteArray = mstream.ToArray();

//Clean up the memory stream
mstream.Flush();
mstream.Close();

// Clear all content output from the buffer stream
Response.Clear();

// Add a HTTP header to the output stream that specifies the default filename
// for the browser's download dialog
Response.AddHeader("Content-Disposition", "attachment; filename=QatargasTimesheet-" + ((DateTime)rdDate.SelectedDate).ToString("MMM yyyy") + ".xls");

// Add a HTTP header to the output stream that contains the 
// content length(File Size). This lets the browser know how much data is being transfered
Response.AddHeader("Content-Length", byteArray.Length.ToString());

// Set the HTTP MIME type of the output stream
Response.ContentType = "application/octet-stream";

// Write the data out to the client.
Response.BinaryWrite(byteArray);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!