Asp.Net基于WPS预览Word和Excel
用浏览器做在线Office文件可以调用微软Office的接口把Word和Excel转换成html再展示。也能基于WPS吧Word和Excel转换成PDF做在线预览。
基于Office做文件转换,服务器需要装引用的Office软件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Office.Interop.Excel;namespace LIS.Office14
{/// <summary>/// Excel预览/// </summary>public class ExcelPreview{public static bool Priview(System.Web.UI.Page p, string inFilePath, string outDirPath = ""){try{Microsoft.Office.Interop.Excel.Application excel = null;Microsoft.Office.Interop.Excel.Workbook xls = null;excel = new Microsoft.Office.Interop.Excel.Application();object missing = Type.Missing;object trueObject = true;excel.Visible = false;excel.DisplayAlerts = false;FileInfo inFileInfo = new FileInfo(inFilePath);string randomName = inFileInfo.Name.Replace(inFileInfo.Extension, "");xls = excel.Workbooks.Open(inFilePath, missing, trueObject, missing,missing, missing, missing, missing, missing, missing, missing, missing,missing, missing, missing);//保存Excel为htmlobject format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;//当前工作簿Workbook wsCurrent = xls;String outputFile = outDirPath + randomName + ".htm";wsCurrent.SaveAs(outputFile, format, missing, missing, missing,missing, XlSaveAsAccessMode.xlNoChange, missing,missing, missing, missing, missing);wsCurrent.Close();excel.Quit();#region 删除文件foreach (string fp in Directory.GetFiles(outDirPath)){FileInfo fi = new FileInfo(fp);//超过3000分钟的删除if ((DateTime.Now - fi.CreationTime).Minutes > 3000){File.Delete(fp);string directoryPath1 = fp.Replace(".htm", ".files");if (Directory.Exists(directoryPath1)){foreach (string fp1 in Directory.GetFiles(directoryPath1)){File.Delete(fp1);}Directory.Delete(directoryPath1);}}}#endregion//重定向生成的htmlp.Response.Redirect("../tmp/" + randomName + ".htm");}catch (Exception ex){p.Response.Write("14异常" + ex.Message);return false;}return true;}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
using System.IO;namespace LIS.Office14
{/// <summary>/// 预览Word/// </summary>public class WordPreview{public static bool Priview(System.Web.UI.Page p, string inFilePath, bool isToPdf, string outDirPath = ""){try{object missingType = Type.Missing;object readOnly = true;object isVisible = false;object documentFormat = 8;FileInfo inFileInfo = new FileInfo(inFilePath);string randomName = inFileInfo.Name.Replace(inFileInfo.Extension, "");object htmlFilePath = outDirPath + randomName + ".htm";string directoryPath = outDirPath + randomName + ".files";if (isToPdf == true){string pdfPath = outDirPath + randomName + ".pdf";bool pdfRet = WordToPDF(inFilePath, pdfPath);if (pdfRet == true){//重定向打开生成的htmlp.Response.Redirect("../tmp/" + randomName + ".pdf", false);return true;}}object filePath = inFilePath;//后台打开文档Microsoft.Office.Interop.Word.ApplicationClass applicationclass = new Microsoft.Office.Interop.Word.ApplicationClass();applicationclass.Documents.Open(ref filePath,ref readOnly,ref missingType, ref missingType, ref missingType,ref missingType, ref missingType, ref missingType,ref missingType, ref missingType, ref isVisible,ref missingType, ref missingType, ref missingType,ref missingType, ref missingType);applicationclass.Visible = false;Document document = applicationclass.ActiveDocument;//保存文档为htmldocument.SaveAs(ref htmlFilePath, ref documentFormat, ref missingType,ref missingType, ref missingType, ref missingType,ref missingType, ref missingType, ref missingType,ref missingType, ref missingType, ref missingType,ref missingType, ref missingType, ref missingType,ref missingType);//关闭文档document.Close(ref missingType, ref missingType, ref missingType);applicationclass.Quit();foreach (System.Diagnostics.Process pro in System.Diagnostics.Process.GetProcessesByName("WINWORD")){pro.Kill();}#region 删除文件foreach (string fp in Directory.GetFiles(outDirPath)){FileInfo fi = new FileInfo(fp);//超过3000分钟的删除if ((DateTime.Now - fi.CreationTime).Minutes > 3000){File.Delete(fp);string directoryPath1 = fp.Replace(".htm", ".files");if (Directory.Exists(directoryPath1)){foreach (string fp1 in Directory.GetFiles(directoryPath1)){File.Delete(fp1);}Directory.Delete(directoryPath1);}}}#endregion//重定向打开生成的htmlp.Response.Redirect("../tmp/" + randomName + ".htm", false);}catch (Exception ex){p.Response.Write("14异常" + ex.Message);return false;}return true;}/// <summary>/// 把word转PDF再展示/// </summary>/// <param name="sourcePath"></param>/// <param name="targetPath"></param>/// <returns></returns>public static bool WordToPDF(string sourcePath, string targetPath){bool result = false;Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();Document document = null;try{application.Visible = false;document = application.Documents.Open(sourcePath);document.ExportAsFixedFormat(targetPath, WdExportFormat.wdExportFormatPDF);result = true;}catch (Exception e){result = false;}finally{document.Close();application.Quit();foreach (System.Diagnostics.Process pro in System.Diagnostics.Process.GetProcessesByName("WINWORD")){pro.Kill();}}return result;}}}
基于WPS做转换,服务器需要装WPS软件。
需要在工程右键添加引用里选Com栏目引用Com主键里的
代码部分
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Word;
using Excel;namespace LIS.WPS
{/// <summary>/// wps转PDF/// </summary>public static class WpsToPdf{/// <summary>/// word转pdf/// </summary>/// <param name="source">源<see cref="string"/>.</param>/// <param name="newFilePath">新文件路径<see cref="string"/>.</param>/// <returns>The <see cref="bool"/>.</returns>public static bool WordWpsToPdf(string source, string newFilePath){var type = Type.GetTypeFromProgID("KWps.Application");dynamic wps = Activator.CreateInstance(type);try{//用wps打开word不显示界面dynamic doc = wps.Documents.Open(source, Visible: false);//转pdfdoc.ExportAsFixedFormat(newFilePath, WdExportFormat.wdExportFormatPDF);//设置隐藏菜单栏和工具栏//wps.setViewerPreferences(PdfWriter.HideMenubar | PdfWriter.HideToolbar);doc.Close();}catch (Exception e){//添加你的日志代码return false;}finally{wps.Quit();GC.Collect();GC.WaitForPendingFinalizers();}return true;}/// <summary>/// excel转pdf/// </summary>/// <param name="source">源<see cref="string"/>.</param>/// <param name="newFilePath">新文件路径<see cref="string"/>.</param>/// <returns>The <see cref="bool"/>.</returns>public static bool ExcelToPdf(string source, string newFilePath){var type = Type.GetTypeFromProgID("KET.Application");dynamic wps = Activator.CreateInstance(type);try{var targetType = XlFixedFormatType.xlTypePDF;var missing = Type.Missing;//转pdfvar doc = wps.Application.Workbooks.Open(source, missing, missing, missing, missing, missing,missing, missing, missing, missing, missing, missing, missing, missing, missing);doc.ExportAsFixedFormat(targetType, newFilePath, XlFixedFormatQuality.xlQualityStandard, true, false,missing, missing, missing, missing);doc.Close();}catch (Exception e){//添加你的日志代码return false;}finally{wps.Quit();GC.Collect();GC.WaitForPendingFinalizers();}return true;}}
}
正常以上做完了在VS模式上基于浏览器做展示就没问题了。但是部署到IIS程序就没有权限访问Com组件。
报下图错误,或者“无权访问Com主键”之类的
这是因为IIS使用的用户和VS调试时候的用户权限不同导致的。我以前按网上很多方法试了,有到Com组件设置权限的,都很麻烦,大部分不好使。
以前基于Office做转换试出一个好使的配置:
2.1:在服务器上安装office的Excel软件.
2:在"开始"->"运行"中输入dcomcnfg.exe启动"组件服务"或者dcomcnfg.exe -32
或者comexp.msc -32
3:依次双击"组件服务"->“计算机”->“我的电脑”->“DCOM配置”
4:在"DCOM配置"中找到"Microsoft Excel 应用程序",在它上面点击右键,然后点击"属性",弹出"Microsoft Excel 应用程序属性"对话框
5:点击"标识"标签,选择"交互式用户"
6:点击"安全"标签,在"启动和激活权限"上点击"自定义",然后点击对应的"编辑"按钮,在弹出的"安全性"对话框中填加Everyone和IIS_IUSRS用户,分配全部权限
7:点击"常规"标签,在"身份验证级别"上选择"无"
Excel同样配置
做WSP对接时候可以按下面配置解决(Office的没试过,应该也好使):
1.右键程序池-》高级设置
2.找到进程权限-》标识
3.选择自定义账户里使用超级管理员用户
希望对做到这块的有帮助,这方面资料比较少,特别涉及到IIS部署不好使的部分。
Asp.Net基于WPS预览Word和Excel相关推荐
- mvc直接在html页面预览pdf,Asp.net MVC 实现在线预览word、excel、ppt、pdf文件
在线预览word.excel.ppt 原理:主要是引用第三方Dll使本地word.excel.ppt文件转换成Html 需要引用 : Aspose.Cells.dll Aspose.Slides.dl ...
- 微信小程序预览 word、excel、ppt、pdf 等文件
目录 微信小程序预览 word.excel.ppt.pdf 等文件 预览效果 前言 注意点 实现代码 微信小程序预览 word.excel.ppt.pdf 等文件 预览效果 前言 微信官方提供了相关的 ...
- 预览word,excel,ppt,pdf、图片————使用vue实现
预览word,excel,ppt,pdf.图片----使用vue实现 需要下载的依赖: template模板实现: 方法的实现: 需要下载的依赖: npm i xlsx // 预览excel插件 np ...
- 使用永中文档实现java在线预览Word,Excel,Pptx,Pdf
使用永中文档实现java在线预览Word,Excel,Pptx,Pdf 永中文档提供了在线预览的功能 永中开发者文档 如果需要直接运行,请直接修改代码中的两个参数 转换类型在下方,根据传入以及输出类型 ...
- 【学习记录20】vue使用blob流预览word ,Excel,pdf,TXT,图片,视频
TXT,PDF直接使用浏览器本身预览 excel使用插件 xlsx,这个插件需要用到arraybuffer的流格式,我是使用前端转换的详见js代码,也可以叫后台返回arraybuffer的数据流 wo ...
- 在线预览word、excel、pdf、txt、png等功能实现(附简单源码)
项目过程中,有时候我们需要对一些附件进行预览的操作,这里讲述一下实现过程,首先如标题所写的预览格式,预览word和excel是利用POI生成临时文件即把word或者excel生成html文件然后再通过 ...
- 前端页面预览word_详解html实现在线预览word、excel、pdf等文件的功能(附代码)_WEB前端开发...
JavaScript判断"字典"为空的方法_WEB前端开发 字典是一种存储键值对的数据结构,Javascript中的Object类内部即实现为一个字典,本文就来为大家介绍一下判断字 ...
- 前端网页预览word,pdf,excel等各类文档
项目中有需求要在网页上预览word文档,但是使用a标签的话只能预览图片一类的文件,对word只会下载文档不能预览 在网上查了很多资料,有些方法只能支持ie(使用ActiveXObject) 终于发现有 ...
- vue+iview 内网预览(本文重点)+外网预览word、excel、pdf、ppt
访问内网文件思路如下: 1.后端将word.excel.pdf文件转为二进制文件流 前端将文件流转为html从而实现文件预览 2.pdf没这么复杂具体可看下文 3.ppt的实现方式是后端将ppt转为p ...
最新文章
- 如何查看 el-form-item 的prop属性_PHP 7.4中的类型属性(Typed Properties)
- ConfigParser
- 省赛组队赛3 比赛总结
- mysql handlers,2 Handlers
- 2021年科技赋能中医药产业发展报告
- mysql避免死锁的方法
- axios发送post数据后台收不到_axios 发 post 请求,后端接收不到参数的解决方案
- vue 对象继承_JS面向对象—对象的继承
- Binaural Microphone
- SQL Server 数据库文件路径迁移步骤
- 全新微头像V2.1.8版全套iApp源码分享
- 用flash做古诗动画_Flash制作跟我学 用遮罩技术制作古诗动画-FLASH课件制作(FLASH课件制作教程)-flash课件吧(湖北金鹰)...
- 知了课堂Day3——微信小程序基础03——组件的一些笔记
- Linux下查看网卡光衰值
- 计算机主机号截图,电脑如何截图?截图三种方法推荐
- db2dual_DB2中类似于ORACLE中的DUAL表的表
- BFS最短路径的两种打印方法
- 【03】Linux笔记
- 小程序体积优化(1)--优化大文本
- jquery遍历后台数据