页面中打开Word,在线浏览

本文介绍了一种将Word文档转换为HTML以实现在线预览的方法。通过使用Microsoft Word应用程序进行后台操作,此方法能够自动将指定的Word文档转换成HTML格式,并生成对应的静态页面,便于网页展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

/// <summary>

/// 为了通用,放到一个类文件中,别的也没直接调用
/// </summary>
/// <param name="fileName">得到上传的文件名字以及后缀名字</param>
/// <param name="inFilePath">要打开文件的路径</param>
/// <param name="ShowPath">生产静态页面的路径,以及一些别的文件</param>
/// <param name="outDirPath">可以为空</param>
/// <returns></returns>

public static Tuple<string, string> Priview(string fileName, string inFilePath, string outDirPath = "")
{
object missingType = Type.Missing;
object readOnly = true;
object isVisible = false;
object documentFormat = 8;
//string randomName = DateTime.Now.ToString("yyyyMMddHHmmss");
string randomName = fileName.Remove(fileName.LastIndexOf("."));
object htmlFilePath = outDirPath + randomName + ".html";
string directoryPath = outDirPath + randomName + "_files";

object filePath = inFilePath;
//Open the word document in background

ApplicationClass applicationclass = new 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;

//Save the word document as HTML file
document.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);

//Close the word document
document.Close(ref missingType, ref missingType, ref missingType);
KillPreviewWordProcess();
return new Tuple<string, string>(randomName, htmlFilePath.ToString());
}
/// <summary>
/// 关闭所有用于预览的Word后台进程
/// </summary>
public static void KillPreviewWordProcess()
{
//Process.GetProcessesByName("WINWORD").ToList().Where(pro => string.IsNullOrWhiteSpace(pro.MainWindowTitle)).ToList().ForEach(nullPro => nullPro.Kill());
Process[] wordProcess = Process.GetProcessesByName("WINWORD");
try
{
foreach (Process pro in wordProcess)
{
string mwt = pro.MainWindowTitle;
if (string.IsNullOrWhiteSpace(mwt))
{
pro.Kill();
}
}
}
catch
{
}
}

 

 

页面中使用

Tuple<string, string> tupleW = WordPreview.Priview(this,get_Name, filePath, outputDirPath);
DName = tupleW.Item1.ToString();
JspUtility.OpenWindow("./Html/" + fileNameWithNoExt.ToString() + ".html", "", "1366", "768", UpdatePanel1);//路径生产静态页面的路径,注意

转载于:https://www.cnblogs.com/yan-xue-biao/p/3841038.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值