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

首頁(yè) > 編程 > .NET > 正文

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

2020-01-18 00:53:25
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
復(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();
}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 五华县| 吉木萨尔县| 武清区| 乌苏市| 扎鲁特旗| 特克斯县| 壤塘县| 冷水江市| 瓮安县| 德江县| 宿松县| 兴海县| 兴化市| 敖汉旗| 平武县| 景德镇市| 香河县| 体育| 浪卡子县| 临城县| 台北县| 察哈| 东海县| 淮北市| 磐安县| 雅安市| 泰来县| 泾阳县| 汉寿县| 云龙县| 赣州市| 海安县| 汉川市| 商都县| 上饶县| 咸丰县| 琼结县| 安化县| 璧山县| 海门市| 星座|