C #. NET import the array to the Excel source code

Source: Internet
Author: User

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 ..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.