c# - EPPlus not formatting last record -
i'm using helper below export data our website simple excel spreadsheet. use createandformateworksheet
method to... create , format worksheet datetime
data types preserved leading zeros.
this works great, fomatting isn't being applied last row of spreadsheet.
i've done research , can't find on topic, i'm thinking must have setting either out of place or used incorrectly. ideas?
public static void exportdata<t>(httpresponsebase response, list<t> dataset, string filename) { var pck = new officeopenxml.excelpackage(); var ws = createandformatworksheet(pck, dataset); response.clear(); response.contenttype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; response.addheader("content-disposition", "attachment; filename=" + filename + ".xlsx"); response.binarywrite(pck.getasbytearray()); response.flush(); response.end(); } private static excelworksheet createandformatworksheet<t>(officeopenxml.excelpackage pck, list<t> dataset) { excelworksheet ws = pck.workbook.worksheets.add("monet_export_" + datetime.now.tostring()); ws.cells["a1"].loadfromcollection(dataset, true); dynamic first = dataset[0]; string rowcount = dataset.count.tostring(); int = 0; foreach (propertyinfo info in typeof(t).getproperties()) { string column = columnindex[i]; string indexer = column + "2:" + column + rowcount; if (info.propertytype == typeof(datetime)) { ws.cells[indexer].style.numberformat.format = "mm/dd/yyyy"; } else { // treat other column text preserve leading 0's ws.cells[indexer].style.numberformat.format = "@"; } i++; } return ws; }
here screen shot of spreadsheet. notice datetime
columns fomatted correctly in first 2 rows, incorrectly in last.
remember .loadfromcollection(dataset, true);
, true
boolean tells included header row makes table height = rowcount + 1 header line. change it:
//+1 header line string rowcount = (dataset.count + 1).tostring();