首页 技术 正文
技术 2022年11月16日
0 收藏 694 点赞 3,924 浏览 1997 个字
 #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
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918