gridview - How to export grid view to excel 2007 and above C# -
i'm trying export grid view excel 2007 , above file using excel package library on every time run export function file empty. i'm working on old code implemented else. @ first converting list anonymous object type that:
var exportdata = onpremisestransactionspagemodel.inclinictransactionslist.select(intrans => new { billingid = intrans.financialtransactionid == 0 ? string.empty : "tr" + intrans.financialtransactionid, appointmentdate = intrans.appointmentdatetime == datetime.minvalue ? string.empty : intrans.appointmentdatetime.tostring("mm/dd/yyyy, hh:mm tt"), amount = intrans.amount == 0 ? string.empty : intrans.amount.tostring("c"), paymenttype = intrans.amount == 0 ? string.empty : ((intrans.paymentmethodtypeid == convert.toint32(preferredinclinicpaymentoption.cash)) ? intrans.paymentmethodtypename : intrans.paymentmethodprovidername), receivername = string.isnullorwhitespace(intrans.receivername) ? string.empty : intrans.receivername }).tolist();
and binding grid view this:
string[] columnheaders= { resource.getstring("id"), resource.getstring("transactiondatetime"), resource.getstring("amount"), resource.getstring("transactiondescription") }; gridview.datasource = source; gridview.rowdatabound += ((sender, e) => customizegridcolumnheaders(sender, e, columnheaders)); gridview.databind(); void customizegridcolumnheaders(object sender, gridviewroweventargs e, string[] columnheaders) { if ((e.row.rowtype == datacontrolrowtype.header) && ((columnheaders != null) && (columnheaders.length > 0))) { (int idx = 0, length = e.row.cells.count; idx < length; idx++) { if (idx >= columnheaders.count()) { continue; } e.row.cells[idx].text = columnheaders[idx]; } } }
after using following code generate 97-2003 excel file , need generate file on 2007 or above versions
this implemented code previous team:
httpcontext curcontext = httpcontext.current; curcontext.response.clear(); curcontext.response.addheader("content-disposition", "attachment;filename=inclinictransactions.xls"); curcontext.response.charset = "utf-8"; curcontext.response.cache.setcacheability(httpcacheability.nocache); curcontext.response.contenttype = "application/vnd.ms-excel"; ////convert rendering of gridview string representation stringwriter sw = new stringwriter(); system.web.ui.htmltextwriter htw = new htmltextwriter(sw); gridview.rendercontrol(htw); ////write response curcontext.response.write(sw.tostring()); curcontext.response.flush(); curcontext.response.end();
and i'm trying this:
httpcontext curcontext = httpcontext.current; curcontext.response.clear(); curcontext.response.addheader("content-disposition", "attachment;filename=inclinictransactions.xlsx"); curcontext.response.charset = "utf-8"; curcontext.response.cache.setcacheability(httpcacheability.nocache); curcontext.response.contenttype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; datatable dt = new datatable(); (int idx = 0; idx <= gridview.headerrow.cells.count - 1; idx++) { dt.columns.add(gridview.headerrow.cells[idx].text.tostring()); } (int = 0; < gridview.rows.count; i++) { datarow dr = dt.newrow(); (int j = 0; j <= gridview.rows[i].cells.count - 1; j++) { if (!string.isnullorempty(gridview.rows[i].cells[j].text.tostring())) { dr[j] = gridview.rows[i].cells[j].text.tostring(); } } dt.rows.add(dr); } using (excelpackage pck = new excelpackage()) { excelworksheet wsdt = pck.workbook.worksheets.add("sheet1"); wsdt.cells["a1"].loadfromdatatable(dt, true, tablestyles.none); wsdt.cells[wsdt.dimension.address].autofitcolumns(); curcontext.response.binarywrite(pck.getasbytearray()); } curcontext.response.flush(); curcontext.response.end();
but on every time run function i'm getting file empty, don't know mistake , if can me please solve issue, , in advance.
Comments
Post a Comment