NOTE: Refer to the online code in this article, and modify some content for me.
Development Environment: C #2005 (winform)
To export dataset to excel, you must:
- The format remains unchanged.
- The Worksheet name is the name of the able.
- Fast.
- You do not need to save the file. After exporting the file, open the excle file directly.
(If it is not saved, the newly generated process cannot be killed. Therefore, it is saved first and then opened .)
I found some code on the Internet and found this problem after trying it:
- If it is opened directly, the generated Excel process cannot be killed.
- Other normal Excel processes will be killed.
The Code is as follows:
Using Microsoft. office. interOP. excel; /// <summary> /// cause dataset to excel /// </Summary> /// <Param name = "dataset"> dataset </param> Public static void exporttoexcel (Dataset dataset) {try {// create the Excel application object // applicationclass excelapp = new applicationclass (); string outputpath = system. windows. forms. application. startuppath + @ "/EXCEL/123.xls"; excel. application excelapp = new Microsoft. office. interOP. excel. application (); excelapp. displayalerts = false; // do not prompt when saving // system. diagnostics. process xlprc; system. diagnostics. process [] xlprcarray; xlprcarray = system. diagnostics. process. getprocessesbyname ("Excel"); // determines the process system for creating an Excel file. diagnostics. process newexcelprc = xlprcarray [0]; for (INT I = 0; I <= xlprcarray. length-1; I ++) {If (xlprcarray [I]. totalprocessortime. totalseconds <newexcelprc. totalprocessortime. totalseconds) {newexcelprc = xlprcarray [I] ;}}// create a new Excel Workbook workbook excelworkbook = excelapp. workbooks. add (type. missing); int sheetindex = 0; // copy each datatable foreach (system. data. datatable DT in dataset. tables) {// copy the datatable to an object array object [,] rawdata = new object [DT. rows. count + 1, DT. columns. count]; // copy the column names to the first row of the object array for (INT Col = 0; Col <DT. columns. count; Col ++) {rawdata [0, Col] = DT. columns [col]. columnname;} // copy the values to the object array for (INT Col = 0; Col <DT. columns. count; Col ++) {// Add 'if (DT. columns [col]. datatype = system. type. getType ("system. string ") {for (int row = 0; row <DT. rows. count; row ++) {rawdata [row + 1, Col] = "'" + dt. rows [row]. itemarray [col]. tostring () ;}} else {for (int row = 0; row <DT. rows. count; row ++) {rawdata [row + 1, Col] = DT. rows [row]. itemarray [col]. tostring () ;}}// calculate the final column letter string finalcolletter = string. empty; string colcharset = "abcdefghijklmnopqrstuvwxyz"; int colcharsetlen = colcharset. length; If (DT. columns. count> colcharsetlen) {finalcolletter = colcharset. substring (DT. columns. count-1)/colcharsetlen-1, 1);} finalcolletter + = colcharset. substring (DT. columns. count-1) % colcharsetlen, 1); // create a new sheet worksheet excelsheet = (worksheet) excelworkbook. sheets. add (excelworkbook. sheets. get_item (++ sheetindex), type. missing, 1, xlsheettype. xlworksheet); excelsheet. name = DT. tablename; // fast data export to Excel String excelrange = string. format ("A1: {0} {1}", finalcolletter, DT. rows. count + 1); excelsheet. get_range (excelrange, type. missing ). value2 = rawdata; // mark the first row as bold (range) excelsheet. rows [1, type. missing]). font. bold = true;} // save and close the workbook excelworkbook. saveas (outputpath, xlfileformat. xlworkbooknormal, type. missing, type. missing, type. missing, type. missing, xlsaveasaccessmode. xlexclusive, type. missing, type. missing, type. missing, type. missing, type. missing); excelworkbook. close (true, type. missing, type. missing); excelworkbook = NULL; // release the Application Object excelapp. quit (); excelapp = NULL; // collect the unreferenced objects GC. collect (); GC. waitforpendingfinalizers (); newexcelprc. kill (); // open the exported file process. start (outputpath);} catch (exception ex) {MessageBox. show (ex. message );}}