全国统一热线:
028-86758058
18980748058
购买流程
付款方式
常见问题
在线提问
续租服务
购物车(
0
件)
用户名:
密 码:
记住
首 页
HOME
域名注册
DOMAIN
虚拟主机
WEB HOST
成品网站超市
AUTO Site
VPS主机
VPS SERVER
云 主 机
CLOUD HOST
租用托管
SERVER
海外主机
HK HOST
代理专区
AGENT
客服中心
SERVICE
站长教程
SERVICE
欢迎光临西部数据云计算中心,我们将竭诚为您提供最优质的服务!
中文域名
域名转入
域名转出
DNS管理
动态域名
获取域名证书
域名停放
域名过户
集群主机
双线主机
基本主机
港台主机
论坛主机
Linux主机
Vip合租主机
超G型主机
ASP.net主机
Java主机
智能建站主机
网店主机
美国主机
数据库
成品网站超市
智能建站主机
集群VPS主机
国内VPS主机
香港VPS主机
美国VPS主机
云主机介绍
云主机购买
服务器租用
主机托管
常见问题
香港主机
港台主机
美国主机
国内免备案
步骤流程
代理级别
代理合同
代理模式
代理优势
在线申请
产品列表
常见问题
代理商分布图
常见问题
有问必答
跟踪提问
购买流程
产品价格
付款方式
常用软件
网站备案
续租服务
汇款确认
相关文档
联系我们
域名资讯
主机资讯
行业动态
网页制作
php教程
mysql教程
域名资讯
云计算资讯
网络编程
您当前的位置:
首页
>
网络编程
>
Net教程
asp.net 读取Excel数据到DataTable的代码
时间:2015-01-29 来源:互联网 作者:佚名
复制代码
代码如下:
/// <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 Web Service 接口大量数据
具体请看下面的流程图及其说明 1,流程图2,流程说明:线程1
C#通过POP3获取邮件的代码(正文和附
使用方法: 获取第1封邮件 复制代码 代码如下: Zgke.Net.POP3
相关文章
ADO.NET 读取EXCEL的实现代码((c#))
c# datatable用法总结
asp.net动态获取Excel表名的函数代码
asp.net中操作Excel助手相关代码
ASP.NET操作EXCEL的总结篇
ASP.NET中生成Excel遇到的问题及改进方法
Asp.net操作Excel更轻松的实现代码
Asp.net生成Excel文件并下载(更新:解决使用迅雷下载
ASP.NET怎么操作DataTable实例应用
asp.net与excel互操作实现代码
栏目更新
栏目热门
返回首页
关于我们
联系我们
付款方式
价格总览
资讯中心
友情链接
媒体关注
有问必答
投诉建议
网站备案
《中华人民共和国增值电信业务经营许可证》编号:川B2-20080058号
官方网址:
xibushuju.com
西部数据
Copyright © 2002~2015
天府快车
版权所有
电话总机:
028-86758058
(50线) 传真:
028-86758058