C# Excel 数据导入mysql数据库

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Addmysql
{
    public class ExcelToTable
    {
        ///   
        ///  读取excel文件内容并存放在DataSet中.  
        ///   
        /// 返回DataSet对象  
        public DataSet ExcelToDS(string path, out string err)
        {
            DataSet ds = null;
            err = "";
            try
            {
                //string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
                //    + "Data Source=" + @path + ";" + "Extended Properties=Excel 8.0;";
                string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + @path + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
                OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();
                string strExcel = "";
                OleDbDataAdapter myCommand = null;
                strExcel = "select * from [sheet1$]";
                myCommand = new OleDbDataAdapter(strExcel, strConn);
                DataTable table1 = new DataTable();
                ds = new DataSet();
                myCommand.Fill(table1);
                myCommand.Fill(ds);
            }
            catch (Exception ex)
            {
                err = ex.Message;
            }
            return ds;
        }
    }
}

这个上ExcelToTable类,继续下一篇:sqlHelp


你可能感兴趣的:(C#,C#,C#)