写下这篇博客来记录自己的工作,这部分功能是读取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. canutils上板测试问题记录

    ltp-ddt运行can_loopback时出错 pan(1881): Must supply a file collection or a command 原因runtest/ddt/can_loo ...

  2. Docker Swarm 介绍 or 工作原理

    Docker Swarm 介绍 Swarm 简介 Swarm是Docker公司自研发的容器集群管理系统,Swarm在早期是作为一个独立服务存在,在Docker Engine v1.12中集成了Swar ...

  3. Splay详解

    平衡树实际很简单的 以下讲解都以Luogu P3369 [模板]普通平衡树为例 我不会带指针的Splay,所以我就写非指针型的Splay Splay是基于二叉查找树(bst)实现的 什么是二叉查找树呢 ...

  4. [c/c++] programming之路(19)、数组指针

    一.指针运算 #include<stdio.h> #include<stdlib.h> void main0(){ ; int *p=&a; printf());//变 ...

  5. (转)A curated list of Artificial Intelligence (AI) courses, books, video lectures and papers

    A curated list of Artificial Intelligence (AI) courses, books, video lectures and papers. Updated 20 ...

  6. HDU4745--区间DP+回文串

    这题的题意为,给你一个环状的字符串,有两只兔子分别从某任意的石头上开始跳跃.一只顺时针跳.一只逆时针跳.两只兔子每一次落脚处石头的质量都相同.兔子要一步一步的跳,且不能跳到之前跳到过的地方.总的来说, ...

  7. Java对象的访问方式

    之前写过一篇随笔 https://www.cnblogs.com/qianjinyan/p/10352749.html 现在看看,貌似不是很准确,方法区和栈应当区分开来,两者有很大的区别 看下面的一个 ...

  8. tar命令-压缩,解压缩文件

    tar: -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 上面五个参数是独立的,压缩解压都要用到其中一个,可以和下面的命令连用但只能用其 ...

  9. Maven的pom配置文件

    1.1           Maven的pom配置文件 1.1.1            pom文件内容和作用 Pom.xml文件用来设置项目的项目依赖.插件.项目版本等信息,其中必须的是xml的根元 ...

  10. linux存储管理之基本分区

    基本分区管理 ====================================================================================基本分区(MBR| ...