ASP.NET 使用Aspose实现Office格式转换和在线预览

有关于.net 仿百度文库在线预览Word、Excel、PPT等office相关文件。这里我们使用Aspose相关组件,将office文件转化为Pdf格式文件存储或在线预览(Word可以存为Png格式,但是直接保存只会保存第一页)。

Aspose.Words.dll 操作Word

首先说转换为PDF格式存储和显示:

/// <summary>
        /// 将Word转换为Pdf 
        /// </summary>
        /// <param name="strFilePath">文件地址</param>
        /// <param name="savepath">保存地址</param>
        /// <returns></returns>
        public bool Word2Pdf(string strFilePath,string savepath)
        {

            try
            {

                Aspose.Words.Document doc = new Aspose.Words.Document(strFilePath);
                //将Word保存到指定路径下 savepath:保存路径
                doc.Save(savepath, Aspose.Words.SaveFormat.Pdf);
                //将Word保存至数据流中 再通过Response显示在页面中
                MemoryStream memStream = new MemoryStream();
                doc.Save(memStream, Aspose.Words.SaveFormat.Pdf);
                byte[] bt = memStream.ToArray();
                Response.ContentType = "application/pdf";
                Response.OutputStream.Write(bt, 0, bt.Length);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

然后,在线以图片的形式显示在网页上,分页显示调用一次显示一张图片,可以添加js做成类似于翻书的效果。

/// <summary>
        /// 将Word转换为Png 
        /// </summary>
        /// <param name="filepath">文件地址</param>
        /// <param name="pageIndex">要转换的页</param>
        /// <returns></returns>
public void Word2Png(string filepath, int pageIndex)
        {
            MemoryStream memStream = new MemoryStream();
            Aspose.Words.Document doc = new Aspose.Words.Document(filepath);
            PageInfo pageInfo = Document.GetPageInfo(pageIndex);
            float scale = 100 / 100.0f;
            const int Resolution = 96;
            Size imgSize = pageInfo.GetSizeInPixels(scale, Resolution);
            using (Bitmap img = new Bitmap(imgSize.Width, imgSize.Height))
            {
                img.SetResolution(Resolution, Resolution);
                using (Graphics gfx = Graphics.FromImage(img))
                {
                    gfx.Clear(Color.White);
                    Document.RenderToScale(pageIndex, gfx, 0, 0, scale);
                    img.Save(memStream, ImageFormat.Png);
                    }
                }
            }
            // Send the bitmap data to the output stream.
            Response.ContentType = "image/png";
            byte[] imageData = memStream.ToArray();
            Response.OutputStream.Write(imageData, 0, imageData.Length);
        }

Aspose.Cells.dll转换Excel为Pdf

将Excel转换为Pdf格式显示或保存。

/// <summary>
        /// 将Word转换为Pdf 
        /// </summary>
        /// <param name="strFilePath">文件地址</param>
        /// <param name="savepath">保存地址</param>
        /// <returns></returns>
public bool Excel2Pdf(string fileName, string savepath)
        {
            try
            {
                Aspose.Cells.Workbook excel = new Aspose.Cells.Workbook(fileName);
                //将ppt保存到指定路径下 savepath:保存路径
                excel.Save(savepath, Aspose.Cells.SaveFormat.Pdf);
                //将ppt保存至数据流中 再通过Response显示在页面中
                MemoryStream memStream = new MemoryStream();
                excel.Save(memStream, Aspose.Cells.SaveFormat.Pdf);
                byte[] bt = memStream.ToArray();
                Response.ContentType = "application/pdf";
                Response.OutputStream.Write(bt, 0, bt.Length);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

Aspose.Slides.dll 操作PPT

首先说转换为PDF格式存储和显示:

/// <summary>
        /// 转换ppt文件在线预览
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name="savepath">保存路径</param>
        /// <returns></returns>
        public bool ppt2Pdf(string fileName, string savepath)
        {
            try
            {
                Aspose.Slides.Presentation ppt = new Aspose.Slides.Presentation(fileName);
                //将ppt保存到指定路径下 savepath:保存路径
                ppt.Save(savepath, Aspose.Slides.Export.SaveFormat.Pdf);
                //将ppt保存至数据流中 再通过Response显示在页面中
                MemoryStream memStream = new MemoryStream();
                ppt.Save(memStream, Aspose.Slides.Export.SaveFormat.Pdf);
                byte[] bt = memStream.ToArray();
                Response.ContentType = "application/pdf";
                Response.OutputStream.Write(bt, 0, bt.Length);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

官网地址:www.aspose.com.
官方是要钱的,免费的大多数会有水印,不过神通广大的网友们还是有提供去除水印的dll,目前不知道是不是限时的。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值