C#將Excel中的數(shù)據(jù)轉(zhuǎn)換成DataSet
更新時間:2015年03月23日 16:23:13 投稿:hebedich
這篇文章主要介紹了C#將Excel中的數(shù)據(jù)轉(zhuǎn)換成DataSet的方法,非常簡單實用,從本人項目中提取出來的,推薦給大家,希望對大家學(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)稅", "墊付費用", "超期", "到賬利潤" };
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)換的實例代碼
- C#實現(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#實現(xiàn)讀取Excel文件并將數(shù)據(jù)寫入數(shù)據(jù)庫和DataTable
- C#中ExcelDataReader的具體使用

