將excel數(shù)據(jù)轉(zhuǎn)換成dataset示例
更新時間:2014年02月28日 14:53:16 作者:
這篇文章主要介紹了不借助第三方插件的情況下將Excel中的數(shù)據(jù)轉(zhuǎn)換成DataSet的方法,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
/// <summary>
/// EXCEL數(shù)據(jù)轉(zhuǎn)換DataSet
/// </summary>
/// <param name="filePath">文件全路徑</param>
/// <param name="search">表名</param>
/// <returns></returns>
private DataSet GetDataSet(string fileName)
{
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';";
OleDbConnection objConn = null;
objConn = new OleDbConnection(strConn);
objConn.Open();
DataSet ds = new DataSet();
//List<string> List = new List<string> { "收款金額", "代付關(guān)稅", "墊付費用", "超期", "到賬利潤" };
List<string> List = new List<string> { };
DataTable dtSheetName = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
foreach (DataRow dr in dtSheetName.Rows)
{
if (dr["Table_Name"].ToString().Contains("$") && !dr[2].ToString().EndsWith("$"))
{
continue;
}
string s = dr["Table_Name"].ToString();
List.Add(s);
}
try
{
for (int i = 0; i < List.Count; i++)
{
ds.Tables.Add(List[i]);
string SheetName = List[i];
string strSql = "select * from [" + SheetName + "]";
OleDbDataAdapter odbcCSVDataAdapter = new OleDbDataAdapter(strSql, objConn);
DataTable dt = ds.Tables[i];
odbcCSVDataAdapter.Fill(dt);
}
return ds;
}
catch (Exception ex)
{
return null;
}
finally
{
objConn.Close();
objConn.Dispose();
}
}
相關(guān)文章
解析C#設(shè)計模式編程中適配器模式的實現(xiàn)
這篇文章主要介紹了C#設(shè)計模式編程中適配器模式的實現(xiàn),分別舉了類的對象適配器與對象的適配器模式的例子,需要的朋友可以參考下2016-02-02Unity?UGUI的VerticalLayoutGroup垂直布局組件介紹使用
這篇文章主要為大家介紹了Unity?UGUI的VerticalLayoutGroup垂直布局組件介紹使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07C# 中的 IReadOnlyDictionary 和 IReadOnlyLis
C# 中的IReadOnlyDictionary和IReadOnlyList是接口,用于表示只讀的字典和只讀的列表,這些接口提供了對集合的只讀訪問權(quán)限,即不允許對集合進行修改操作,這篇文章主要介紹了C# 中的 IReadOnlyDictionary 和 IReadOnlyList實例詳解,需要的朋友可以參考下2024-03-03