ASP.NET----实现网页中查看offic文件,word,ppt等

原理很简单,实现一个树,先获取文件存放文件夹地址,然后遍历每个文档,填入到树节点中去,看下截图效果:

2011051319215595.jpg

具体看下面代码:

File.cs:

 
  
public partial class Files : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
if ( ! this .IsPostBack)
this .InitTree();
}

private void InitTree()
{
// 清除树节点
this .trvFiles.Nodes.Clear();
// 实例化根节点
TreeNode node = new TreeNode();
node.Text
= " 简历文件 " ;
node.Value
= "" ;

// 获取存放Word PPT文件的文件夹路径
string dicPath = this .Server.MapPath( " WordFiles " );
// 实例化文件夹对象
DirectoryInfo dic = new DirectoryInfo(dicPath);
// 循环获取Word文档文件
foreach (FileInfo file in dic.GetFiles( " *.doc " ))
{
// 实例化文件节点
TreeNode fileNode = new TreeNode();
// 赋值
fileNode.Text = file.Name;
fileNode.Value
= file.FullName;
// 添加至根节点中
node.ChildNodes.Add(fileNode);
continue ;
}

// 循环获取PPT文件
foreach (FileInfo file in dic.GetFiles( " *.ppt " ))
{
TreeNode fileNode
= new TreeNode();
fileNode.Text
= file.Name;
fileNode.Value
= file.FullName;
node.ChildNodes.Add(fileNode);
continue ;
}
// 展开文件夹节点
node.Expand();
this .trvFiles.Nodes.Add(node);
}

protected void trvFiles_SelectedNodeChanged( object sender, EventArgs e)
{
// 判断选中节点不是根节点
if ( ! string .IsNullOrEmpty( this .trvFiles.SelectedNode.Value))
{
// 拼装打开文件的JavaScript脚本
string js = " window.frames[0].location.href='{0}'; " ;
js
= string .Format(js, " WordFiles/ " + this .trvFiles.SelectedNode.Text);
// 执行脚本 即在DocShow.aspx页面打开相应节点
this .WriteJS(js);
}
}

protected void WriteJS( string msg)
{
string script = string .Format( " <script type='text/javascript' language='javascript'>{0}</script> " , msg);
this .ClientScript.RegisterStartupScript( this .GetType(), " alert " , script);
}

protected void AlertMsg( string msg)
{
string script = string .Format( " <script type='text/javascript' language='javascript'>window.alert('{0}');</script> " ,msg);
this .ClientScript.RegisterStartupScript( this .GetType(), " alert " ,script);
}


}

页面布局:

 
  
< body >
< form id ="form1" runat ="server" >
< div >
< table style ="border: none; padding: none; margin: none; width: 100%; height: 100%;" >
< tr >
< td style ="width:20%;height:100%;vertical-align:top;" >
< asp:TreeView ID ="trvFiles" runat ="server" ImageSet ="XPFileExplorer" NodeIndent ="15"
OnSelectedNodeChanged
="trvFiles_SelectedNodeChanged" >
< ParentNodeStyle Font-Bold ="False" />
< HoverNodeStyle Font-Underline ="True" ForeColor ="#6666AA" />
< SelectedNodeStyle BackColor ="#B5B5B5" Font-Underline ="False" HorizontalPadding ="0px"
VerticalPadding
="0px" />
< NodeStyle Font-Names ="Tahoma" Font-Size ="8pt" ForeColor ="Black" HorizontalPadding ="2px"
NodeSpacing
="0px" VerticalPadding ="2px" />
</ asp:TreeView >
</ td >
< td >
< iframe style ="scroll:auto;width:100%;height:800px;border:none;" src ="DocShow.aspx" >
</ iframe >
</ td >
</ tr >
</ table >
</ div >
</ form >
</ body >

转载于:https://www.cnblogs.com/brainmao/archive/2011/05/13/2045795.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值