Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C WordprocessingDocument按原样将表解析为另一个.DOCX文件_C#_Copy Paste - Fatal编程技术网

C# C WordprocessingDocument按原样将表解析为另一个.DOCX文件

C# C WordprocessingDocument按原样将表解析为另一个.DOCX文件,c#,copy-paste,C#,Copy Paste,我最近刚开始解析OpenXml格式,很好奇是否有更简单的方法来解决我的问题 这幅图说明了问题所在 图片: 左边最远的文档是sample_tables.docx,它包含三个不同样式的随机表。我要做的是对文件中的每个表实例,按原样解析内容和样式,并为每个表实例创建一个全新的file.docx文件 我开发了这个示例C代码来自动解析每个表实例的内容,从而创建一个包含每个表实例的单独的.docx文件。每个解析表实例都显示在图像右侧的三个图像中 我的问题是。因为我基本上是在阅读openxml,所以在samp

我最近刚开始解析OpenXml格式,很好奇是否有更简单的方法来解决我的问题

这幅图说明了问题所在

图片:

左边最远的文档是sample_tables.docx,它包含三个不同样式的随机表。我要做的是对文件中的每个表实例,按原样解析内容和样式,并为每个表实例创建一个全新的file.docx文件

我开发了这个示例C代码来自动解析每个表实例的内容,从而创建一个包含每个表实例的单独的.docx文件。每个解析表实例都显示在图像右侧的三个图像中

我的问题是。因为我基本上是在阅读openxml,所以在sample_tables.docx中复制与给定表实例关联的所有相关xml,以创建1:1的颜色、边框、底纹等等,是否有一条捷径。。。是否将其复制到另一个文件中

这是我的C代码:

using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;


namespace sample_table_parser
{
    class Program
    {

        //
        // given a Word table instance, it will capture the contents and create a 
        // new Word file ( .docx ) with that table
        //
        static private void parse_word_table( Table tbl, string file_name )
        {
            using ( WordprocessingDocument doc = WordprocessingDocument.Create( file_name , WordprocessingDocumentType.Document ))
            {
                MainDocumentPart mdp = doc.AddMainDocumentPart();
                mdp.Document = new Document();
                Body body = mdp.Document.AppendChild(new Body());

                Table table = new Table();
                TableRow tableRow;
                TableCell tableCell;

                foreach ( TableRow row in tbl.Descendants<TableRow>() )
                {
                    tableRow = new TableRow();

                    foreach ( TableCell cell in row.Descendants<TableCell>() )
                    {
                        tableCell = new TableCell(new Paragraph(new Run(new Text(cell.InnerText))));
                        tableRow.Append(tableCell);
                    }

                    table.AppendChild(tableRow);
                }

                body.AppendChild(table);
                mdp.Document.Save();
            }
        }



        //
        // Main
        // 
        static void Main(string[] args)
        {
            int counter = 0;
            string file_that_has_tables = "sample_tables.docx";

            WordprocessingDocument doc = WordprocessingDocument.Open(file_that_has_tables, false);
            Body body = doc.MainDocumentPart.Document.Body;

            foreach (var b in body)
            {
                if (b.ToString() == "DocumentFormat.OpenXml.Wordprocessing.Table")
                {
                    counter++;
                    Table fooTable = (Table)b;
                    parse_word_table(fooTable, "table_" + counter + ".docx");
                }
            }

            doc.Close();
        }
    }
}

你问得太多了,不清楚你到底想要什么。你给出了很多代码,但没有说如果有什么问题怎么办。如果你还没有说确切的问题是什么,谁能回答呢?我的代码的问题是仅仅捕获表的内容。对于每个表实例,我想看看是否有方法自动记录表是否有边框,如果有边框,样式是什么,表是否使用颜色,使用的颜色是什么,等等。我知道我可以通过检查几个if条件来捕获表的每个样式,但我只是想知道是否有一条捷径可以在一两步内捕获表的所有样式。这更清楚吗?