頁面中開啟Word,線上瀏覽

來源:互聯網
上載者:User

標籤:style   使用   檔案   os   for   cti   

 

 

/// <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);//路徑生產靜態頁面的路徑,注意

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.