c# - How to write data on new sheet of EXCEL -
i have mysql procedure return 5 tables, need set tables single excel files 5 different sheets. using vs 2010, jquery, asp.net. how write table's in excel file on new sheet.
$("#btnexcel").click(function (e) { $('#divexcelexporting').html($('#containerone').html()); $('#divexcelexporting').append($('#containertwo').html()); $('#divexcelexporting').append($('#containerthree').html()); $('#divexcelexporting').append($('#containerfour').html()); $('#divexcelexporting').append($('#containerfive').html()); var trcoll = $('#divexcelexporting').find('.border-middle1').find('tr'); $.each(trcoll, function (d, f) { $($(this).find('td')[0]).remove(); }); var trcol2 = $('#divexcelexporting').find('.border-middle2').find('tr'); $.each(trcol2, function (d, f) { $($(this).find('td')[0]).remove(); }); var trcol3 = $('#divexcelexporting').find('.border-middle3').find('tr'); $.each(trcol3, function (d, f) { $($(this).find('td')[0]).remove(); }); var trcol4 = $('#divexcelexporting').find('.border-middle4').find('tr'); $.each(trcol4, function (d, f) { $($(this).find('td')[0]).remove(); }); var trcol5 = $('#divexcelexporting').find('.border-middle5').find('tr'); $.each(trcol5, function (d, f) { $($(this).find('td')[0]).remove(); }); webservice.setvendorhtml($('#divexcelexporting').html(), onwsrequestcomplete);
});
i got solution openxml.
string path = context.server.mappath("~/exceldata/test.xslx"); exportdataset(ds, path); private void exportdataset(dataset ds, string destination) { using (var workbook = spreadsheetdocument.create(destination, documentformat.openxml.spreadsheetdocumenttype.workbook)) { var workbookpart = workbook.addworkbookpart(); workbook.workbookpart.workbook = new documentformat.openxml.spreadsheet.workbook(); workbook.workbookpart.workbook.sheets = new documentformat.openxml.spreadsheet.sheets(); foreach (system.data.datatable table in ds.tables) { var sheetpart = workbook.workbookpart.addnewpart<worksheetpart>(); var sheetdata = new documentformat.openxml.spreadsheet.sheetdata(); sheetpart.worksheet = new documentformat.openxml.spreadsheet.worksheet(sheetdata); documentformat.openxml.spreadsheet.sheets sheets = workbook.workbookpart.workbook.getfirstchild<documentformat.openxml.spreadsheet.sheets>(); string relationshipid = workbook.workbookpart.getidofpart(sheetpart); uint sheetid = 1; if (sheets.elements<documentformat.openxml.spreadsheet.sheet>().count() > 0) { sheetid = sheets.elements<documentformat.openxml.spreadsheet.sheet>().select(s => s.sheetid.value).max() + 1; } documentformat.openxml.spreadsheet.sheet sheet = new documentformat.openxml.spreadsheet.sheet() { id = relationshipid, sheetid = sheetid, name = table.tablename }; sheets.append(sheet); documentformat.openxml.spreadsheet.row headerrow = new documentformat.openxml.spreadsheet.row(); list<string> columns = new list<string>(); foreach (system.data.datacolumn column in table.columns) { columns.add(column.columnname); documentformat.openxml.spreadsheet.cell cell = new documentformat.openxml.spreadsheet.cell(); cell.datatype = documentformat.openxml.spreadsheet.cellvalues.string; cell.cellvalue = new documentformat.openxml.spreadsheet.cellvalue(column.columnname); headerrow.appendchild(cell); } sheetdata.appendchild(headerrow); foreach (system.data.datarow dsrow in table.rows) { documentformat.openxml.spreadsheet.row newrow = new documentformat.openxml.spreadsheet.row(); foreach (string col in columns) { documentformat.openxml.spreadsheet.cell cell = new documentformat.openxml.spreadsheet.cell(); cell.datatype = documentformat.openxml.spreadsheet.cellvalues.string; cell.cellvalue = new documentformat.openxml.spreadsheet.cellvalue(dsrow[col].tostring()); // newrow.appendchild(cell); } sheetdata.appendchild(newrow); } } } }
references :
using documentformat.openxml.packaging; using documentformat.openxml.spreadsheet; using documentformat.openxml;
Comments
Post a Comment