asp.net を使用して .docx を HTML に変換するにはどうすればよいですか?
質問
Word 2007 は、ドキュメントを .docx 形式で保存します。これは実際には、ドキュメントを含む xml ファイルを含む多くのものが含まれた zip ファイルです。
.docx ファイルを取得して、asp.net Web アプリ内のフォルダーにドロップし、コードで .docx ファイルを開いて、ドキュメント (の xml 部分) を Web ページとしてレンダリングできるようにしたいと考えています。
これについてさらに詳しい情報を求めてウェブを検索していますが、これまでのところあまり見つかりません。私の質問は次のとおりです。
- (a) XSLT を使用して XML を HTML に変換しますか、それとも (b) .net の XML 操作ライブラリ (3.5 の XDocument や XElement など) を使用して HTML に変換しますか、それとも (c) その他ですか?
- 出発点として使用できる、これを実現したオープン ソース ライブラリ/プロジェクトをご存知ですか?
ありがとう!
解決
これを試して 役職?わかりませんが、あなたが探しているものかもしれません。
他のヒント
私が書いた マンモス.js, は、docx ファイルを HTML に変換する JavaScript ライブラリです。.NET でサーバー側のレンダリングを実行したい場合は、.NET バージョンの Mammoth もあります。 NuGet で利用可能.
Mammoth は、セマンティック情報を調べてクリーンな HTML を生成しようとします。たとえば、Word の段落スタイルをマッピングします (例: Heading 1
) を HTML/CSS の適切なタグとスタイル (たとえば、 <h1>
)。正確な視覚的コピーを生成するものが必要な場合は、おそらく Mammoth は向いていません。すでに適切に構造化されているものをきちんとした HTML に変換したい場合は、Mammoth を使用するとうまくいくかもしれません。
Word 2007 には、HTML に変換するために使用できる API があります。それについて語った投稿はこちら http://msdn.microsoft.com/en-us/magazine/cc163526.aspx. 。API に関するドキュメントは見つかりますが、API に HTML への変換関数があることを覚えています。
このコードは変換に役立ちます .docx
ファイルからテキストへ
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\t@\/\_\(\)]/","",$striped_content);
echo nl2br($striped_content);
}
相互運用機能を使用しています。多少問題がありますが、ほとんどの場合は正常に機能します。
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Word;
これは、HTML に変換されたドキュメントのパスのリストを返します。
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に変換するものです。
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にレンダリングします。
ヘルプドキュメントのリストを含むドロップダウンがあります。値はその HTML バージョンへのパスで、テキストはドキュメントの名前です。
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();
}
選択したインデックスが変更されると、フレームに再描画されます
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";
}
}