【C#】C#读取Excel中的数据

先贴代码:

private void btnStart_Click(object sender, EventArgs e)
        {
            if (txtPath.Text != "")
            {
                string conStr = "Provider=Microsoft.Ace.OleDb.12.0;Data Source=" + txtPath.Text + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
                using (OleDbConnection con = new OleDbConnection(conStr))
                {
                    string str1 = "select * from [全部电脑类资产$] where [ERP资产编号] is not null";
                    try
                    {
                        con.Open();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("连接Excel出错:" + ex.Message);
                    }

                    using (OleDbDataAdapter da = new OleDbDataAdapter(str1, con))
                    {
                        DataTable dt = new DataTable();
                        
                        da.Fill(dt);
                        //removeEmpty(dt);
                        dataGridView1.DataMember = "[全部电脑类资产$]";
                        dataGridView1.DataSource = dt;
                    }

                }
            }
            else
            {
                MessageHelper.WRONG("请先选择导入文件");
              
            }

        }


说明:

1.连接字符串中的:Microsoft.Ace.OleDb.12.0。既可以连接xls文件又可以连接xlsx文件,不建议使用Microsoft.Jet.OLEDB.4.0了,这个只能连接xls的excel.

 

2.连接字符串中的txtPath.Text就是你的excel文件的路径名,如:C:\Users\Jim\Desktop\2016.2.24.xlsx。其中的HDR=YES,是声明Excel表中的第一行是列名而不是数据,HDR=NO,则相反。

 

3.如果读取到的Excel中有空白行数据,就用sql语句中的is notnull过滤掉。如:

select * from [全部电脑类资产$] where [ERP资产编号] isnot null。


或者采用如下方法,移除DataTable中的空白行

  private void removeEmpty(DataTable dt)
        {
            List<DataRow> removelist = new List<DataRow>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                bool rowdataisnull = true;
                for (int j = 0; j < dt.Columns.Count; j++)
                {

                    if (!string.IsNullOrEmpty(dt.Rows[i][j].ToString().Trim()))
                    {

                        rowdataisnull = false;
                    }

                }
                if (rowdataisnull)
                {
                    removelist.Add(dt.Rows[i]);
                }

            }
            for (int i = 0; i < removelist.Count; i++)
            {
                dt.Rows.Remove(removelist[i]);
            }
        }


4.由于Microsoft.Ace.OleDb.12.0与Microsoft.Jet.OLEDB.4.0都不支持64位系统,所以如果出现“xxxxxx未注册”之类的问题,请将项目配置为x86平台。如果不想配置,则下载Microsoft.Ace.OleDb.12.0的64位驱动,点击下载



By Jim







  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JimCarter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值