如何将Excel表格作为数据源绑定到DataGird
2006-03-26 10:17
489 查看
使用OleDb数据提供者可以对多种数据进行操作,本例说明如何操纵Excel表格。
首先,需要建立具有严格结构的Excel表格ExcelData.xls,并在菜单中选择:插入——名称;这个名称相当于select语句中的table名。将Excel表格放在主目录下。
程序代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ExcelCSTest
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("../ExcelData.xls") + ";Extended Properties=Excel 8.0;"; //关键是这句
try{
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1, "XLData");
DataGrid1.DataSource = objDataset1.Tables[0].DefaultView;
DataGrid1.DataBind();
objConn.Close();
}catch(Exception ex){
Label1.Text = ex.Message;
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
首先,需要建立具有严格结构的Excel表格ExcelData.xls,并在菜单中选择:插入——名称;这个名称相当于select语句中的table名。将Excel表格放在主目录下。
程序代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ExcelCSTest
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("../ExcelData.xls") + ";Extended Properties=Excel 8.0;"; //关键是这句
try{
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1, "XLData");
DataGrid1.DataSource = objDataset1.Tables[0].DefaultView;
DataGrid1.DataBind();
objConn.Close();
}catch(Exception ex){
Label1.Text = ex.Message;
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
相关文章推荐
- 如何将枚举类型作为数据源绑定在一个DROPDOWNLIST上,并指定用户可见枚举的真实值是名称
- 如何将Excel作为UltraWebGrid 数据源绑定到UltraWebGrid中?
- 如何将XML作为数据源绑定到控件
- Sqldatareader 绑定后如何在ItemDataBound事件获取数据源
- WPF使用 ObjectDataProvider 为方法作为绑定数据源
- vue中如何动态绑定图片,vue中通过data返回图片路径的方法
- C# Winform 对用BindingList(List)作为数据源绑定的datagridview进行上下行移动操作
- 如何绑定到根元素的数据源
- C#_以SQLDataReader为数据源,实现TreeView与ListView之间的动态绑定
- [翻译]如何使用webservice作为数据源去生成Microsoft Reporting Services 2005的报表
- [技巧]DataGird的hyper column的url field 绑定两个字段
- 11-28 自己写一个数据集DataSet---ds,向其中添加一个数据表DataTable ---dt,并创建该表的列和行。然后将本本表作为数据源绑定到GradView中。
- VS2005下水晶报表如何实现动态数据源绑定?
- Redis学习笔记~把redis放在DATA层,作为一种数据源,我认为更合理,也更符合我的面向对象原则
- 如何将自己的App作为外部数据源提供给Android系统搜索?
- 如何将数据源绑定在essential grid for JavaScript网格控件上
- datagird 绑定hyperlink 连接
- windows版jmeter的body data如何用\n作为“换行”
- Winform中Datagird数据输出到Excel表格,C# .NET
- 把txt作为数据源绑定到GridView中