国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net 讀取Excel數(shù)據到DataTable的代碼

2024-07-10 13:26:21
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:


/// <summary>
/// 獲取指定路徑、指定工作簿名稱的Excel數(shù)據:取第一個sheet的數(shù)據
/// </summary>
/// <param>文件存儲路徑</param>
/// <param>工作簿名稱</param>
/// <returns>如果爭取找到了數(shù)據會返回一個完整的Table,否則返回異常</returns>
public DataTable GetExcelData(string astrFileName)
{
string strSheetName = GetExcelWorkSheets(astrFileName)[0].ToString();
return GetExcelData(astrFileName, strSheetName);
}



代碼

復制代碼 代碼如下:


/// <summary>
/// 返回指定文件所包含的工作簿列表;如果有WorkSheet,就返回以工作簿名字命名的ArrayList,否則返回空
/// </summary>
/// <param>要獲取的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("無法獲取指定Excel的架構。");
}
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中某些工作薄名稱為漢字的表無法正確識別的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;
}


代碼

復制代碼 代碼如下:


/// <summary>
/// 獲取指定路徑、指定工作簿名稱的Excel數(shù)據
/// </summary>
/// <param>文件存儲路徑</param>
/// <param>工作簿名稱</param>
/// <returns>如果爭取找到了數(shù)據會返回一個完整的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;
}


代碼

復制代碼 代碼如下:


/// <summary>
/// 獲取鏈接字符串
/// </summary>
/// <param></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();
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 盱眙县| 奉节县| 四川省| 开化县| 蓬莱市| 阿坝| 申扎县| 武穴市| 柳州市| 会泽县| 绍兴县| 根河市| 宁南县| 岳池县| 会宁县| 鹰潭市| 宿迁市| 临漳县| 五家渠市| 武宁县| 遂昌县| 同心县| 兖州市| 苏尼特左旗| 青神县| 罗江县| 阿图什市| 中西区| 兴安盟| 平乐县| 固始县| 安化县| 永州市| 冕宁县| 漳州市| 探索| 招远市| 绥德县| 木里| 察哈| 广安市|