sql - Get column names from my dataset -



sql - Get column names from my dataset -

my code here loops through client , exports result excel document multiple sheets, depending on how many services/reports client has. want column names , print them out gridview. here total code run client , export excel file (which works fine). seem totally lost here , need help.

var custname = convert.tostring(request.querystring["cusname"]); seek { dataset dt = new dataset(); sqlconnection connection = new sqlconnection("data source=ap;initial catalog=db_inf;user id=sa;password=*****;app=entityframework"); using (sqlcommand sqlcmd = new sqlcommand("info.dbo.sp_accounting_getall_services", connection)) { sqlcmd.commandtype = commandtype.storedprocedure; sqlcmd.parameters.addwithvalue("@customername", sqldbtype.nvarchar).value = custname; using (sqldataadapter sqlda = new sqldataadapter(sqlcmd)) { session["tasktable"] = dt; sqlda.fill(dt); if (dt.tables.count > 0) { memorystream ms = new memorystream(); int = 1; using (excelpackage bundle = new excelpackage(ms)) { string filename = server.mappath("") + @"\accounting_templates\" + custname + ".xlsx"; fileinfo inputfile = new fileinfo(filename); excelpackage summarytemplate = new excelpackage(inputfile); foreach (excelworksheet sheet in summarytemplate.workbook.worksheets) { package.workbook.worksheets.add(sheet.name, sheet); } foreach (datatable table in dt.tables) { var columns = new list<string>(); excelworksheet worksheet = package.workbook.worksheets.tolist().where(z => z.name == string.format("report{0}", i)).firstordefault(); if (worksheet != null) { worksheet.cells["a1"].loadfromdatatable(table, true); worksheet.cells.autofitcolumns(); } i++; } response.clear(); package.saveas(response.outputstream); response.addheader("content-disposition", "attachchment; filename=" + custname + ".xls"); response.charset = ""; response.contenttype = "application/vnd.xls"; response.end(); } } } } } grab (exception) { page.clientscript.registerstartupscript(gettype(), "msgbox", "alert('something wen't wrong');", true); }

excelworksheet worksheet = package.workbook.worksheets.tolist().where(z => z.name == string.format("report{0}", i)).firstordefault(); if (worksheet != null) { (int colno = 0; colno < table.columns.count; colno++) { worksheet.cells[this.colnumbertoname(colno) + "1"] = table.columns[colno].name; } worksheet.cells["a2"].loadfromdatatable(table, true); worksheet.cells.autofitcolumns(); } i++;

note loadfromdatatable goes in "a2", not "a1".

i leave implement private string colnumbertoname(int colno). translates number 0 on a, b, c, etc.

sql asp.net dataset

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -