2008-09-10 6 views
8

Word 2007は文書を.docx形式で保存します。これは実際にはzipファイルであり、その文書にはxmlファイルを含むzipファイルが含まれています。asp.netを使用して.docxをhtmlに変換するにはどうすればよいですか?

私は.docxファイルを取得してasp.net Webアプリケーションのフォルダにドロップして、コードに.docxファイルを開き、(xmlの部分)ドキュメントをWebとしてレンダリングできるようにしたいページ。

私はこれに関する詳細についてウェブを検索してきましたが、それほど多くは見つかりませんでした。私の質問は以下のとおりです。

  1. ます(a)はHTMLにXMLを変換するためにXSLTを使用するか、または(b)のHTMLに変換するために、(3.5などXDocumentとXElementのような).NETでXML操作ライブラリを使用するか(うc)他の?
  2. 私が出発点として使うことができたこれを行ったオープンソースライブラリ/プロジェクトは知っていますか?

ありがとう!

答えて

4

お試しくださいpost?私は知らないが、あなたが探しているものかもしれない。

2

Word 2007には、HTMLに変換するためのAPIがあります。ここにそれについて話す投稿はhttp://msdn.microsoft.com/en-us/magazine/cc163526.aspxです。 APIに関連するドキュメントを見つけることができますが、APIにはHTMLへの変換機能があることを覚えています。

+0

あなたが話しているAPIと「HTMLに変換する」機能についてもう少し具体的にお考えですか?おそらくあなたはPackageクラスについて話しているでしょうか?この「HTMLに変換」機能はどこにありますか? – Jez

1

このコード意志が、私は相互運用機能を使用してい

function read_file_docx($filename){ 

    $striped_content = ''; 
    $content = ''; 

    if(!$filename || !file_exists($filename)) { echo "sucess";}else{ echo "not sucess";} 

    $zip = zip_open($filename); 

    if (!$zip || is_numeric($zip)) return false; 

    while ($zip_entry = zip_read($zip)) { 

     if (zip_entry_open($zip, $zip_entry) == FALSE) continue; 

     if (zip_entry_name($zip_entry) != "word/document.xml") continue; 

     $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); 

     zip_entry_close($zip_entry); 
    }// end while 

    zip_close($zip); 

    //echo $content; 
    //echo "<hr>"; 
    //file_put_contents('1.xml', $content);  

    $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content); 
    $content = str_replace('</w:r></w:p>', "\r\n", $content); 
    //header("Content-Type: plain/text"); 


    $striped_content = strip_tags($content); 


     $striped_content = preg_replace("/[^a-zA-Z0-9\s\,\.\-\n\r\[email protected]\/\_\(\)]/","",$striped_content); 

    echo nl2br($striped_content); 
} 
0

をテキストに.docxファイルを変換するのに役立ちます。やや予言的ですが、ほとんどの場合、うまく動作します。

using System.Runtime.InteropServices; 
using Microsoft.Office.Interop.Word; 

この1つの文書パス

public List<string> GetHelpDocuments() 
    { 

     List<string> lstHtmlDocuments = new List<string>(); 
     foreach (string _sourceFilePath in Directory.GetFiles("")) 
     { 
      string[] validextentions = { ".doc", ".docx" }; 
      if (validextentions.Contains(System.IO.Path.GetExtension(_sourceFilePath))) 
      { 
       sourceFilePath = _sourceFilePath; 
       destinationFilePath = _sourceFilePath.Replace(System.IO.Path.GetExtension(_sourceFilePath), ".html"); 
       if (System.IO.File.Exists(sourceFilePath)) 
       { 
        //checking if the HTML format of the file already exists. if it does then is it the latest one? 
        if (System.IO.File.Exists(destinationFilePath)) 
        { 
         if (System.IO.File.GetCreationTime(destinationFilePath) != System.IO.File.GetCreationTime(sourceFilePath)) 
         { 
          System.IO.File.Delete(destinationFilePath); 
          ConvertToHTML(); 
         } 
        } 
        else 
        { 
         ConvertToHTML(); 
        } 

        lstHtmlDocuments.Add(destinationFilePath); 
       } 
      } 


     } 
     return lstHtmlDocuments; 
    } 

とhtmlにドキュメントを変換するには、この1変換されたHTMLのリストを返します。

