asp.net - GridView to Excel using EPPlus -
i'm trying create excel sheet using epplus's library. however, output excel file not relate cells of representing numbers.
the code i'm using is:
using (var pck = new excelpackage()) { excelworksheet ws = pck.workbook.worksheets.add(string.isnullorempty(spreadsheetname) ? "report" : spreadsheetname); ws.cells["b2"].loadfromdatatable(gridviewtable, true, officeopenxml.table.tablestyles.light1); (int = 1; <= gridviewtable.columns.count; i++) { ws.column(i).autofit(); } // ************** // header // ************** //prepare range column headers string cellrange = "b2:" + convert.tochar('b' + gridviewtable.columns.count - 1) + 2; //format header columns using (excelrange rng = ws.cells[cellrange]) { rng.style.wraptext = false; rng.style.horizontalalignment = excelhorizontalalignment.center; rng.style.font.bold = true; rng.style.fill.patterntype = excelfillstyle.solid; //set pattern background solid rng.style.fill.backgroundcolor.setcolor(colortranslator.fromhtml("#007a99")); rng.style.font.color.setcolor(color.white); } // ************ // data // ************ //prepare range rows string rowscellrange = "b3:" + convert.tochar('b' + gridviewtable.columns.count - 1) + (gridviewtable.rows.count + 1); //format rows using (excelrange rng = ws.cells[rowscellrange]) { rng.style.wraptext = false; rng.style.horizontalalignment = excelhorizontalalignment.left; rng.style.fill.patterntype = excelfillstyle.solid; //set pattern background solid rng.style.fill.backgroundcolor.setcolor(colortranslator.fromhtml("#b2d1f0")); rng.style.font.color.setcolor(color.black); } response.clearheaders(); response.clearcontent(); response.contenttype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; response.addheader("content-disposition", "attachment; filename=" + (string.isnullorempty(filename) ? "report" : filename) + ".xlsx"); response.binarywrite(pck.getasbytearray()); }
does might know why happening ?
i got it.
if use loadfromdatatable() method, datatable object should typed in columns, meaning should create columns using table.columns.add(columnname, columntype);
Comments
Post a Comment