I am working on a small tool recently. the two-dimensional array and staggered array data calculated in. NET are imported into the Excel file and searched in the blog garden. Many predecessors have already pasted well-developed code. I made some improvements and shared them again. The source of the code below is unknown due to chaos (only in the blog Park). Thank you for that. Of course, you must first apply Interop. Excel.
Using Excel;
......
Code /// <summary> /// convert a one-dimensional array to an Excel file: // currently, the supported data types include DataTable, two-dimensional array, two-dimensional staggered array, and DataGridView, arrayList /// </summary> /// <param name = "data"> data Object </param> /// <param name = "xlsSaveFileName"> Save as Excel file name </param> // <returns> whether the file is successfully saved </returns> public static bool ConvertDataToExcel <T> (T [] data, string xlsSaveFileName) {// Add Excl to the reference before doing this !! Excel. application excel = new Excel. application (); // if the system is Excl2007, the added reference will be different // The Code is as follows: Microsoft. office. interop. excel. application excel // = new Microsoft. office. interop. excel. application (); if (excel = null) {return false;} else {try {Excel. workbook xlBook = excel. workbooks. add (true); Excel. worksheet xlSheet = (Excel. worksheet) xlBook. worksheets [1]; // import data for (int I = 0; I <data. length; I ++) {if (data [I]! = Null) {excel. cells [1, I + 1] = data [I]. toString (). trim () ;}} xlBook. saved = true; // save the file xlBook. saveCopyAs (xlsSaveFileName); return true;} catch {return false;} finally {// close an excel object. Otherwise, many excel processes exist in the memory. quit (); excel = null; GC. collect ();}}} /// <summary> /// import two-dimensional array data to Excel /// </summary> /// <param name = "data"> data </param> /// <param name = "xlsSaveFileName"> saved file name (PATH) </param> // <returns> whether the storage is successful </returns> publi C static bool ConvertDataToExcel <T> (T [,] data, string xlsSaveFileName) {// Add Excl to reference before doing this !! Excel. application excel = new Excel. application (); // if the system is Excl2007, the added reference will be different. The Code is as follows: Microsoft. office. interop. excel. application excel = new Microsoft. office. interop. excel. application (); if (excel = null) {return false;} else {Excel. workbook xlBook = excel. workbooks. add (true); Excel. worksheet xlSheet = (Excel. worksheet) xlBook. worksheets [1]; int rows = data. getLength (0); // The row int cols = data. getLength (1); // column // Import Data for (int I = 0; I <rows; I ++) {for (int j = 0; j <cols; j ++) {if (data [I, j]! = Null) {excel. cells [I + 1, j + 1] = data [I, j]. toString (). trim () ;}}try {xlBook. saved = true; xlBook. saveCopyAs (xlsSaveFileName); return true;} catch {return false;} finally {excel. quit (); excel = null; GC. collect ();}}} /// <summary> /// import the two-dimensional staggered array data to Excel. // </summary> /// <param name = "data"> data </ param> /// <param name = "xlsSaveFileName"> saved file name (PATH) </param> // <returns> whether the file is successfully saved </returns> public static bool Convert DataToExcel <T> (T [] [] data, string xlsSaveFileName) {// Add Excl to reference before doing this !! Excel. application excel = new Excel. application (); // if the system is Excl2007, the added reference will be different. The Code is as follows: Microsoft. office. interop. excel. application excel = new Microsoft. office. interop. excel. application (); if (excel = null) {return false;} else {Excel. workbook xlBook = excel. workbooks. add (true); Excel. worksheet xlSheet = (Excel. worksheet) xlBook. worksheets [1]; int rows = data. getLength (0); // row // int cols = data. getLength (1); // column // Import data for (int I = 0; I <rows; I ++) {for (int j = 0; j <data [I]. length; j ++) {if (data [I] [j]! = Null) {excel. cells [I + 1, j + 1] = data [I] [j]. toString (). trim () ;}}try {xlBook. saved = true; xlBook. saveCopyAs (xlsSaveFileName); return true;} catch {return false;} finally {excel. quit (); excel = null; GC. collect ();}}}
I heard that there is a better Excel read and write component NPOI. Go back and have a try ..