高分求教,如何在VS2005的页面中嵌入显示Excel文件

sxuhai 2006-05-26 02:05:56
如题!!!
...全文
684 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
up
greki 2006-07-08
  • 打赏
  • 举报
回复
收藏
dqmayg 2006-05-29
  • 打赏
  • 举报
回复
up
MonkWang 2006-05-29
  • 打赏
  • 举报
回复
收藏
哥本哈士奇 2006-05-29
  • 打赏
  • 举报
回复
收藏了先
顺便帮顶
楼上都是高手
brbrm 2006-05-28
  • 打赏
  • 举报
回复
还可使用Spreadsheet控件

节选:
Spreadsheet控件是Microsoft Office Web Component程序集中提供的一个ActiveX控件,它能够直接嵌入到Windows窗体或Web页面当中。要在Visual Studio .NET开发环境的工具箱中显示该控件,可以通过菜单命令“工具 | 选择工具箱项”打开如图4.16所示的对话框,在其中的“COM组件”选项卡下选择“Microsoft Office Spreadsheet 11.0”选项(该选项所代表的控件对应于Excel 2003;对于Excel 2002和Excel2000,相应的控件版本分别是10.0和9.0),单击“确定”按钮即可.
该控件提供了一个完整的可交互的电子表格,它同样可以包含多个工作表,在其中不仅能够编辑单元格内容、设置表格样式,还能够进行数据排序和公式计算。此外,Spreadsheet控件能够容纳的单元格数量为262144行×18278列,这也大大超过了标准Excel工作表的容量。
Spreadsheet控件就在该程序集的AxOWC11命名空间下定义。该程序集还包含另一个命名空间OWC11,它和Microsoft..Office.Interop.Excel命名空间有很多相似的地方,例如它也定义了Workbooks、Workbook、Sheets、Worksheet、Range等对象(接口),表示Spreadsheet控件所使用的电子表格的相应内容。Spreadsheet控件对象的Sheets属性类型就是OWC11.Sheets,对于该集合中的Worksheet对象也可以通过Rows、Column、UsedRange属性以及get_Range方法来访问单元格内容。只要熟悉标准的Excel对象模型,掌握Spreadsheet控件的用法也就不是什么难事了。
Jianyi 2006-05-27
  • 打赏
  • 举报
回复
接着:

<!--titlebar start-->

<table width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="#FFCC00">
<tr>
<td bgcolor="#FFCC00" valign="top" width="250" rowspan="2"><img src="officelogo.gif" alt="Microsoft(R) Office" border="0" /></td>
<td bgcolor="#FFCC00" valign="middle" height="20" align="right" class="fontSize1" nowrap="true">DSO Framer ActiveX Document Control Sample  </td>
</tr>
<tr><td bgcolor="#FFCC00" valign="top" height="40" align="right"><a href="http://www.microsoft.com/"><img src="mslogo.gif" border="0" alt="microsoft.com"></a></td></tr>
<tr><td bgcolor="#FFFFFF" valign="top" height="2"></td><td bgcolor="#FFFFFF" valign="top" height="2"></td></tr>
<tr><td bgcolor="#FFCC00" valign="top" height="2"></td><td bgcolor="#FFCC00" valign="top" height="2"></td></tr>
<tr><td bgcolor="#FFFFFF" valign="top" height="5"></td><td bgcolor="#FFFFFF" valign="top" height="5"></td></tr>
</table>

<!--titlebar end-->

<!--main body start-->

<table width="100%" height="87%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td valign="top" width="10"></td>
<td valign="top" width="175">

<!--left-side start-->

<table cellpadding="0" cellspacing="0" border="0" width="175" bgcolor="#f1f1f1">

