C#將Excel中的數(shù)據(jù)轉(zhuǎn)換成DataSet
更新時(shí)間:2015年03月23日 16:23:13 投稿:hebedich
這篇文章主要介紹了C#將Excel中的數(shù)據(jù)轉(zhuǎn)換成DataSet的方法,非常簡單實(shí)用,從本人項(xiàng)目中提取出來的,推薦給大家,希望對(duì)大家學(xué)習(xí)C#能夠有所幫助。
使用C#在不借助第三方插件的情況下將Excel中的數(shù)據(jù)轉(zhuǎn)換成DataSet
/// <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)稅", "墊付費(fèi)用", "超期", "到賬利潤" };
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();
}
}
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
您可能感興趣的文章:
- C#如何將DataTable導(dǎo)出到Excel解決方案
- C#操作EXCEL DataTable轉(zhuǎn)換的實(shí)例代碼
- C#實(shí)現(xiàn)將DataTable內(nèi)容輸出到Excel表格的方法
- C#使用Datatable導(dǎo)出Excel
- C#中DataGridView導(dǎo)出Excel的兩種方法
- C#把EXCEL數(shù)據(jù)轉(zhuǎn)換成DataTable
- C#把DataTable導(dǎo)出為Excel文件
- C#實(shí)現(xiàn)讀取Excel文件并將數(shù)據(jù)寫入數(shù)據(jù)庫和DataTable
- C#中ExcelDataReader的具體使用
相關(guān)文章
C# Winform實(shí)現(xiàn)圓角無鋸齒按鈕
這篇文章主要為大家詳細(xì)介紹了C# Winform實(shí)現(xiàn)圓角無鋸齒按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
c#基于Redis實(shí)現(xiàn)輕量級(jí)消息組件的步驟
這篇文章主要介紹了c#基于Redis實(shí)現(xiàn)輕量級(jí)消息組件的步驟,幫助大家更好的理解和學(xué)習(xí)使用c#進(jìn)行開發(fā),感興趣的朋友可以了解下2021-05-05
C#實(shí)現(xiàn)協(xié)同過濾算法的實(shí)例代碼
這篇文章介紹了C#實(shí)現(xiàn)協(xié)同過濾算法的實(shí)例代碼,有需要的朋友可以參考一下2013-07-07

