error in getting file using sharepoint designer 2010 programmatically
protected void updatetemplate_1() { spsite spsite = spcontext.current.site; spweb spweb = spsite.openweb(); string filepath = ""; try { spweb.allowunsafeupdates = true; splistitemcollection listitems = spweb.lists["captive report"].items; string sitecol= spsite.url.tostring().trim(); foreach(splistitem lst11 in listitems) { filepath=lst11.url.tostring().trim(); //gets site-relative url of item } spfile file = spweb.getfile(filepath); system.io.fileinfo templatefile = new system.io.fileinfo(@filepath); using (officeopenxml.excelpackage pck = new officeopenxml.excelpackage(templatefile)) //officeopenxml.excelpackage(result, source) { int count = pck.workbook.worksheets.count; response.write(count+"<br>"); byte[] filebytes = pck.getasbytearray(); //byte[] filebytes = file.openbinary(); response.clear(); response.clearcontent(); response.clearheaders(); response.cookies.clear(); //add header & other information response.cache.setcacheability(httpcacheability.private); response.cachecontrol = "private"; response.charset = system.text.utf8encoding.utf8.webname; response.contentencoding = system.text.utf8encoding.utf8; response.appendheader("content-length", filebytes.length.tostring()); response.appendheader("pragma", "cache"); response.appendheader("expires", "60"); string filename = "filename_" + datetime.now.tostring("mmm").trim();// +".xlsx"; response.appendheader("content-disposition", "attachment; " + "filename=\"" + filename + ".xlsx\"; " //filename of excel ); response.contenttype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; //write client response.binarywrite(filebytes); } } catch (exception ex) { response.write(ex.tostring()); }
}
the workbook trying fetch has 3 worksheets,still giving worksheet count 0 (0), , hence while converting using getasbytearray, showing error :
system.invalidoperationexception: workbook must contain @ least 1 worksheet @ officeopenxml.excelworkbook.save() @ officeopenxml.excelpackage.getasbytearray(boolean save) at
Comments
Post a Comment