<!-- Load a Document -->
<tr>
<td valign="top" bgcolor="#FFCC00" colspan="2"><img src="lefttopcurve.gif" width="10" height="10"></td>
<td valign="middle" bgcolor="#FFCC00" class="fontSize1" width="155" rowspan="2"><B>Load a Document</B></td>
<td valign="top" bgcolor="#FFCC00" colspan="2"><img src="righttopcurve.gif" width="10" height="10"></td>
</tr>
<tr>
<td width="1" bgcolor="#D6D3D6"></td><td bgcolor="#FFCC00" width="9" height="9"></td>
<td bgcolor="#FFCC00" width="9" height="10"></td><td width="1" bgcolor="#D6D3D6"></td>
</tr>
<tr>
<td width="1" bgcolor="#D6D3D6"></td>
<td width="9"></td>
<td class="fontSize1" width="155"><br>
<div class="fakehlink" onClick="NewDoc">Create a New Document</div><br>
<div class="fakehlink" onClick="OpenDoc">Open a Document</div><br>
<div class="fakehlink" onClick="OpenWebDoc">Open a Web Document</div><br>
</td>
<td width="9"></td>
<td width="1" bgcolor="#D6D3D6"></td>
</tr>

<!-- Perform Document Task -->
<tr>
<td width="1" bgcolor="#D6D3D6"></td><td width="9" height="20" bgcolor="#FFCC00"></td>
<td valign="middle" bgcolor="#FFCC00" class="fontSize1" width="155"><B>Perform Document Task</B></td>
<td width="9" height="20" bgcolor="#FFCC00"></td><td width="1" bgcolor="#D6D3D6"></td>
</tr>
<tr>
<td width="1" bgcolor="#D6D3D6"></td>
<td width="9"></td>
<td class="fontSize1" width="155"><br>
<div class="fakehlink" onClick="SaveCopyDoc">Save a Copy</div><br>
<div class="fakehlink" onClick="ChgLayout">Change Page Layout</div><br>
<div class="fakehlink" onClick="PrintDoc">Print the Document</div><br>
<div class="fakehlink" onClick="CloseDoc">Close the Document</div><br>
</td>
<td width="9"></td>
<td width="1" bgcolor="#D6D3D6"></td>
</tr>

<!-- Control Appearance -->
<tr>
<td width="1" bgcolor="#D6D3D6"></td><td width="9" height="20" bgcolor="#FFCC00"></td>
<td valign="middle" bgcolor="#FFCC00" class="fontSize1" width="155"><B>Control Appearance</B></td>
<td width="9" height="20" bgcolor="#FFCC00"></td><td width="1" bgcolor="#D6D3D6"></td>
</tr>
<tr>
<td width="1" bgcolor="#D6D3D6"></td>
<td width="9"></td>
<td class="fontSize1" width="155"><br>
<div class="fakehlink" onClick="ToggleTitlebar">Show/Hide Titlebar</div><br>
<div class="fakehlink" onClick="ToggleToolbars">Show/Hide Toolbars</div><br>
<div class="fakehlink" onClick="ToggleMenubar">Show/Hide Menubar</div><br>
</td>
<td width="9"></td>
<td width="1" bgcolor="#D6D3D6"></td>
</tr>


<tr><td colspan="5"><img src="bottomcurve.gif" width="175" height="10"></td></tr>
</table>

<p class="fontSize1"><span id="tstat">Click on the document icon on the titlebar to show the file menu, or use the commands above to perform an action.</span>

<!--left-side end-->

</td>
<td valign="top" width="10"></td>
<td valign="top">

<!--right-side start-->

<table width="100%" height="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td valign="top"></td>
<td valign="top" colspan="2">
<object classid="clsid:00460182-9E5E-11d5-B7C8-B8269041DD57" id="oframe" width="100%" height="100%">
<param name="BorderStyle" value="1">
<param name="TitlebarColor" value="52479">
<param name="TitlebarTextColor" value="0">
<param name="Menubar" value="1">
</object>
</td>
</tr>
<tr>
<td valign="top" height="15"></td>
<td valign="bottom" class="fontSize1" colspan="2">Copyright (c)2001-2004 Microsoft Corporation. All rights reserved.</td>
</tr>
</table>

<!--right-side end-->

</td>
<td valign="top" width="10"></td>
</tr>
</table>

<!--main body end-->

</body>
</html>
Jianyi 2006-05-27
  • 打赏
  • 举报
回复
感谢 LoveCherry , 以下的代码可行:

<html>
<head><title>DsoFramer Control Test Page (KB311765)</title>