private void ConvertToHtml() 
    { 
     IsError = false; 
     if (System.IO.File.Exists(sourceFilePath)) 
     { 
      Microsoft.Office.Interop.Word.Application docApp = null; 
      string strExtension = System.IO.Path.GetExtension(sourceFilePath); 
      try 
      { 
       docApp = new Microsoft.Office.Interop.Word.Application(); 
       docApp.Visible = true; 

       docApp.DisplayAlerts = WdAlertLevel.wdAlertsNone; 
       object fileFormat = WdSaveFormat.wdFormatHTML; 
       docApp.Application.Visible = true; 
       var doc = docApp.Documents.Open(sourceFilePath); 
       doc.SaveAs2(destinationFilePath, fileFormat); 
      } 
      catch 
      { 
       IsError = true; 
      } 
      finally 
      { 
       try 
       { 
        docApp.Quit(SaveChanges: false); 

       } 
       catch { } 
       finally 
       { 
        Process[] wProcess = Process.GetProcessesByName("WINWORD"); 
        foreach (Process p in wProcess) 
        { 
         p.Kill(); 
        } 
       } 
       Marshal.ReleaseComObject(docApp); 
       docApp = null; 
       GC.Collect(); 
      } 
     } 
    } 

単語の殺害は楽しみではありませんが、右、それはそこにぶら下がってみましょうと他人をブロックすることはできませんか?

web/htmlでは、htmlをiframeにレンダリングします。

ヘルプドキュメントのリストを含むドロップダウンがあります。 ValueはHTMLバージョンへのパスで、textはドキュメントの名前です。選択した索引の

private void BindHelpContents() 
    { 
     List<string> lstHelpDocuments = new List<string>(); 
     HelpDocuments hDoc = new HelpDocuments(Server.MapPath("~/HelpDocx/docx/")); 
     lstHelpDocuments = hDoc.GetHelpDocuments(); 
     int index = 1; 
     ddlHelpDocuments.Items.Insert(0, new ListItem { Value = "0", Text = "---Select Document---", Selected = true }); 

     foreach (string strHelpDocument in lstHelpDocuments) 
     { 
      ddlHelpDocuments.Items.Insert(index, new ListItem { Value = strHelpDocument, Text = strHelpDocument.Split('\\')[strHelpDocument.Split('\\').Length - 1].Replace(".html", "") }); 
      index++; 
     } 
     FetchDocuments(); 

    } 

は、私がmammoth.js、HTMLへのdocxファイルを変換するJavaScriptライブラリです書い

protected void RenderHelpContents(object sender, EventArgs e) 
    { 
     try 
     { 
      if (ddlHelpDocuments.SelectedValue == "0") return; 
      string strHtml = ddlHelpDocuments.SelectedValue; 
      string newaspxpage = strHtml.Replace(Server.MapPath("~/"), "~/"); 
      string pageVirtualPath = VirtualPathUtility.ToAbsolute(newaspxpage);// 
      documentholder.Attributes["src"] = pageVirtualPath; 
     } 
     catch 
     { 
      lblGError.Text = "Selected document doesn't exist, please refresh the page and try again. If that doesn't help, please contact Support"; 
     } 
    } 
+0

ASP.NETや他のサーバーテクノロジからOffice Interopを使用するのは、ひどい考えです。これらのAPIは、デスクトップアプリケーション(オフィスアプリケーションのスイート)を自動化するために作成されました。サーバーアプリケーションはさまざまな点で異なるため、Office Interopを使用することは非常に悪い考えです。 Microsoftによってもサポートされておらず、Officeライセンスに違反する可能性があります。 [Officeのサーバー側の自動化に関する考慮事項]を参照してください。(http://support.microsoft.com/kb/257757) –

+0

@JohnSaunders、私は恐ろしい考えですが、要件が範囲から脱線するのは簡単ではありません。私はこの質問に対して良い選択肢に非常に感謝しています。 –

3

フレームにrenedredされ、変更しました。 .NETでレンダリングサーバー側を実行する場合は、Mammoth available on NuGetの.NETバージョンもあります。

マンモスはWordの段落スタイル(Heading 1など)をHTML/CSSの適切なタグとスタイル(たとえば<h1>)にマッピングするなど、意味情報を調べることでクリーンなHTMLを作成しようとします。あなたが正確な視覚的なコピーを生成するものを望むなら、マンモスはおそらくあなたのためではありません。すでにうまく構成されていて、HTMLを整形したHTMLに変換したいものがあれば、マンモスはこのトリックを行うかもしれません。

+0

dotnet-mammothは有用なライブラリです。私はそれを使い、うまく動作します。私はあなたがそれを見てみることができるならば、配備に問題があります[ここ](http://stackoverflow.com/questions/31885962/converting-docx-to-html-with-dotnet-mammoth-fails-at-デプロイメントサーバ)。 – zed

関連する問題