在C#中将MS字表转换为html

在C#中将MS字表转换为html

本文介绍了在C#中将MS字表转换为html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#编写Word Addin,将所有格式替换为xml标签,
现在我希望将word转换为带有标准标签的html,
表可能在行数和列数上有所不同,
我的意思是表包含合并的单元格或列

I'm writing a Word Addin with C# that replace all formats to xml tags,now I want convert tables in word to html with standard tags,tables may different in rows count and columns count,I mean table contains merged cells or columns

相同:

-------------------------
|  1  |  2  |  3  |  4  |
|     -------------------
|     |  5  |  6  |  7  |
|     -------------------
|     |        8        |
|     -------------------
|     |  9  | 10  | 11  |
|------------------------
| 12  | 13  | 14  | 15  |
-------------------------

单元格1合并一列中的四行
而单元格8合并一行中的三列

that cell 1 is merge of four rows in one columnand cell 8 is merge of three columns in one row

如何我能转换它吗?

推荐答案

回复tanx,
i找到了将字表转换为html的方法。
我写这段代码:

tanx for reply,i found a way to convert word tables to html.
i write this code:

private static void ConvertTableToHTML()
    {
        try
        {
            foreach (Table tb in Common.WordApplication.ActiveDocument.Tables)
            {

                for (int r = 1; r <= tb.Rows.Count; r++)
                {
                    for (int c = 1; c <= tb.Columns.Count; c++)
                    {
                        try
                        {
                            Cell cell = tb.Cell(r, c);
                            foreach (Paragraph paragraph in cell.Range.Paragraphs)
                            {
                                Tagging(paragraph.Range, "P");
                            }
                            Tagging(cell.Range, "TD");
                        }
                        catch (Exception e)
                        {
                            if (e.Message.Contains("The requested member of the collection does not exist."))
                            {
                                //Most likely a part of a merged cell, so skip over.
                            }
                            else throw;
                        }
                    }
                    try
                    {
                        Row row = tb.Rows[r];
                        Tagging(row.Range, "TR");
                    }
                    catch (Exception ex)
                    {
                        bool initialTrTagInserted = false;
                        int columnsIndex = 1;
                        int columnsCount = tb.Columns.Count;
                        while (!initialTrTagInserted && columnsIndex <= columnsCount)
                        {
                            try
                            {
                                Cell cell = tb.Cell(r, columnsIndex);
                                cell.Range.InsertBefore("<TR>");
                                initialTrTagInserted = true;
                            }
                            catch (Exception e)
                            {
                            }
                            columnsIndex++;
                        }

                        columnsIndex = tb.Columns.Count;
                        bool endTrTagInserted = false;
                        while (!endTrTagInserted && columnsIndex >= 1)
                        {
                            try
                            {
                                Cell cell = tb.Cell(r, columnsIndex);
                                cell.Range.InsertAfter("</TR>");
                                endTrTagInserted = true;
                            }
                            catch (Exception e)
                            {
                            }
                            columnsIndex--;
                        }
                    }
                }
                Common.Tagging2(tb.Range, "Table");

                object separator = "";
                object nestedTable = true;
                tb.ConvertToText(separator, nestedTable);
            }
        }
        catch (Exception ex) { MessageBox.Show(ex.Message); }
    }


public static void Tagging(Range range, string TagName)
    {
        try
        {
            range.InsertBefore("<" + TagName + ">");
            range.InsertAfter("</" + TagName + ">");
        }
        catch (Exception ex) { throw new Exception(ex.Message); }
    }

这篇关于在C#中将MS字表转换为html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:48