Using Excel = Microsoft. Office. InterOP. Excel;
Using system. reflection;
Int modelsheetheadrows = 1; excel. application Excel = NULL; try {Excel = new excel. application (); string newmodel = tbmodel. text. replace (". "," merge. "); excel. visible = true; file. copy (tbmodel. text, newmodel, true); excel. workbook modelworkbook = excel. workbooks. open (newmodel, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing); For (INT I = 0; I <listviewsel. items. count; I ++) {string pathname = listviewsel. items [I]. subitems [2]. text + "\" + listviewsel. items [I]. subitems [1]. text; excel. workbook tmpworkbook = excel. workbooks. open (pathname, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing); For (Int J = 1; j <= tmpworkbook. sheets. count; j ++) {excel. worksheet tmpsheet = (Excel. worksheet) tmpworkbook. sheets [J]; If (modelworkbook. sheets. count <j) {modelworkbook. sheets. add (missing. value);} excel. worksheet modelsheet = (Excel. worksheet) modelworkbook. sheets [J]; if (I = 0) {modelsheetheadrows = modelsheet. usedrange. cells. rows. count;} int tmpsheetrows = tmpsheet. usedrange. cells. rows. count; int tmpsheetcolumns = tmpsheet. usedrange. cells. columns. count; int modelsheetrows = modelsheet. usedrange. cells. rows. count; tmpsheet. get_range (Excel. range) tmpsheet. cells [modelsheetheadrows + 1, 1], (Excel. range) tmpsheet. cells [tmpsheetrows, tmpsheetcolumns]). copy (modelsheet. get_range (Excel. range) modelsheet. cells [modelsheetrows + 1, 1], (Excel. range) modelsheet. cells [tmpsheetrows + modelsheetrows-1, tmpsheetcolumns]);} tmpworkbook. close (); modelworkbook. save ();} modelworkbook. close ();} catch (exception ee) {MessageBox. show ("[Error]:" + ee. message, "message prompt");} finally {pathtb. TEXT = NULL; tbmodel. TEXT = NULL; listview. items. clear (); listviewsel. items. clear (); excel. quit (); MessageBox. show ("merged ");}