c# - Web <table> to Excel -
i have been developing webpage allows users search items in database based on several filters/criteria. 1 of requirements ability export info spreadsheet (excel). have created simple method far utilizing csv file.
//get model... stringwriter sw = new stringwriter(); //first line column names sw.writeline("col1,col2,col3"); foreach (datatype in model) { sw.writeline(string.format({0},{1},{2}, i.data1, i.data2, i.data3)); } response.addheader("content-disposition", "attachment; filename=test.csv"); response.contenttype = "text/csv"; response.contentencoding = system.text.encoding.getencoding("utf-8"); response.write(sw); response.end();
simple enough, want more command on formatting of spreadsheet. after searching, found these documents: wikipedia & msdn describe type of xml accepted excel. i'd larn more document type, documentation seems lacking. in wiki link there details format, , there no examples in msdn article. can't cross reference 2 because seem utilize different versions (ss: workbook
vs workbook
). heck, can't find out ss:
,x:
, , c:
prefix stands for. additionally, msdn article office 2003 , can't seem find more recent version.
perhaps looking wrong thing together; there more modern way accomplish goal? sources appreciated.
jeff,
i'm not sure you're looking for, epplus can generate xlsx worksheet you. wanted provide sample instead of saying, "here utilize this".
here sample taken here: epplus source listing
excelpackage pkg = new excelpackage(); var ws = pck.workbook.worksheets.add("sheet1"); //first line column names ws.cells["a1"] = "col1"; ws.cells["a2"] = "col2"; ws.cells["a3"] = "col3"; int row = 2; foreach (datatype in model) { //i don't know info model, i'm guessing //row, col ws.cells[row, 1] = i.data1); ws.cells[row, 2] = i.data2); ws.cells[row, 3] = i.data3); row++; } pck.saveas(response.outputstream); response.contenttype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; response.addheader("content-disposition", "attachment; filename=younamegoeshere.xlsx");
hopefully along lines of need.
c# xml excel
No comments:
Post a Comment