手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >asp.net 读取Excel数据到DataTable的代码
asp.net 读取Excel数据到DataTable的代码
摘要:复制代码代码如下://////获取指定路径、指定工作簿名称的Excel数据:取第一个sheet的数据//////文件存储路径///工作簿名称...

复制代码 代码如下:

/// <summary>

/// 获取指定路径、指定工作簿名称的Excel数据:取第一个sheet的数据

/// </summary>

/// <param name="FilePath">文件存储路径</param>

/// <param name="WorkSheetName">工作簿名称</param>

/// <returns>如果争取找到了数据会返回一个完整的Table,否则返回异常</returns>

public DataTable GetExcelData(string astrFileName)

{

string strSheetName = GetExcelWorkSheets(astrFileName)[0].ToString();

return GetExcelData(astrFileName, strSheetName);

}

代码

复制代码 代码如下:

/// <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("无法获取指定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数据

/// </summary>

/// <param name="FilePath">文件存储路径</param>

/// <param name="WorkSheetName">工作簿名称</param>

/// <returns>如果争取找到了数据会返回一个完整的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 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 读取Excel数据到DataTable的代码】相关文章:

asp.net Linq把数据导出到Excel的代码

用asp.net c# HttpWebRequest获取网页源代码

Asp.net在线备份、压缩和修复Access数据库示例代码

c#将Excel数据导入到数据库的实现代码

asp.net中调用winrar实现压缩解压缩的代码

asp.net Repeater中使用if的代码

asp.net汉字转拼音和获取汉字首字母的代码

asp.net 备份和恢复数据库的方法

Asp.net 通用万级数据分页代码[修正下载地址]

asp.net 文章内容分页显示的代码

精品推荐
分类导航