c# - loop through Data for one or more UserIDs and create new worksheet for each User -
right code returns info database userid(s) , specific date ranges. results exported excel spreadsheet. if more 1 userid searched need have each userid have there own worksheet in workbook. right results displayed on "worksheet1". loop through results , utilize if statements check more 1 unique id? , every id new worksheet created , populated data? new programming , help great.
controller
[httppost] public fileresult export(reportphonesupportvm model) { reportphonesupportresulttypedview results = new reportphonesupportresulttypedview(); string[] userids = model.userid.split(','); foreach (string userid in userids) { int iuserid = 0; if (int32.tryparse(userid, out iuserid)) { retrievalprocedures.fetchreportphonesupportresulttypedview(results, model.fromdate, model.todate, iuserid); } } var excelresults = results; //create new excel workbook var workbook = new hssfworkbook(); //create new excel sheet var sheet = workbook.createsheet(); //(optional) set width of columns sheet.setcolumnwidth(0, 10 * 256); sheet.setcolumnwidth(1, 50 * 256); sheet.setcolumnwidth(2, 50 * 256); sheet.setcolumnwidth(3, 50 * 256); //create header row var headerrow = sheet.createrow(0); //set column names in header row headerrow.createcell(0).setcellvalue("activitydate"); headerrow.createcell(1).setcellvalue("assignment"); headerrow.createcell(2).setcellvalue("action"); headerrow.createcell(3).setcellvalue("tofrom"); headerrow.createcell(2).setcellvalue("result"); headerrow.createcell(3).setcellvalue("description"); //(optional) freeze header row not scrolled sheet.createfreezepane(0, 1, 0, 1); int rownumber = 1; //populate sheet values grid info foreach (reportphonesupportresultrow er in excelresults) { //create new row var row = sheet.createrow(rownumber++); //set values cells row.createcell(0).setcellvalue(er.activitydate); row.createcell(1).setcellvalue(er.assignment); row.createcell(2).setcellvalue(er.action); row.createcell(3).setcellvalue(er.tofrom); row.createcell(2).setcellvalue(er.result); row.createcell(3).setcellvalue(er.description); } //write workbook memory stream memorystream output = new memorystream(); workbook.write(output); //return result end user homecoming file(output.toarray(), //the binary info of xls file "application/vnd.ms-excel", //mime type of excel files "gridexcelexport.xls"); //suggested file name in "save as" dialog displayed end user }
use next code:
var excelresults = results; string userid = ""; int rownumber = 1; //create new excel workbook var workbook = new hssfworkbook(); var? sheet = null; //populate sheet values grid info foreach (reportphonesupportresultrow er in excelresults) { if (er.userid != userid) { //create new excel sheet sheet = workbook.createsheet(); //(optional) set width of columns sheet.setcolumnwidth(0, 10 * 256); sheet.setcolumnwidth(1, 50 * 256); sheet.setcolumnwidth(2, 50 * 256); sheet.setcolumnwidth(3, 50 * 256); //create header row var headerrow = sheet.createrow(0); //set column names in header row headerrow.createcell(0).setcellvalue("activitydate"); headerrow.createcell(1).setcellvalue("assignment"); headerrow.createcell(2).setcellvalue("action"); headerrow.createcell(3).setcellvalue("tofrom"); headerrow.createcell(2).setcellvalue("result"); headerrow.createcell(3).setcellvalue("description"); //(optional) freeze header row not scrolled sheet.createfreezepane(0, 1, 0, 1); userid = er.userid; rownumber = 1; } //create new row var row = sheet.createrow(rownumber++); //set values cells row.createcell(0).setcellvalue(er.activitydate); row.createcell(1).setcellvalue(er.assignment); row.createcell(2).setcellvalue(er.action); row.createcell(3).setcellvalue(er.tofrom); row.createcell(2).setcellvalue(er.result); row.createcell(3).setcellvalue(er.description); } //write workbook memory stream memorystream output = new memorystream(); workbook.write(output); //return result end user homecoming file(output.toarray(), //the binary info of xls file "application/vnd.ms-excel", //mime type of excel files "gridexcelexport.xls"); //suggested file name in "save as" dialog displayed end user
}
c# asp.net-mvc excel npoi
No comments:
Post a Comment