ASPxRichEdit和ASPxSpreadsheet实现word和excel预览

 原理:

先用过时的webclient下载文件到tmp目录,然后再读取文件加载显示

这里是前端
<dx:ASPxRichEdit ID="ASPxRichEdit1" runat="server"  WorkDirectory="~\App_Data\WorkDirectory" Width="100%" Height="1920px"  ReadOnly="True" ShowFormulaBar="False" FullscreenMode="True" SettingsView-Mode="Reading" RibbonMode="None">
            </dx:ASPxRichEdit>

<dx:ASPxSpreadsheet runat="server" ID="excel" WorkDirectory="~/App_Data/WorkDirectory" Width="100%" ReadOnly="True" ShowFormulaBar="False" FullscreenMode="True" SettingsView-Mode="Reading" RibbonMode="None">
                <SettingsView Mode="Reading" />
            </dx:ASPxSpreadsheet>
这里是后端代码
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace docx
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //获取URL参数
            string urlFile=Request.QueryString["filename"];
            string id = Request.QueryString["id"];
            //获取配置
            string _directory = @ConfigurationSettings.AppSettings["down"];
            string url = @ConfigurationSettings.AppSettings["url"];

            if (urlFile == null)
            {
                Response.Write("<script>alert('似乎没有拿到文件名称?');</script>");

                return;
            }
            string file = df(url,id,urlFile, _directory);

            if (file.Contains(".xls"))
            {
                excel.ReadOnly = true;
                excel.ShowFormulaBar = false;
                excel.Open(_directory + file);
                ASPxRichEdit1.Visible = false;
            }
            if (file.Contains(".doc"))
            {
                ASPxRichEdit1.Open(_directory + file);
                ASPxRichEdit1.ReadOnly = true;
                excel.Visible = false;

            }




        }


        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="url">下载地址</param>
        /// <returns>文件名称</returns>
        [Obsolete]
        public string df(string url,string id,string urlFile,string _directory)
        {
            try
            {
                string fileName = "";

                if (urlFile.Contains(".xls"))  fileName = "tmp.xlsx";

                if (urlFile.Contains(".doc")) fileName = "tmp.docx";

                if (!Directory.Exists(_directory))
                {
                    Directory.CreateDirectory(_directory);
                }
                WebClient client = new WebClient();
                url = url + "&id=" + id.ToString() + "&field=content&download=true&" + "filename=" + urlFile;
                client.DownloadFile(url, _directory + fileName);
                return fileName;
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Write("<script>alert('文件下载错误?');</script>");
                return "";
            }
        }

    
    }

}
...	
     <appSettings>
		<!--文件下载目录-->
		<add key="down" value="c:\tmp\"/>
    <add key="url" value="http://127.0.0.1:666/web/cont?" />
	</appSettings>
</configuration>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xcagy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值