asp.net 读取Excel数据到DataTable的代码
更新时间:2010年03月16日 19:48:40 作者:
asp.net 读取Excel数据到DataTable的代码,需要的朋友可以参考下。
复制代码 代码如下:
/// <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实现导出DataTable数据到Word或者Excel的方法
- asp.net实现数据从DataTable导入到Excel文件并创建表的方法
- Asp.net中DataTable导出到Excel的方法介绍
- ASP.NET DataTable去掉重复行的2种方法
- ASP.NET中DataTable与DataSet之间的转换示例
- ASP.NET怎么操作DataTable实例应用
- Asp.net下使用Jquery Ajax传送和接收DataTable的代码
- asp.net 数据库的连接和datatable类
- Asp.net静态方法之Grid转DataTable方法实现步骤
- Asp.net实现选择性的保留DataTable中的列
- asp.net DataTable导出Excel自定义列名的方法
相关文章
.NET使用js制作百度搜索下拉提示效果(不是局部刷新)实现思路
搞了个不是局部刷新的百度搜索框下拉提示效果大致思路:前台放一个input标签,然后当该标签内的值输入有变化的时候,调用后台代码查询 符合条件的数据绑定ListBox,感兴趣的朋友可以了解下2013-01-01
asp.net Google的translate工具翻译 API
很久前的一天,我想使用这个东西,然后看了下,GooGle的Translate工具目前没有公开API,还是一个逐渐完善的过程,另一方面,利用一段很小的程序就可以得到我们想要的效果。2008-12-12
.net6环境下使用RestSharp请求GBK编码网页乱码的解决方案
这篇文章介绍了.net6环境下使用RestSharp请求GBK编码网页乱码的解决方案,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2021-12-12
ASP.NET Core MVC基础学习之局部视图(Partial Views)
这篇文章主要给大家介绍了关于ASP.NET Core MVC基础学习之局部视图(Partial Views)的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用ASP.NET Core MVC具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧2019-08-08


最新评论