组件:是对象的集合,功能完备的单元,他又对外的接口
1 如何使用已有的组件(例如Word和Excel)
首先你要将要引用的组件添加到程序中去
private void UserWord(string filenamepath)
{
try
{
object nothing=System.Reflection.Missing.Value;//调用默认值,以后只需要写nothing就可以了
//创建Word组件对象
Word.Application app=new Word.ApplicationClass();
//创建WordDoc的文档
Word.Document doc=app.Document.Add(ref nothing,ref nothing,ref nothing);
//增减表格
Word.Table tb=doc.Table.Add(app.Selection.Range,1,1,ref nithing,ref nothing);
tb.Cell(1,1).Range.Text="prolove";
//在文档的空白处添加文字
doc.Paragraphs.Last.Range.Text="2007-11-21";
doc.SaveAs(ref filenamepath,ref nothinf,ref nothing,ref nothing,...... );
doc.Close(ref nothing,ref nothing,ref nothing);
app.Quit(ref nothing,ref nothing,ref nothing);
}
catch
{
....
}
}
2)利用ADONET操作Excel
private void AddExcel(string filenamepath)
{
OleDbConnection conn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filenamepath+";Extended Properties=Excel 8.0");
conn.Open();
OleDb cmd=new OleDbCommand();
cmd.Connection=conn;
//创建一个新的工作表
//cmd.CommandText="create table prolove(username char(20),pwd char(20))";
// cmd.ExecuteNonQuery();
//向一个工作表中添加数据
cmd.CommandText="insert into prolove(username,pwd) values ('prolove','12345')";
cmd.ExecuteNonQuery();
conn.Close();
}
2 创建用户的控件(.ascx)
1).aspx是继承.cs文件的,所以你可以直接使用.cs中的方法和public 属性
例如:
.cs中
public string strColor="Red";
.aspx
<td bgcolor="<%=strcolor%>"></td>
2)怎样使用控件
直接拖动到窗口
//前缀 //名称 //来源
<%@ Register TagPrefix="uc1" TagName="KJ" Src="KJ.ascx" %>
通过编码方式
Control cr=LoadControl("KJ.ascx");
((KJ)cr).属性="数值";
Page.Controls.Add(cr);
组件
最新推荐文章于 2025-06-06 23:19:56 发布