--------------------------------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. Nginx 容器教程

    春节前,我看到 Nginx 加入了 HTTP/2 的 server push 功能,就很想试一下. 正好这些天,我在学习 Docker,就想到可以用 Nginx 容器.万一哪里改乱了,直接删掉,再重启 ...

  2. mint-ui之Swipe使用

    <template> <div> <div class="swipe-wrapper"> <mt-swipe :auto="10 ...

  3. VC++ 获取Windows系统版本号、CPU名称

    转载:https://blog.csdn.net/sunflover454/article/details/51525179 转载:https://blog.csdn.net/magictong/ar ...

  4. topcoder srm 690 div1 -3

    1.给定一个数字$N$,从1到100的100个数字中选出$K$个数字(设为集合$S$),然后对$S$进行如下运算: (1)删除$S$中的某些数字:(可以删除0个数字) (2)将$S$中的某些数字取为它 ...

  5. Python3 tkinter基础 Entry validate isdigit 只能输入数字的输入框

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  6. Flask学习【第5篇】:用Falsk实现的分页

    Flask实现的分页组件 from urllib.parse import urlencode,quote,unquote class Pagination(object): "" ...

  7. 编译openwrt时报错:FMCGenericError.h:34:27: fatal error: libxml/parser.h: No such file or directory

    解决办法: 更新openwrt的feeds,并重新make menuconfig ./script/feeds update -a ./script/feeds install -a

  8. 使用vs code搭建C开发环境

    关于搭建vscode的开发环境来开发c网上已经有很多类似的贴子,但是几乎都是直接给出tasks.json和launch.json文件,并未说明这两个文件的作用以及如何配置.这里面向纯小白着重说明下(1 ...

  9. Ubuntu 18.04 修改gedit的配色方案

    下图中的蓝色的注释代码,真是有点让人瞎眼的感觉 去这个网站 https://github.com/mig/gedit-themes/tree/master 下载所有后解压到/usr/share/gtk ...

  10. SpringBoot 优化内嵌的Tomcat

    在通过SpringBoot调用其它服务时,报错 org.springframework.web.client.ResourceAccessException: I/O error on POST re ...