C # operation instance code for Excel DataTable conversion

Source: Internet
Author: User
Load Excel Public DataSet loaddatafromexcel (string filePath) {try {                 String strconn; strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + FilePath + "; Extended properties= ' Excel 8.0;                 Hdr=false;imex=1 ' "; strconn = string. Format ("Provider=microsoft.ace.oledb.12.0;data source={0}; Extended properties= ' Excel 8.0;                 hdr=yes;imex=1; ' ", FilePath);                 OleDbConnection oleconn = new OleDbConnection (strconn);                 Oleconn.open (); String sql = "SELECT * FROM [sheet1$]";//change sheet name, such as Sheet2, etc. oledbdataadapter oledaexcel = new OLE DB                 DataAdapter (SQL, oleconn);                 DataSet oledsexcle = new DataSet ();                 Oledaexcel.fill (oledsexcle, "Sheet1");                 Oleconn.close ();             return oledsexcle;             } catch (Exception err) {return null;      }         }   <summary>//DataTable directly export Excel, this method will open the DataTable data in Excel, and then manually to save to the exact location///</summary&gt         ; <param name= "DT" > to export Excel datatable</param>///<returns></returns> public Boo L Doexport (System.Data.DataTable dt) {Microsoft.Office.Interop.Excel.Application app = new Applicatio             NClass ();             if (app = = null) {throw new Exception ("Excel cannot Start"); } app.             Visible = true; Workbooks WBS = App.             Workbooks; Workbook wb = WBS.             ADD (Missing.Value); Worksheet ws = (Worksheet) WB.             WORKSHEETS[1]; int cnt = dt.             Rows.Count; int columncnt = dt.             Columns.count;  Get Data ******************** object[,] objdata = new object[cnt + 1, columncnt]; Create cache data//Get column header for (int i = 0; i < columncnt; i++) {objdata[0, I] = dt. Columns[i].             ColumnName; }//Get specific data for (int i = 0; i < cnt; i++) {System.Data.DataRow dr = dt.                 Rows[i];                 for (int j = 0; J < columncnt; J + +) {Objdata[i + 1, j] = Dr[j]; }}//********************* Write excel****************** Range r = Ws.get_range (app. Cells[1, 1], app.             cells[cnt + 1, columncnt]);             R.numberformat = "@";             R = R.get_resize (cnt+1, columncnt);             R.value2 = Objdata;             R.entirecolumn.autofit ();             App = null;         return true; }
Related Article

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.