国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 浦东新区| 沽源县| 岳阳县| 屯留县| 柘城县| 巫溪县| 汾西县| 深州市| 宁国市| 惠来县| 达州市| 荆州市| 加查县| 洱源县| 新兴县| 三河市| 昌邑市| 玉环县| 宽甸| 德清县| 新巴尔虎右旗| 南乐县| 壶关县| 竹北市| 方山县| 汕头市| 黄平县| 名山县| 南部县| 潮州市| 二连浩特市| 筠连县| 嘉兴市| 阳城县| 益阳市| 井陉县| 通辽市| 鄂托克前旗| 静海县| 西贡区| 新闻|