当前位置:首页> 正文

关于 xml:如何使用 asp.net 将 .docx 转换为 html?

关于 xml:如何使用 asp.net 将 .docx 转换为 html?

How do I convert a .docx to html using asp.net?

Word 2007 将其文档保存为 .docx 格式,这实际上是一个 zip 文件,其中包含一堆内容,包括带有文档的 xml 文件。

我希望能够获取一个 .docx 文件并将其放入我的 asp.net Web 应用程序的文件夹中,并让代码打开 .docx 文件并将文档的(xml 部分)呈现为网页.

我一直在网上搜索有关这方面的更多信息,但到目前为止还没有找到太多信息。我的问题是:

  • 您会 (a) 使用 XSLT 将 XML 转换为 HTML,还是 (b) 使用 .net 中的 xml 操作库(例如 3.5 中的 XDocument 和 XElement)转换为 HTML 或 (c) 其他?
  • 您是否知道我可以将其用作起点的任何开源库/项目?
  • 谢谢!


    试试这个帖子?我不知道,但可能是您正在寻找的。


    我编写了 mammoth.js,这是一个将 docx 文件转换为 HTML 的 JavaScript 库。如果您想在 .NET 中进行服务器端渲染,NuGet 上还有一个 .NET 版本的 Mammoth。

    Mammoth 试图通过查看语义信息来生成干净的 HTML——例如,将 Word 中的段落样式(例如 Heading 1)映射到 HTML/CSS 中的适当标签和样式(例如 h1)。如果您想要产生精确视觉副本的东西,那么猛犸象可能不适合您。如果您有一些已经结构良好的内容并希望将其转换为整洁的 HTML,那么 Mammoth 可能会做到这一点。


    Word 2007 有一个 API,可用于转换为 HTML。这是一篇讨论它的帖子 http://msdn.microsoft.com/en-us/magazine/cc163526.aspx。您可以找到有关 API 的文档,但我记得 API 中有一个转换为 HTML 的功能。


    此代码将有助于将 .docx 文件转换为文本

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    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:tcw:tc',"", $content);
        $content = str_replace('/w:r/w:p',"\
    \
    ", $content);
         //header("Content-Type: plain/text");


        $striped_content = strip_tags($content);


          $striped_content = preg_replace("/[^a-zA-Z0-9\\s\\,\\.\\-\
    \
    \\t@\\/\\_\\(\\)]/","",$striped_content);

        echo nl2br($striped_content);
    }


    我正在使用互操作。这有点问题,但在大多数情况下都可以正常工作。

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

    这个返回html转换文档的列表\\'路径

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    public Liststring GetHelpDocuments()
        {

            Liststring lstHtmlDocuments = new Liststring();
            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;
        }

    还有这个将doc转换为html。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    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 版本的路径,文本是文档的名称。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    private void BindHelpContents()
        {
            Liststring lstHelpDocuments = new Liststring();
            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();

        }

    在选定的索引发生变化时,它被重新渲染为框架

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
        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";
            }
        }


    展开全文阅读

    相关内容