<style>
.fontSize1 {font-size: 65%; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;}
.fontSize2 {font-size: 70%; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;}
.fontSize3 {font-size: 75%; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;}
.fontSize4 {font-size: 80%; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;}
.fontSize5 {font-size: 125%; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;}

.fakehlink {cursor: hand; text-decoration: underline; color: #0066CC; font-weight:normal;}

</style>

</head>
<body bgcolor="#ffffff" topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">

<!-- web page script -->

<script language="VBScript">
Dim bDocOpen

Sub oframe_OnDocumentOpened(str, obj)
Dim s, s2
On Error Resume Next
bDocOpen = True

if len(str) = 0 then
str = "New Document"
else
Dim x
x = InStr(str, "\")
if x then
do
str = mid(str, x+1)
x = Instr(str, "\")
loop while x > 0
else
x = Instr(str, "/")
if x Then
do
str = mid(str, x+1)
x = Instr(str, "/")
loop while x > 0
end if
end if
end if

s = obj.Application.Name
if len(s) = 0 then s = "Unknown Server"

document.all.tstat.InnerHTML = "File: " & str & "<br>Server: " & s
End Sub

Sub oframe_OnDocumentClosed()
bDocOpen = False
document.all.tstat.InnerHTML = "Try another file."
End Sub

Sub NewDoc()
On Error Resume Next
oframe.showdialog 0 'dsoDialogNew
if err.number then
MsgBox "Unable to Create New Object: " & err.description
end if
End Sub

Sub OpenDoc()
On Error Resume Next
document.all.oframe.showdialog 1 'dsoDialogOpen
if err.number then
MsgBox "Unable to Open Document: " & err.description
end if
End Sub

Sub OpenWebDoc()
Dim sUrl
On Error Resume Next
sUrl = InputBox("Type the URL to a file on a Web Folder...", ,"http://[server]/[folder]/test.doc")
If Len(sUrl) Then
oframe.open sUrl, true
if err.number then
MsgBox "Unable to Open URL: " & err.description
end if
End If
End Sub

Sub SaveCopyDoc()
On Error Resume Next
If Not bDocOpen Then
MsgBox "You do not have a document open."
Else
oframe.showdialog 3 'dsoDialogSaveCopy
End If
End Sub

Sub ChgLayout()
On Error Resume Next
If Not bDocOpen Then
MsgBox "You do not have a document open."
Else
oframe.showdialog 5 'dsoDialogPageSetup
End If
End Sub

Sub PrintDoc()
On Error Resume Next
If Not bDocOpen Then
MsgBox "You do not have a document open."
Else
oframe.printout True
End If
End Sub

Sub CloseDoc()
On Error Resume Next
If Not bDocOpen Then
MsgBox "You do not have a document open."
Else
oframe.close
End If
End Sub

Sub ToggleTitlebar()
Dim x
On Error Resume Next
x = oframe.Titlebar
oframe.Titlebar = Not x
End Sub

Sub ToggleToolbars()
Dim x
On Error Resume Next
x = oframe.Toolbars
oframe.Toolbars = Not x
End Sub

Sub ToggleMenubar()
Dim x
On Error Resume Next
x = oframe.Menubar
oframe.Menubar = Not x
oframe.Activate
End Sub
</script>
hccl 2006-05-27
  • 打赏
  • 举报
回复
学习学习~~~
gedeng_2000 2006-05-27
  • 打赏
  • 举报
回复
Asp.net(C#)群主有很多资料...哈哈!QQ群109473350
sxuhai 2006-05-27
  • 打赏
  • 举报
回复
用DataGrid来显示的办法我知道,但用来显示Excel的报表无法按原有格式显示出来。
zsm0071982 2006-05-27
  • 打赏
  • 举报
回复
本节将通过一个程序来介绍Visual C#读取Excel表格中的数据,并把数据以DataGrid的形式显示出来。

  (1).如何读取数据:

  其实读取Excel表格中的数据和读取数据库中的数据是非常类似的,因为在某种程度上Excel表格可以看成是一张一张的数据表。其二者的主要区别在于所使用的数据引擎不一样。在本文的程序中,通过下列代码实现读取Excel表格数据,具体如下:


//创建一个数据链接
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = c:\\sample.xls;Extended Properties=Excel 8.0" ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom = " SELECT * FROM [Sheet1$] " ;
myConn.Open ( ) ;
file://打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
file://创建一个 DataSet对象
myDataSet = new DataSet ( ) ;
file://得到自己的DataSet对象
myCommand.Fill ( myDataSet , "[Sheet1$]" ) ;
file://关闭此数据链接
myConn.Close ( ) ;


  怎么样读取Excel表格中的数据其实和读取数据库中的数据没有什么实质上的区别。

  注释:这里读取的是C盘根目录下的"Sample.xls"文件。

  (2).用DataGrid来显示得到的数据集:

  在得到DataSet对象后,只需要通过下列二行代码,就可以把数据集用DataGrid显示出来了:


DataGrid1.DataMember= "[Sheet1$]" ;
DataGrid1.DataSource = myDataSet ;

  (3).用Visual C#读取Excel表格,并用DataGrid显示出来的程序代码(Read.cs)和程序运行的界面:

  掌握了上面二点,水到渠成就可以得到以下代码:


using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
using System.Data.OleDb ;
public class Form1 : Form
{
private Button button1 ;
private System.Data.DataSet myDataSet ;
private DataGrid DataGrid1 ;
private System.ComponentModel.Container components = null ;

public Form1 ( )
{
file://初始化窗体中的各个组件
InitializeComponent ( ) ;
file://打开数据链接,得到数据集
GetConnect ( ) ;
}
file://清除程序中使用过的资源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}

private void GetConnect ( )
{
file://创建一个数据链接
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = c:\\sample.xls;Extended Properties=Excel 8.0" ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom = " SELECT * FROM [Sheet1$] " ;
myConn.Open ( ) ;
file://打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
file://创建一个 DataSet对象
myDataSet = new DataSet ( ) ;
file://得到自己的DataSet对象
myCommand.Fill ( myDataSet , "[Sheet1$]" ) ;
file://关闭此数据链接
myConn.Close ( ) ;
}
private void InitializeComponent ( )
{
DataGrid1 = new DataGrid ( ) ;
button1 = new Button ( ) ;
SuspendLayout ( ) ;
DataGrid1.Name = "DataGrid1";
DataGrid1.Size = new System.Drawing.Size ( 400 , 200 ) ;

button1.Location = new System.Drawing.Point ( 124 , 240 ) ;
button1.Name = "button1" ;
button1.TabIndex = 1 ;
button1.Text = "读取数据" ;
button1.Size = new System.Drawing.Size (84 , 24 ) ;
button1.Click += new System.EventHandler ( this.button1_Click ) ;

this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
this.ClientSize = new System.Drawing.Size ( 400 , 280 ) ;
this.Controls.Add ( button1 ) ;
this.Controls.Add ( DataGrid1 ) ;
this.Name = "Form1" ;
this.Text = "读取Excle表格中的数据,并用DataGrid显示出来!" ;
this.ResumeLayout ( false ) ;

}
private void button1_Click ( object sender , System.EventArgs e )
{
DataGrid1.DataMember= "[Sheet1$]" ;
DataGrid1.DataSource = myDataSet ;

}
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
}
 
  以上只是读取了Excel表格中"Sheet1"中的数据,对于其他"Sheet"中的内容,可以参照读取"Sheet1"中的程序,只作一点修改就可以了,譬如要读取"Sheet2"中的内容,只需要把"Read.cs"程序中的"Sheet1$"改成"Sheet2$"就可以了。

sxuhai 2006-05-26
  • 打赏
  • 举报
回复
再顶
sxuhai 2006-05-26
  • 打赏
  • 举报
回复
up
baiyashan 2006-05-26
  • 打赏
  • 举报
回复
帮顶
LoveCherry 2006-05-26
  • 打赏
  • 举报
回复
Visual C++ ActiveX Control for hosting Office documents in Visual Basic or HTML
http://support.microsoft.com/default.aspx?scid=kb;en-us;311765

如何在Web页面上直接打开、编辑、创建Office文档
http://blog.joycode.com/kaneboy/archive/2004/11/03/37889.aspx
ilovejolly 2006-05-26
  • 打赏
  • 举报
回复
mark

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