您的位置:首页 > 编程语言 > ASP

在ASP.NET中使用用户控件

2006-07-30 15:03 465 查看


引用Word组件 MSWORD.OLB
private void btnOK_Click(object sender, System.EventArgs e)
{
try
{
object Nothing=System.Reflection.Missing.Value;//调用默认参数

//取得Word文件保存路径
object filename=@tbPath.Text;
//object visible=false;
//创建一个名为WordApp的组件对象
Word.Application WordApp=new Word.ApplicationClass();
//创建一个名为WordDoc的文档对象
object missing = System.Reflection.Missing.Value;
Word.Document WordDoc=WordApp.Documents.Add(ref missing,ref missing,ref missing,ref missing);
//增加一表格
Word.Table table=WordDoc.Tables.Add(WordApp.Selection.Range,1,1,ref Nothing,ref Nothing);
//在表格第一单元格中添加自定义的文字内容
table.Cell(1,1).Range.Text=tbBody.Text;
//在文档空白地方添加文字内容
WordDoc.Paragraphs.Last.Range.Text="欢迎参加.NET学习!";
//将WordDoc文档对象的内容保存为DOC文档
WordDoc.SaveAs(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
//关闭WordDoc文档对象
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭WordApp组件对象
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
//返回结果
lbResult.Text="文档路径:<a href='"+tbPath.Text+"'>"+tbPath.Text+"</a>(点击链接查看)<br>生成结果:成功!";
}
catch(Exception Ex)
{
string str = Ex.Message;
Response.Write(str);
}

引用Exel组件



public class Main : System.Web.UI.Page
{
// Excel object references.
private Excel.Application objExcel = null;
private Excel.Workbooks objBooks = null;
private Excel._Workbook objBook = null;
private Excel.Sheets objSheets = null;
private Excel._Worksheet objSheet = null;
private Excel.Range objRange = null;
private Excel.Font objFont = null;

// Frequenty-used variable for optional arguments.
private object objOpt = System.Reflection.Missing.Value;

// Paths used by the sample code for accessing and storing data.
private object strSampleFolder = "c://";
protected System.Web.UI.WebControls.Button btnAuto;
protected System.Web.UI.WebControls.Button btnAdo;
//使用ADO.NET方式反问
private void btnAdo_Click(object sender, System.EventArgs e)
{
// Establish a connection to the data source.
OleDbConnection objConn = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strSampleFolder +
"new.xls;Extended Properties=Excel 8.0;");
objConn.Open();
// Add two records to the table named 'MyTable'.
OleDbCommand objCmd = new OleDbCommand();
objCmd.Connection = objConn;
objCmd.CommandText = "CREATE TABLE MyTable2 (FirstName char(255),LastName char(255))";
objCmd.ExecuteNonQuery();
// objCmd.CommandText = "Insert into MyTable (FirstName, LastName)" +
// " values ('shao', 'zhidong')";
// objCmd.ExecuteNonQuery();
// objCmd.CommandText = "Insert into MyTable (FirstName, LastName)" +
// " values ('张', '三')";
// objCmd.ExecuteNonQuery();
// Close the connection.
objConn.Close();
}
//使用自动化方式
private void btnAuto_Click(object sender, System.EventArgs e)
{
// Start a new workbook in Excel.
objExcel = new Excel.Application();
objBooks = (Excel.Workbooks)objExcel.Workbooks;
objBook = (Excel._Workbook)(objBooks.Add(objOpt));
// Add data to cells of the first worksheet in the new workbook.
objSheets = (Excel.Sheets)objBook.Worksheets;
objSheet = (Excel._Worksheet)(objSheets.get_Item(1));
objRange = objSheet.get_Range("A1", objOpt);
objRange.set_Value(objOpt,"Last Name");
objRange = objSheet.get_Range("B1", objOpt);
objRange.set_Value(objOpt,"First Name");
objRange = objSheet.get_Range("A2", objOpt);
objRange.set_Value(objOpt,"shao");
objRange = objSheet.get_Range("B2", objOpt);
objRange.set_Value(objOpt,"zhidong");
// Apply bold to cells A1:B1.
objRange = objSheet.get_Range("A1", "B1");
objFont = objRange.Font;
objFont.Bold=true;
// Save the workbook and quit Excel.
objBook.SaveAs(strSampleFolder + "Book1.xls", objOpt, objOpt,
objOpt, objOpt, objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
objOpt, objOpt, objOpt, objOpt, objOpt);
objBook.Close(false, objOpt, objOpt);
objExcel.Quit();
}





用户控件的使用
<table bgcolor="<%=strColor%>" width="100%" cellpadding="10" cellspacing="0">
public abstract class header : System.Web.UI.UserControl
{ public string strColor = "Red";
private void Page_Load(object sender, System.EventArgs e)
{
}
}

private void btnApply_Click(object sender, System.EventArgs e)
{
lbColor.Text = ddlColor.SelectedItem.Text;
//改变用户控件背景色
Header1.strColor = ddlColor.SelectedItem.Text;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: