C#读取Excel数据方式


直接把Excel当作数据库,查询Excel的数据,代码如下:


  1.             String source = null;

  2.             OdbcConnection conn = null;

  3.             string sql = "select * from [Sheet1$]";

  4.             try

  5.             {

  6.                 source = "Driver={Microsoft Excel Driver (*.xls)};DBQ=" + tbExcelFilePath.Text;

  7.                 conn = new OdbcConnection(source);

  8.                 conn.Open();

  9.             }

  10.             catch (OdbcException e)

  11.             {

  12.                 try

  13.                 {

  14.                     source = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" + tbExcelFilePath.Text;

  15.                     conn = new OdbcConnection(source);

  16.                     conn.Open();

  17.                 }

  18.                 catch (OdbcException e1)

  19.                 {

  20.                     MessageBox.Show("请确认此文件没有被其它程序打开!");

  21.                 }

  22.             }

  23.             try

  24.             {

  25.                 OdbcCommand cmd = new OdbcCommand(sql, conn);

  26.                 OdbcCommand cmd1 = new OdbcCommand("select count(*) from [Sheet1$]", conn);

  27.                 OdbcDataReader read = cmd.ExecuteReader();

  28.                 int count = int.Parse(cmd1.ExecuteScalar().ToString());

  29.                 int rn = 1;

  30.      while (read.Read())

  31.                 {

  32.                     try

  33.                     {

  34.                         if (m_stop) break;

  35.                         rn++;

  36.                         string lv_strSql;

  37.                         string lv_strSqlOne = "insert into user (";

  38.                         string lv_strSqlTwo = " value(";

  39.                         String[] row = new String[read.FieldCount];

  40.                         for (int i = 0; i < read.FieldCount; i++)

  41.                         {

  42.                             row[i] = read.GetValue(i).ToString();

  43.                             if (read.GetName(i) == "会员姓名" && read.GetValue(i).ToString().Trim() != "")

  44.                             {

  45.                                 lv_strSqlOne += "name,";

  46.                                 lv_strSqlTwo += "'" + read.GetValue(i).ToString() + "',";

  47.                             }

  48.                             ............//Excel可能有多列

  49.                             else if (read.GetName(i) == "累计积分" && read.GetValue(i).ToString().Trim() != "")

  50.                             {

  51.                                 lv_strSqlOne += "score,";

  52.                                 lv_strSqlTwo += "'" + read.GetValue(i).ToString() + "',";

  53.                             }

  54.                          }

  55.                         lv_strSqlOne += "create_date,sync_flag)";

  56.                         lv_strSqlTwo += "'" + Date

  57.                           Time.Now + "',0)";

  58.                         lv_strSql = lv_strSqlOne + lv_strSqlTwo;

  59.                         Console.WriteLine("lv_strSql:" + lv_strSql);

  60.                         int lv_ret = m_db.RunNoQuery(lv_strSql);

  61.                     }

  62.                     catch (Exception ex)

  63.                     {

  64.                         Console.WriteLine(ex.Message);

  65.                     }                

  66.         }

  67.                 read.Close();

  68.                 conn.Close();

  69.      catch (Exception e)

  70.             {

  71.                 MessageBox.Show(e.Message);

  72.             }  

  73.             }

      


原文地址:http://blog.csdn.net/zhaozhi_1983/article/details/2866099


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