ASP.NET 使用Aspose实现Office格式转换和在线预览
来源:互联网 发布:linux redmine 启动 编辑:程序博客网 时间:2024/06/03 17:47
有关于.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,目前不知道是不是限时的。
阅读全文
1 0
- ASP.NET 使用Aspose实现Office格式转换和在线预览
- .net 实现Office文件预览,word文件在线预览、excel文件在线预览、ppt文件在线预览,excel转html,office格式转换,
- Asp.net MVC 利用(aspose+pdfobject.js) 实现在线预览word、excel、ppt、pdf文件
- openoffice+pdf2swf实现在线转换office文档,实现在线预览
- Java实现在线预览附件 office转换PDF
- asp.net实现word在线预览功能
- Java实现在线预览office
- Java实现Office在线预览
- web使用openoffice实现在线预览office文档
- web使用openoffice实现在线预览office文档
- office转pdf和图片实现在线预览
- OFFICE 文档转换为html在线预览
- office转换为html在线预览
- Office在线预览及PDF在线预览的实现方式
- .net 实现Office文件预览
- java实现在线预览office文档
- Java office在线预览实现--openoffice
- Android实现在线预览office文档(Word,Pdf,excel,PPT.txt等格式)
- Java的内省
- 库编译自嘲
- vi复制粘贴剪切
- C++premer chapter 9
- MySQL双主(主主)架构方案
- ASP.NET 使用Aspose实现Office格式转换和在线预览
- tcp三次握手,四次挥手的原因
- PAT甲级1135
- Linxu搜索文件内容指令--grep
- angular4实战(1) angular-cli
- C++基本语法,入门及提高(6)
- JAVA线程池原理详解一
- Java设计模式——建造者模式(Builder Pattern)
- 收集整理一些常用的MySQL命令