c# - Download file with ClosedXML -
all
how can download file user sees downloading (like stream?)
i using closedxml, if use saveas method, have give hard-coded url, , if give file name not automatically download download folder.
the method below works great, have create own excel file, based upon html, , file grows way large should, when use closedxml file 50% or less size of code below: however, download behaviour how be.
is there way can convert code below can give 'workbook' object, , downloads workbook?
httpcontext.current.response.appendheader("content-disposition","attachment;filename=excel.xls"); httpcontext.current.response.charset ="utf-8"; httpcontext.current.response.contentencoding=system.text.encoding.default; httpcontext.current.response.contenttype = "application/ms-excel"; ctl.page.enableviewstate =false; system.io.stringwriter tw = new system.io.stringwriter() ; system.web.ui.htmltextwriter hw = new system.web.ui.htmltextwriter (tw); ctl.rendercontrol(hw); httpcontext.current.response.write(tw.tostring()); httpcontext.current.response.end();
thanks
the saveas()
method supports stream, closedxml workbook stream use:
public stream getstream(xlworkbook excelworkbook) { stream fs = new memorystream(); excelworkbook.saveas(fs); fs.position = 0; return fs; }
and downloading file:
string myname = server.urlencode(reportname + "_" + datetime.now.toshortdatestring() + ".xlsx"); memorystream stream = getstream(excelworkbook); response.clear(); response.buffer = true; response.addheader("content-disposition", "attachment; filename=" + myname); response.contenttype = "application/vnd.ms-excel"; response.binarywrite(stream.toarray()); response.end();
Comments
Post a Comment