前段时间项目有一个在线预览office文档的需求,做了一个小栗子,和大家一起学习学习!如何实现这个功能呢?我总结了几个方案:
方案一:将需要转换的文档通过office组件转换成html查看
方案二:将需要转换的文档通过office组件转换成pdf查看
方案三:通过在服务器部署微软的office web apps进行文档预览 参考网页: https://www.cnblogs.com/yanweidie/p/4516164.html
方案四:office 在线预览服务
方案五:使用网上很多收费或者不收费的第三方插件
我使用的是方案一,也试过方案二和方案三,相比而言,转换成html比转换成pdf的效率要快很多,只是页面没有pdf美观,而在服务器部署office web apps配置失败了,所以选择放弃。
以下是方案一的案例:
1.首先引入com组件中office库,然后在程序集扩展中引入word的dll,我本来是想使用.net core做这个在线预览offce文档,结果发现.net core 并没有office库,可能是我没有找到
值得注意的是,虽然项目里引入office库,但运行项目后,发现会遇到各种问题,原因是office库并没有你电脑的office应用匹配,所以需要在电脑需要部署office插件
1.运行栏中输入命令:dcomcnfg,打开组件服务管理窗口,组件服务->我的电脑->DCOM配置
2.找到Microsoft Excel Application 设置属性标识:交互式用户,在安全了的三个配置全选自定义配置,添加Everyone 组,给这个组的权限都勾选未允许,
3.再找到 Microsoft PowerPoint 幻灯片 权限设置和第二步一样
4.再找到Microsoft Word 97 - 2003 文档 权限设置和第二步一样 (可能office版本不同)
5.在项目的webconfig 里 <system.web> 下 添加 <identity impersonate="true" userName="Administrator" password="123456"/> userName、password为服务器的对应账号密码
查了很多资料,亲测可用 参考网页:https://www.cnblogs.com/5426z/articles/4865312.html
2.预览Excel
/// 预览Excel
/// </summary>
/// <param name="physicalPath">解码的地址</param>
/// <param name="url"></param>
public string PreviewExcel(string physicalPath, string url)
{
var resultUrl = string.Empty;
var serverUrl = string.Empty;
try
{
Microsoft.Office.Interop.Excel.Application application = null;
//参数
Workbook workbook = null;
application = new Microsoft.Office.Interop.Excel.Application();
object missing = Type.Missing;
object trueObject = true;
application.Visible = false;
application.DisplayAlerts = false;
workbook = application.Workbooks.Open(physicalPath, missing, trueObject, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
object format = XlFileFormat.xlHtml;
string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";
String outputFile = Path.GetDirectoryName(physicalPath) + "\\" + htmlName;
workbook.SaveAs(outputFile, XlFileFormat.xlHtml, null, null, false, false,
XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
workbook.Close();
application.Quit();
resultUrl = Path.GetDirectoryName(url) + "\\" + htmlName;
var lastUrl = Path.GetFileName(resultUrl);
resultUrl = ConfigurationManager.AppSettings["ResultUrl"] + lastUrl;
serverUrl = ConfigurationManager.AppSettings["ServerUrl"];
}
catch (Exception ex)
{
this.Write(ex);
}
return serverUrl + resultUrl;
}
4.预览word
> /// 预览Word(HtmL类型)
> /// </summary>
> /// <param name="physicalPath"></param>
> /// <param name="url"></param>
> /// <returns></returns>
> public string PreviewWord(string physicalPath, string url)
> {
>
> var resultUrl = string.Empty;
> var serverUrl = string.Empty;
> object outputFile1 = new object();
> object missing1 = new object();
> //检查doc 是否被实例化
> var idd = 0;
> var ss = 1;
> try
> {
> Microsoft.Office.Interop.Word._Application application = null;
> Microsoft.Office.Interop.Word._Document doc = null;
> application = new Microsoft.Office.Interop.Word.Application();
> //参数
> object missing = Missing.Value;
> object trueObject = true;
> application.Visible = false;
> application.DisplayAlerts = WdAlertLevel.wdAlertsNone;
> object physicalPathObj = physicalPath;
> //实例化
> doc = application.Documents.Open(ref physicalPathObj, ref missing, ref trueObject, ref missing, ref missing, ref missing,
> ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
> missing);
> string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";
> object outputFile = Path.GetDirectoryName(physicalPath) + "\\" + htmlName;
> object format = WdSaveFormat.wdFormatHTML;
> outputFile1 = outputFile;
> missing1 = missing;
> idd = doc.DocID;
> ss = doc.CompatibilityMode;
> //转换并保存
> doc.SaveAs(ref outputFile, ref format, ref missing, ref missing, ref missing,
> ref missing, XlSaveAsAccessMode.xlNoChange, ref missing,
> ref missing, ref missing, ref missing, ref missing);
> doc.Close();
> application.Quit();
> //返回137的路径
>
> resultUrl = Path.GetDirectoryName(url) + "\\" + htmlName;
> var lastUrl = Path.GetFileName(resultUrl);
> resultUrl = ConfigurationManager.AppSettings["ResultUrl"] + lastUrl;
> serverUrl = ConfigurationManager.AppSettings["ServerUrl"];
> }
> catch (Exception ex)
> {
> this.Write(ex, $"{outputFile1}+{missing1}+{idd.ToString()}+{ss.ToString()}+{physicalPath}");
>
> }
> return serverUrl + resultUrl; }
其余的信息请转到 https://blog.csdn.net/qq_42638454/article/details/103577309
网友评论