将 Excel 数据导入 MySql

能承受上万数据量,速度快,并且使用了事务,不会出现某条数据错误而导致部分数据插入(要是全部成功要是一条都不成功,测试过程中没出现失败),需要的朋友可以参考下

 

1.NPOI

2.MySql.Data

这里做个记录,待日后使用

效果图

 

 

     

 

连接字符串 app.config:

 <connectionStrings>
    <add name="DBConnectString" connectionString="Server=ip;Database=testjh;Uid=user;Pwd=123456;charset=utf8" providerName="MySql.Data.MySqlClient"/>
  </connectionStrings>

 

执行多条sql语句:

        /// <summary>
        /// 执行多条SQL语句,实现数据库事务。
        /// </summary>mysql数据库
        /// <param name="SQLStringList">多条SQL语句</param>
        public static void ExecuteSqlTran(List<string> SQLStringList)
        {
            using (MySqlConnection conn = new MySqlConnection(MySqlHelper.connectionString))
            {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = conn;
                MySqlTransaction tx = conn.BeginTransaction();
                cmd.Transaction = tx;
                try
                {
                    for (int n = 0; n < SQLStringList.Count; n++)
                    {
                        string strsql = SQLStringList[n].ToString();
                        if (strsql.Trim().Length > 1)
                        {
                            cmd.CommandText = strsql;
                            cmd.ExecuteNonQuery();
                        }
                        //后来加上的
                        if (n > 0 && (n % 500 == 0 || n == SQLStringList.Count - 1))
                        {
                            tx.Commit();
                            tx = conn.BeginTransaction();
                        }
                    }
                    //tx.Commit();//原来一次性提交
                }
                catch (System.Data.SqlClient.SqlException E)
                {
                    tx.Rollback();
                    throw new Exception(E.Message);
                }
            }
        }

  

单击按钮:

        /// <summary>
        /// 单击按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Text = "正在插入中...";
            OpenFileDialog file1 = new OpenFileDialog();
            file1.Filter = "Excel文件|*.xlsx";
            if (file1.ShowDialog() == DialogResult.OK)
            {
                //开始读excel
                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); ;
                Microsoft.Office.Interop.Excel.Workbook workbook = xlApp.Workbooks.Open(file1.FileName, 0, false, 5, "", "",false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, 1, 0);
                int n = workbook.Worksheets.Count;
                string[] sheetSet = new string[n];
                ArrayList al = new ArrayList();
                for (int i = 0; i < n; i++)
                {
                    sheetSet[i] = ((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[i + 1]).Name;
                }
                xlApp.Workbooks.Close();


                ExcelHelper ex = new ExcelHelper(file1.FileName);
                if (sheetSet.Length > 0)//判断存在 一个 Sheet1
                {
                    int countLength = sheetSet.Length;//表示 Sheet的个数
                    dt1 = ex.ExcelToDataTable(sheetSet[0], true);//"Sheet1" ,先假如只有一个Sheet1     
                    insertMySql();
                }
            }

        }

写入数据:

public void insertMySql()
        {
            try
            {
                int ColumnsCount = dt1.Columns.Count;//得到读出表中 列的数量
                string str = string.Empty;
                string[] strArr = new string[ColumnsCount];
                string valuesStr = string.Empty;
                List<string> listStr=new List<string>();                

                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    DataRow dr = dt1.Rows[i];

                    for (int j = 0; j < ColumnsCount; j++)
                    {
                        strArr[j] = dr.ItemArray[j].ToString();
                    }
                    string sqlInsert =string.Format("insert into t_test(torder,tname,stu_num,tage) values('{0}','{1}','{2}','{3}')",strArr[0],strArr[1],strArr[2],strArr[3]);
                    listStr.Add(sqlInsert);
                    #region 没使用事务
                    
                    //string sql = "insert into t_test(torder,tname,stu_num,tage) values(@torder,@tname,@stu_num,@tage)";
                    //MySqlParameter[] parms = { 
                    //                     new MySqlParameter("@torder",strArr[0]),
                    //                     new MySqlParameter("@tname",strArr[1]),
                    //                     new MySqlParameter("@stu_num",strArr[2]),
                    //                     new MySqlParameter("@tage",strArr[3])
                    //                     };
                    // 在这里可以直接调用 ExecuteNonQuery 向数据库插数据
                  //  MySqlHelper.ExecuteNonQuery(MySqlHelper.connectionString, CommandType.Text, sql, parms);
                    #endregion
                }
                ExecuteSqlTran(listStr);//使用事务插入
              //  button1.Text = "插入成功!";
                Thread.Sleep(10000);
                button1.Text = "插入成功,请选择Excel文件!";
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
          
        }

