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

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

ASP.NET----实现网页中查看offic文件,word,ppt等_第1张图片

具体看下面代码:

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( " " , msg);
this .ClientScript.RegisterStartupScript( this .GetType(), " alert " , script);
}

protected void AlertMsg( string msg)
{
string script = string .Format( " " ,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 >

网盘下载:http://u.115.com/file/clia1ceu

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

你可能感兴趣的:(ASP.NET----实现网页中查看offic文件,word,ppt等)