C#读取Excel数据方式

这篇博客介绍了一种将Excel文件当作数据库来读取数据的方法,使用OdbcConnection连接Excel文件,并通过OdbcCommand执行SQL查询。在读取数据过程中,针对Excel的不同列(如会员姓名和累计积分),将数据插入到数据库的相应表中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接把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.             }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值