MySqlHelper:  应该把   ExecuteSqlTran 这个类方法放入该类,这里我没有这么做,项目小(零时需求)就没这么做了!

public static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectString"].ToString();

  

ExcelHelper:  

public class ExcelHelper : IDisposable
    {
        private string fileName = null; //文件名
        private IWorkbook workbook = null;
        private FileStream fs = null;
        private bool disposed;

        public ExcelHelper(string fileName)
        {
            this.fileName = fileName;
            disposed = false;
        }

        /// <summary>
        /// 将DataTable数据导入到excel中
        /// </summary>
        /// <param name="data">要导入的数据</param>
        /// <param name="isColumnWritten">DataTable的列名是否要导入</param>
        /// <param name="sheetName">要导入的excel的sheet的名称</param>
        /// <returns>导入数据行数(包含列名那一行)</returns>
        public int DataTableToExcel(DataTable data, string sheetName, bool isColumnWritten)
        {
            int i = 0;
            int j = 0;
            int count = 0;
            ISheet sheet = null;

            fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            if (fileName.IndexOf(".xlsx") > 0) // 2007版本
                workbook = new XSSFWorkbook();
            else if (fileName.IndexOf(".xls") > 0) // 2003版本
                workbook = new HSSFWorkbook();

            try
            {
                if (workbook != null)
                {
                    sheet = workbook.CreateSheet(sheetName);
                }
                else
                {
                    return -1;
                }

                if (isColumnWritten == true) //写入DataTable的列名
                {
                    IRow row = sheet.CreateRow(0);
                    for (j = 0; j < data.Columns.Count; ++j)
                    {
                        row.CreateCell(j).SetCellValue(data.Columns[j].ColumnName);
                    }
                    count = 1;
                }
                else
                {
                    count = 0;
                }

                for (i = 0; i < data.Rows.Count; ++i)
                {
                    IRow row = sheet.CreateRow(count);
                    for (j = 0; j < data.Columns.Count; ++j)
                    {
                        row.CreateCell(j).SetCellValue(data.Rows[i][j].ToString());
                    }
                    ++count;
                }
                workbook.Write(fs); //写入到excel
                return count;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
                return -1;
            }
        }

        /// <summary>
        /// 将excel中的数据导入到DataTable中
        /// </summary>
        /// <param name="sheetName">excel工作薄sheet的名称</param>
        /// <param name="isFirstRowColumn">第一行是否是DataTable的列名</param>
        /// <returns>返回的DataTable</returns>
        public DataTable ExcelToDataTable(string sheetName, bool isFirstRowColumn)
        {
            ISheet sheet = null;
            DataTable data = new DataTable();
            int startRow = 0;
            try
            {
                fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                if (fileName.IndexOf(".xlsx") > 0) // 2007版本
                    workbook = new XSSFWorkbook(fs);
                else if (fileName.IndexOf(".xls") > 0) // 2003版本
                    workbook = new HSSFWorkbook(fs);

                if (sheetName != null)
                {
                    sheet = workbook.GetSheet(sheetName);
                    if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
                    {
                        sheet = workbook.GetSheetAt(0);
                    }
                }
                else
                {
                    sheet = workbook.GetSheetAt(0);
                }
                if (sheet != null)
                {
                    IRow firstRow = sheet.GetRow(0);
                    int cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数

                    if (isFirstRowColumn)
                    {
                        for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                        {
                            ICell cell = firstRow.GetCell(i);
                            if (cell != null)
                            {
                                string cellValue = cell.StringCellValue;
                                if (cellValue != null)
                                {
                                    DataColumn column = new DataColumn(cellValue);
                                    data.Columns.Add(column);
                                }
                            }
                        }
                        startRow = sheet.FirstRowNum + 1;
                    }
                    else
                    {
                        startRow = sheet.FirstRowNum;
                    }

                    //最后一列的标号
                    int rowCount = sheet.LastRowNum;
                    for (int i = startRow; i <= rowCount; ++i)
                    {
                        IRow row = sheet.GetRow(i);
                        if (row == null) continue; //没有数据的行默认是null       

                        DataRow dataRow = data.NewRow();
                        for (int j = row.FirstCellNum; j < cellCount; ++j)
                        {
                            if (row.GetCell(j) != null) //同理,没有数据的单元格都默认是null
                                dataRow[j] = row.GetCell(j).ToString();
                        }
                        data.Rows.Add(dataRow);
                    }
                }

                return data;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
                return null;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    if (fs != null)
                        fs.Close();
                }

                fs = null;
                disposed = true;
            }
        }       
    }

  

posted on 2015-05-13 17:46  知鸟  阅读(563)  评论(0)    收藏  举报