写下这篇博客来记录自己的工作,这部分功能是读取Excel表格中的数据,并通过c#中的datagridview控件将读取的数据显示出来。为了方便用户,我设计了一个read按钮,用户点击这个按钮,会弹出打开文件的窗口,选择想要打开的文件后,datagridview中会显示读取的表格数据。

程序运行截图如下:

主要代码如下:

首先引用要System.Data.OleDb;

using System.Data.OleDb;

点击read按钮弹出打开文件的窗口:

private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.Filter = "表格(*.xls)|*.xls|所有文件(*.*)|*.*";
if (openfile.FilterIndex == && openfile.ShowDialog() == DialogResult.OK)
ExcelToDS(openfile.FileName);
}

需要在Designer.cs文件中添加最下面的一行代码:

// button1
//
this.button1.Location = new System.Drawing.Point(, );
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(, );
this.button1.TabIndex = ;
this.button1.Text = "read";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//

读取数据显示在datagrid中:

 public DataSet ExcelToDS(string path)
{
//源的定义
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + @path + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);//连接数据源
conn.Open();
//Sql语句
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel = "select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);//适配到数据源
ds = new DataSet();//定义存放的数据表
myCommand.Fill(ds, "table1");
dataGridView1.DataSource = ds.Tables["table1"];
return ds; }

很简单的代码,但是问题就出在连接字符串上面,后面一定要加上Extended Properties='Excel 8.0;

一切就像操作数据库一样,只是需要注意的是:数据提供程序使用Jet,同时需要指定Extended Properties 关键字设置 Excel 特定的属性。

不同版本的Excel对应不同的属性值:

对于 Microsoft Excel 8.0 (97)、9.0 (2000) 和 10.0 (2002) 工作簿,请使用 Excel 8.0;

对于 Microsoft Excel 5.0 和 7.0 (95) 工作簿,请使用 Excel 5.0;

对于 Microsoft Excel 4.0 工作簿,请使用 Excel 4.0;

对于 Microsoft Excel 3.0 工作簿,请使用 Excel 3.0。

读取Excel表格中数据原型的更多相关文章

  1. C#读取Excel表格中数据并返回datatable

    在软件开发的过程中,经常用到从excel表格中读取数据作为数据源,以下整理了一个有效的读取excel表格的方法. DataTable GetDataTable(string tableName,str ...

  2. C#读取Excel表格的数据

    1.创建工程后,需要下载 EPPlus.dll 添加到工程中,这里有一个下载地址:https://download.csdn.net/download/myunity/10784634 2.下面仅实现 ...

  3. 利用java反射机制实现读取excel表格中的数据

    如果直接把excel表格中的数据导入数据库,首先应该将excel中的数据读取出来. 为了实现代码重用,所以使用了Object,而最终的结果是要获取一个list如List<User>.Lis ...

  4. python读取excel表格中的数据

    使用python语言实现Excel 表格中的数据读取,需要用到xlrd.py模块,实现程序如下: import xlrd #导入xlrd模块 class ExcelData(): def __init ...

  5. java用poi读取Excel表格中的数据

    Java读写Excel的包是Apache POI(项目地址:http://poi.apache.org/),因此需要先获取POI的jar包,本实验使用的是POI 3.9稳定版.Apache POI 代 ...

  6. java读取Excel表格中的数据

    1.需求 用java代码读取hello.xls表格中的数据 2.hello.xls表格 3.java代码 package com.test; import java.io.File; import j ...

  7. php 读取excel表格中的内容

    <?php /** * excel表格内容在网页中显示 * * 首先需要下载PHPExcel 工具包 * 网址: http://phpexcel.codeplex.com/releases/vi ...

  8. 解决读取Excel表格中某列数据为空的问题 c#

    解决同一列中“字符串”和“数字”两种格式同时存在,读取时,不能正确显示“字符串”格式的问题:set xlsconn=CreateObject("ADODB.Connection") ...

  9. js/ts/tsx读取excel表格中的日期格式转换

    const formatDate = (timestamp: number) => { const time = new Date((timestamp - 1) * 24 * 3600000 ...

随机推荐

  1. 2019/3/28 wen 继承

  2. springboot使用hibernate validator校验

    一.参数校验 在开发中经常需要写一些字段校验的代码,比如字段非空,字段长度限制,邮箱格式验证等等,写这些与业务逻辑关系不大的代码个人感觉有两个麻烦: 验证代码繁琐,重复劳动 方法内代码显得冗长 每次要 ...

  3. YII1 增、删、改、查数据库操作

    增 //注意插入的时候 是用的new Post $objectPost = new Post; $objectPost->username = $username; $objectPost-&g ...

  4. mybatis 查询优化主子表查询之association和collection

    很多开发人员之所以编写出低效的应用,有一大原因是并不理解怎样编写高效的SQL.以订单查询为例,我们经常需要查询某个用户的订单以及订单明细,并且以树形方式展现如下: 对于这种性质的功能,很多开发人员的做 ...

  5. dataguard从库删除归档的例子

    deletearchlog.sh #!/bin/sh export PATH TABLE='v$archived_log' sqlplus -silent "/ as sysdba" ...

  6. freeswqitch odbc

    1.安装驱动 yum install unixODBC unixODBC-devel libtool-ltdl libtool-ltdl-devel -y yum install mysql-conn ...

  7. 160CrackMe第十九Brad Soblesky.2

    查壳无壳,vc写的. 我们输入假码后,然后点击,弹出错误框,直接打开od,对MessageBoxA下断点也行,寻找字符串也行. 一般的错误提示部分代码类似于这样. call xxx test xxx, ...

  8. Mac redis安装

    Download, extract and compile Redis with: #进入下载目录 $ cd ... $ wget http://download.redis.io/releases/ ...

  9. OpenStack笔记

    *********virsh xml文件解读****************************** https://libvirt.org/format.html https://libvirt ...

  10. 以太坊钱包开发系列2 - 账号Keystore文件导入导出

    以太坊去中心化网页钱包开发系列,将从零开始开发出一个可以实际使用的钱包,本系列文章是理论与实战相结合,一共有四篇:创建钱包账号.账号Keystore文件导入导出.展示钱包信息及发起签名交易.发送Tok ...