asp.Netはオンラインでwordドキュメントを閲覧することを実現する(word転html)
最近wordドキュメントのオンラインブラウズをして、いろいろな方法、コントロールを探した后、wordに戻ってHTMLを回転して、オンラインブラウズ....
以下はバックグラウンドコードで、フロントhtmlページのデフォルトコードでいいです.
ファイルは以下のとおりです.
この場合、参照にコンポーネントを参照する必要がある.Netの下のMicrosoft.Office.Interop.Visio,Microsoft.Office.Interop.Word
バックグラウンドコードは次のとおりです.
以下はバックグラウンドコードで、フロントhtmlページのデフォルトコードでいいです.
ファイルは以下のとおりです.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Word = Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
コンポーネントを参照していない場合、ここではエラーが報告され、Interopなどが見つかりません.この場合、参照にコンポーネントを参照する必要がある.Netの下のMicrosoft.Office.Interop.Visio,Microsoft.Office.Interop.Word
バックグラウンドコードは次のとおりです.
protected void Page_Load(object sender, EventArgs e)
{
string relativePath = Request.QueryString["FilePath"]; // , 。
if (relativePath == "" || relativePath==null) return;
string serverPath = Server.MapPath(relativePath); //
string html = serverPath.Replace(".doc", ".html");
if (!File.Exists(@html)) //html , word html
{
string filename = WordToHtml(serverPath);
StreamReader fread = new StreamReader(filename, System.Text.Encoding.GetEncoding("gb2312"));
string ss = fread.ReadToEnd();
Response.Write(ss); // , , 、 。 html 。
fread.Close();
fread.Dispose();
}
html = relativePath.Replace(".doc", ".html");
//html ,
// html
Response.Redirect(html);
return;
}
///
/// word html
///
///
private string WordToHtml(object wordFileName)
{
//
Word.Application word = new Word.Application();
Type wordType = word.GetType();
Word.Documents docs = word.Documents;
//
Type docsType = docs.GetType();
Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });
// ,
Type docType = doc.GetType();
string wordSaveFileName = wordFileName.ToString();
string strSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.Length - 3) + "html";
object saveFileName = (object)strSaveFileName;
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
// Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
return saveFileName.ToString();
}