#region 从Excel获取数据
/// <summary>
/// 从Excel获取数据
/// </summary>
/// <param name="Path">文件路径(完整路径)</param>
/// <returns></returns>
public static DataSet ReadExcelToDS(string Path)
{
DataSet ds = new DataSet();
OleDbConnection conn = null;
OleDbDataAdapter da = null;
try
{
//string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';";//此连接只能操作Excel2007之前(.xls)文件
string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + Path + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'"; //此连接可以操作.xls与.xlsx文件 (支持Excel2003 和 Excel2007 的连接字符串)
//备注: "HDR=yes;"是说Excel文件的第一行是列名而不是数据,"HDR=No;"正好与前面的相反。
// "IMEX=1 "如果列中的数据类型不一致,使用"IMEX=1"可必免数据类型冲突。
conn = new OleDbConnection(strConn);
conn.Open();
DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
if (schemaTable != null)
{
string sheetName = schemaTable.Rows[]["table_name"].ToString().Trim();
if (sheetName != null && sheetName.Length > )
{
string strExcel = "select * from [" + sheetName + "]";
da = new OleDbDataAdapter(strExcel, conn);
da.Fill(ds);
}
}
}
catch (Exception e)
{
throw e;
}
finally
{
if (conn != null) conn.Close();
if (da != null) da.Dispose();
}
return ds;
}
#endregion
 #region 从CSV文件获取数据
/// <summary>
/// 从CSV文件获取数据
/// </summary>
/// <param name="Path">文件路径(完整路径)</param>
/// <returns></returns>
public static DataSet GetDataSetFromCSV(string Path)
{
string filePath = System.IO.Path.GetDirectoryName(Path);
string fileName = System.IO.Path.GetFileName(Path);
string strConn = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + filePath + ";Extensions=asc,csv,tab,txt;";
DataSet ds = new DataSet();
OdbcConnection Conn = new OdbcConnection(strConn);
OdbcDataAdapter da = null;
try
{
Conn.Open();
DataTable schemaTable = Conn.GetSchema("Tables", null);
if (schemaTable != null)
{
for (int i = ; i < schemaTable.Rows.Count; i++)
{
string sheetName = schemaTable.Rows[i]["table_name"].ToString().Trim();
if (fileName == sheetName)
{
string strSql = "select * from " + sheetName;
da = new OdbcDataAdapter(strSql, Conn);
da.Fill(ds);
break;
}
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
Conn.Close();
if (da != null) da.Dispose();
}
return ds;
}
#endregion

最新文章

  1. IDDD 实现领域驱动设计-上下文映射图及其相关概念
  2. linux命令小常识
  3. GJM : FlatBuffers 与 protobuf 性能比较 [转载 ]
  4. (转)AVI文件格式解析+AVI文件解析工具
  5. ES6:模块简单解释
  6. 【每日scrum】NO.6
  7. Jar包下载
  8. 【转】 当程序崩溃的时候怎么办 Part-2
  9. RDMA编程实例
  10. win7启动后报丢失nscmk.dll解决解决方式
  11. JavaScript之通用addLoadEvent代码源码
  12. Android Studio中添加SlidingMenu
  13. Redis集群介绍
  14. WebService-axis2
  15. 00_HTML入门第一天
  16. 通过Log4net来配置我们需要的日志文件格式
  17. Android 简单统计文本文件字符数、单词数、行数Demo
  18. 爬虫下载QQ音乐:获取所有歌手-每个歌手的专辑-每个专辑里的歌曲
  19. Python3爬虫系列:理论+实验+爬取妹子图实战
  20. .net3.5 支持tuple

热门文章

  1. 【托业】【跨栏】REVIEW2
  2. Centos下搭建邮件服务器
  3. VUE—打印(原生态网页打印)
  4. 20175211 2018-2019-2 《Java程序设计》第二周学习总结
  5. 猪年设计素材:一波免费猪猪icon已为你备好
  6. Django框架获取各种form表单数据
  7. C#中哈希表(HashTable)的用法详解以及和Dictionary比较
  8. WebForm复合控件RadioButtonList、CheckBoxList、DropDownList
  9. matlab将rgb图转为灰度图的原理代码
  10. Filter中request对象强转问题