zoukankan      html  css  js  c++  java
  • [C#]读取不同版本的excel文件的方法

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

    测试如下:

     1         //DataInterface.Method
     2         /// <summary>
     3         /// 读取2007版本excel
     4         /// </summary>
     5         /// <param name="filePath">文件路径</param>
     6         /// <returns>返回一个表</returns>
     7         public System.Data.DataTable GetExcel2007(string filePath)
     8         {
     9             try
    10             {
    11                 string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties="Excel 12.0;HDR=NO;IMEX =1"";
    12 
    13                 OleDbConnection conn = new OleDbConnection(strConn);
    14                 conn.Open();
    15                 System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
    16                 //获取Excel文件中第一个(按字母排序的)sheet页的页名。
    17                 string tableName = schemaTable.Rows[0][2].ToString().Trim();
    18 
    19                 string strExcel = "SELECT  * FROM [" + tableName + "]";
    20                 OleDbDataAdapter myCommand = null;
    21                 myCommand = new OleDbDataAdapter(strExcel, strConn);
    22                 DataSet ds = new DataSet();
    23                 myCommand.Fill(ds, "dtSource");
    24                 System.Data.DataTable table = ds.Tables["dtSource"];
    25                 conn.Close();
    26                 return table;
    27             }
    28             catch
    29             {
    30                 return null;
    31             }
    32         }

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

    测试如下:

     1 /// <summary>
     2         /// 读取2003版本excel
     3         /// </summary>
     4         /// <param name="filePath"></param>
     5         /// <returns></returns>
     6         public System.Data.DataTable GetExcel2003(string filePath)
     7         {
     8             //读取OMR结果文件(xls)数据
     9             try
    10             {
    11         //由于windows系统的更新,无法正常使用此strConn
    12         //如果继续想用请对系统更新的补丁卸载即可
    13                 //string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + xlsPath + 
    14 
    15 ";Extended Properties="Excel 8.0;HDR=YES;IMEX=1"";
    16         //也可以使用以下strConn
    17                 string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + 
    18 
    19 ";Extended Properties="Excel 8.0;HDR=NO;IMEX=1"";
    20                 OleDbConnection conn = new OleDbConnection(strConn);
    21                 conn.Open();
    22                 System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable
    23 
    24 (System.Data.OleDb.OleDbSchemaGuid.Tables, null);
    25                 //获取Excel文件中第一个(按字母排序的)sheet页的页名。
    26                 string tableName = schemaTable.Rows[0][2].ToString().Trim();
    27 
    28                 string strExcel = "SELECT  * FROM [" + tableName + "]";
    29                 OleDbDataAdapter myCommand = null;
    30                 myCommand = new OleDbDataAdapter(strExcel, strConn);
    31                 DataSet ds = new DataSet();
    32                 myCommand.Fill(ds, "dtSource");
    33                 System.Data.DataTable table = ds.Tables["dtSource"];
    34                 conn.Close();
    35                 return table;
    36             }
    37             catch
    38             {
    39                 return null;
    40             }
    41         }
  • 相关阅读:
    vector order by function and object
    C++ Tree node ,when g++ compile multiple cpp files should list them respectively
    Node.1
    asp遍历xml节点
    redis扩展安装以及在tp5中的操作
    JS中在循环内实现休眠(for循环内setTimeout延时问题)
    一种开发软件的新思路,给Web页面穿个马甲,用web页面做软件UI,用C#(或者C++等其它语言)代码做功能 碧血黄沙
    高仿QQMusic播放器,浅谈WinForm关于UI的制作 碧血黄沙
    我的站(艾网城市生活新门户)重新上线了 碧血黄沙
    用WPF开发仿QQ概念版之MessageWindow开发以及Demo下载 碧血黄沙
  • 原文地址:https://www.cnblogs.com/ttkl/p/7729767.html
Copyright © 2011-2022 走看看