--------------------------------2007及以上的版本--------------------------------

测试如下:

         //DataInterface.Method
/// <summary>
/// 读取2007版本excel
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns>返回一个表</returns>
public System.Data.DataTable GetExcel2007(string filePath)
{
try
{
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;HDR=NO;IMEX =1\""; OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
//获取Excel文件中第一个(按字母排序的)sheet页的页名。
string tableName = schemaTable.Rows[][].ToString().Trim(); string strExcel = "SELECT * FROM [" + tableName + "]";
OleDbDataAdapter myCommand = null;
myCommand = new OleDbDataAdapter(strExcel, strConn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "dtSource");
System.Data.DataTable table = ds.Tables["dtSource"];
conn.Close();
return table;
}
catch
{
return null;
}
}

--------------------------------2007以下的版本--------------------------------

测试如下:

 /// <summary>
/// 读取2003版本excel
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public System.Data.DataTable GetExcel2003(string filePath)
{
//读取OMR结果文件(xls)数据
try
{
//由于windows系统的更新,无法正常使用此strConn
//如果继续想用请对系统更新的补丁卸载即可
//string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + xlsPath + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
//也可以使用以下strConn
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1\"";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable (System.Data.OleDb.OleDbSchemaGuid.Tables, null);
//获取Excel文件中第一个(按字母排序的)sheet页的页名。
string tableName = schemaTable.Rows[][].ToString().Trim(); string strExcel = "SELECT * FROM [" + tableName + "]";
OleDbDataAdapter myCommand = null;
myCommand = new OleDbDataAdapter(strExcel, strConn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "dtSource");
System.Data.DataTable table = ds.Tables["dtSource"];
conn.Close();
return table;
}
catch
{
return null;
}
}

[C#]读取不同版本的excel文件的方法的更多相关文章

  1. c# 用OpenXmL读取.xlsx格式的Excel文件 返回DataTable

    1.须要引用的dll :  DocumentFormat.OpenXml.dll  ---须要安装一下OpenXml再引用 WindowsBase  ---直接在项目里加入引用 2.方法: /// & ...

  2. TXLSReadWriteII2版本导出Excel文件:

    //TXLSReadWriteII2版本导出Excel文件: procedure TForm1.N1Click(Sender: TObject); var i: Integer; aSaveDialo ...

  3. ci框架读取上传的excel文件数据

    原文链接: https://blog.csdn.net/qq_38148394/article/details/87921373 此功能实现使用到PHPExcel类库,PHPExcel是一个PHP类库 ...

  4. Java读取txt文件、excel文件的方法

    Java读取txt文件.excel文件的方法 1.读取txt文件 public static String getFileContent(String filePath,String charset) ...

  5. C#使用oledb操作excel文件的方法

    本文实例讲述了C#使用oledb操作excel文件的方法.分享给大家供大家参考.具体分析如下: 不管什么编程语言都会提供操作Excel文件的方式,C#操作Excel主要有以下几种方式: 1.Excel ...

  6. python使用xlrd模块读写Excel文件的方法

    本文实例讲述了python使用xlrd模块读写Excel文件的方法.分享给大家供大家参考.具体如下: 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi ...

  7. python将字典列表导出为Excel文件的方法

    将如下的字典列表内容导出为Excel表格文件形式: ​ 关于上图字典列表的写入,请参考文章:https://blog.csdn.net/weixin_39082390/article/details/ ...

  8. 在线读取Mongodb数据库下载EXCEL文件

    版本:Mongodb2.4.8 通过页面下载Excel文件 jsp <%@ page language="java" contentType="text/html; ...

  9. 读取FTP上的excel文件,并写入数据库

    今天遇到一些问题,需要从ftp上读取一些excel文件,并需要将excel中的数据写入到数据库,这样就可以通过管理页面查看这些数据. 我将相关工作分为三步,1.从ftp上读取相关文件,并将excel文 ...

随机推荐

  1. 【题解】Luogu UVA1411 Ants

    原题传送门 博客里对二分图匹配的详细介绍 这道题是带权二分图匹配 用的是KM算法 我们要知道一个定理:要使线段没有相交,要使距离总和最小 我们先把任意一对白点.黑点的距离算一下 然后运用KM算法 因为 ...

  2. Angular4.x 安装|创建项目|目录结构|创建组件

    Angular4.x 安装|创建项目|目录结构|创建组件 安装最新版本的 nodejs node.js 官网:https://nodejs.org/zh-cn/ 去官网下载 node.js,下一步下一 ...

  3. 搭建ldap服务器及web管理服务--phpldapadmin

    系统版本:centos6 安装配置openldap: yum install openldap openldap-servers openldap-clients openldap-devel com ...

  4. kali linux fuzz工具集简述

    模糊测试是一种自动化软件测试技术,涉及提供无效,意外或随机数据作为计算机程序的输入. 然后监视程序是否存在异常,例如崩溃,内置代码断言失败或潜在的内存泄漏. 通常,模糊器用于测试采用结构化输入的程序. ...

  5. C++max的使用方法

    #include <iostream> //#include <algorithm>//std::min std::max #include <stdint.h> ...

  6. 【python35.1--EasyGui界面】

    一.什么是EasyGUI EasyGUI是python中一个非常简单的GUI编程模块,不同于其他的GUI生成器,它不是事件驱动的,相反,所有的GUI交互都是通过简地函数调用就可以实现(意思是:函数调用 ...

  7. Catogory如何添加属性

    一,Category结构体 typedef struct category_t { const char *name; //类的名字 classref_t cls; //类 struct method ...

  8. Oracle SQL——inner jion;left join;right join的区别和使用场景

    背景 在一次面试的时候,面试官让我说一下这三者的使用场景和区别,当时瞬间懵逼,哈哈.回来赶快看一看,记下来. 详解 inner join 等值查询:返回两张表中,联结字段值相等的组合记录 举例:所有学 ...

  9. Vue学习【第六篇】:Vue-cli脚手架(框架)与实战案例

    环境搭建 安装node 官网下载安装包,傻瓜式安装:https://nodejs.org/zh-cn/ 安装cnpm npm install -g cnpm --registry=https://re ...

  10. 什么是TF-A?

    1. TF-A的全称是什么? Arm Trusted Firmware 2. TF-A的作用是什么? 在secure world和non-secure world之间切换 3. TF-A涉及到哪几个部 ...