asp.net 讀取Excel數(shù)據(jù)到DataTable的代碼
更新時(shí)間:2010年03月16日 19:48:40 作者:
asp.net 讀取Excel數(shù)據(jù)到DataTable的代碼,需要的朋友可以參考下。
復(fù)制代碼 代碼如下:
/// <summary>
/// 獲取指定路徑、指定工作簿名稱的Excel數(shù)據(jù):取第一個(gè)sheet的數(shù)據(jù)
/// </summary>
/// <param name="FilePath">文件存儲(chǔ)路徑</param>
/// <param name="WorkSheetName">工作簿名稱</param>
/// <returns>如果爭(zhēng)取找到了數(shù)據(jù)會(huì)返回一個(gè)完整的Table,否則返回異常</returns>
public DataTable GetExcelData(string astrFileName)
{
string strSheetName = GetExcelWorkSheets(astrFileName)[0].ToString();
return GetExcelData(astrFileName, strSheetName);
}
代碼
復(fù)制代碼 代碼如下:
/// <summary>
/// 返回指定文件所包含的工作簿列表;如果有WorkSheet,就返回以工作簿名字命名的ArrayList,否則返回空
/// </summary>
/// <param name="strFilePath">要獲取的Excel</param>
/// <returns>如果有WorkSheet,就返回以工作簿名字命名的ArrayList,否則返回空</returns>
public ArrayList GetExcelWorkSheets(string strFilePath)
{
ArrayList alTables = new ArrayList();
OleDbConnection odn = new OleDbConnection(GetExcelConnection(strFilePath));
odn.Open();
DataTable dt = new DataTable();
dt = odn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
throw new Exception("無(wú)法獲取指定Excel的架構(gòu)。");
}
foreach (DataRow dr in dt.Rows)
{
string tempName = dr["Table_Name"].ToString();
int iDolarIndex = tempName.IndexOf('$');
if (iDolarIndex > 0)
{
tempName = tempName.Substring(0, iDolarIndex);
}
//修正了Excel2003中某些工作薄名稱為漢字的表無(wú)法正確識(shí)別的BUG。
if (tempName[0] == '\'')
{
if (tempName[tempName.Length - 1] == '\'')
{
tempName = tempName.Substring(1, tempName.Length - 2);
}
else
{
tempName = tempName.Substring(1, tempName.Length - 1);
}
}
if (!alTables.Contains(tempName))
{
alTables.Add(tempName);
}
}
odn.Close();
if (alTables.Count == 0)
{
return null;
}
return alTables;
}
代碼
復(fù)制代碼 代碼如下:
/// <summary>
/// 獲取指定路徑、指定工作簿名稱的Excel數(shù)據(jù)
/// </summary>
/// <param name="FilePath">文件存儲(chǔ)路徑</param>
/// <param name="WorkSheetName">工作簿名稱</param>
/// <returns>如果爭(zhēng)取找到了數(shù)據(jù)會(huì)返回一個(gè)完整的Table,否則返回異常</returns>
public DataTable GetExcelData(string FilePath, string WorkSheetName)
{
DataTable dtExcel = new DataTable();
OleDbConnection con = new OleDbConnection(GetExcelConnection(FilePath));
OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from [" + WorkSheetName + "$]", con);
//讀取
con.Open();
adapter.FillSchema(dtExcel, SchemaType.Mapped);
adapter.Fill(dtExcel);
con.Close();
dtExcel.TableName = WorkSheetName;
//返回
return dtExcel;
}
代碼
復(fù)制代碼 代碼如下:
/// <summary>
/// 獲取鏈接字符串
/// </summary>
/// <param name="strFilePath"></param>
/// <returns></returns>
public string GetExcelConnection(string strFilePath)
{
if (!File.Exists(strFilePath))
{
throw new Exception("指定的Excel文件不存在!");
}
return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFilePath + ";Extended properties=\"Excel 8.0;Imex=1;HDR=Yes;\"";
//@"Provider=Microsoft.Jet.OLEDB.4.0;" +
//@"Data Source=" + strFilePath + ";" +
//@"Extended Properties=" + Convert.ToChar(34).ToString() +
//@"Excel 8.0;" + "Imex=1;HDR=Yes;" + Convert.ToChar(34).ToString();
}
您可能感興趣的文章:
- asp.net實(shí)現(xiàn)導(dǎo)出DataTable數(shù)據(jù)到Word或者Excel的方法
- asp.net實(shí)現(xiàn)數(shù)據(jù)從DataTable導(dǎo)入到Excel文件并創(chuàng)建表的方法
- Asp.net中DataTable導(dǎo)出到Excel的方法介紹
- ASP.NET DataTable去掉重復(fù)行的2種方法
- ASP.NET中DataTable與DataSet之間的轉(zhuǎn)換示例
- ASP.NET怎么操作DataTable實(shí)例應(yīng)用
- Asp.net下使用Jquery Ajax傳送和接收DataTable的代碼
- asp.net 數(shù)據(jù)庫(kù)的連接和datatable類
- Asp.net靜態(tài)方法之Grid轉(zhuǎn)DataTable方法實(shí)現(xiàn)步驟
- Asp.net實(shí)現(xiàn)選擇性的保留DataTable中的列
- asp.net DataTable導(dǎo)出Excel自定義列名的方法
相關(guān)文章
.NetCore?Web?Api?利用ActionFilterAttribute統(tǒng)一接口返回值格式及問(wèn)題解析
在實(shí)際項(xiàng)目開發(fā)過(guò)程中,統(tǒng)一API返回值格式對(duì)前端或第三方調(diào)用將是非常必要的,在.NetCore中我們可以通過(guò)ActionFilterAttribute來(lái)進(jìn)行統(tǒng)一返回值的封裝,對(duì).NetCore?Web?Api?統(tǒng)一接口返回值格式相關(guān)知識(shí)感興趣的朋友一起看看吧2022-03-03MVC使用MvcPager實(shí)現(xiàn)分頁(yè)效果
這篇文章主要為大家詳細(xì)介紹了MVC使用MvcPager實(shí)現(xiàn)分頁(yè)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Asp.net mvc在view中用C#代碼動(dòng)態(tài)創(chuàng)建元素
這篇文章主要給大家介紹了關(guān)于Asp.net mvc如何在view中用C#代碼動(dòng)態(tài)創(chuàng)建元素的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03ASP.NET下母版頁(yè)和內(nèi)容頁(yè)中的事件發(fā)生順序整理
母版頁(yè)與內(nèi)容頁(yè)合并后事件的發(fā)生順序,有需要區(qū)別的朋友能用的到2009-03-03