使用OpenXML将表格中的Word文档内容复制到带有段落格式的Excel [英] Copy Word Document content in a table to Excel with Paragraph formatting using OpenXML
问题描述
我有一个包含表格的word文档(.docx)。用户可以以段落样式输入信息。在每个单元格中,他们可以添加一个或多个段落信息。我想阅读每个单元格中的内容并创建一个excel SpreadSheet。 excel中的每个
单元格文本内容都需要复制到新的Excel文件中。
我使用OpenXML,我可以使用InnerText读取内容。但段落格式(两段之间的空格)缺失。当我复制到Excel单元格时,所有段落文本都合并为单个。如何使用段落样式复制到
Excel单元格。
提前致谢
< hr>
R.Mani | http://rmanimaran.wordpress.com
嗨R.Mani,
你可以尝试下面的代码来更改文字处理文档中表格中的文字:
//更改文字处理文档中表格中的文字。
public static void ChangeTextInCell(string filepath,string txt)
{
//使用传入的文件名和路径作为参数
//打开现有文档。
using(WordprocessingDocument doc =
WordprocessingDocument.Open(filepath,true))
{
//找到文档中的第一个表。
表table =
doc.MainDocumentPart.Document.Body.Elements< Table>()。First();
//查找表格中的第二行。
TableRow row = table.Elements< TableRow>()。ElementAt(1);
//找到行中的第三个单元格。
TableCell cell = row.Elements< TableCell>()。ElementAt(2);
//查找表格单元格中的第一段。
段落p = cell.Elements<段落>()。First();
//查找段落中的第一次运行。
运行r = p.Elements< Run>()。First();
//设置运行的文本。
Text t = r.Elements< Text>()。First();
t.Text = txt;
}
}如需了解更多信息,请查看以下链接:
如何:更改文字处理文档(Open XML SDK)中表格中的文字
最好的问候,
Hi,
I have a word document(.docx) which contains a table. The user can enter the information in a paragraph style. In each cell they can add 1 or more paragraph information. I would like to read the contents in each cell and create a excel SpreadSheet. Each cell text content in excel needs to be copied to the new Excel file.
I used OpenXML and I can read the contents using InnerText. But the Paragraph formatting(space between two paragraph) are missing. When I copied to the Excel cell, all the paragraph text are merged in to single. How to copy with the Paragraph style to the Excel cell.
Thanks in advance
R.Mani | http://rmanimaran.wordpress.com
Hi R.Mani,
You can try the code below to change the text in a table in a word processing document:
// Change the text in a table in a word processing document. public static void ChangeTextInCell(string filepath, string txt) { // Use the file name and path passed in as an argument to // open an existing document. using (WordprocessingDocument doc = WordprocessingDocument.Open(filepath, true)) { // Find the first table in the document. Table table = doc.MainDocumentPart.Document.Body.Elements<Table>().First(); // Find the second row in the table. TableRow row = table.Elements<TableRow>().ElementAt(1); // Find the third cell in the row. TableCell cell = row.Elements<TableCell>().ElementAt(2); // Find the first paragraph in the table cell. Paragraph p = cell.Elements<Paragraph>().First(); // Find the first run in the paragraph. Run r = p.Elements<Run>().First(); // Set the text for the run. Text t = r.Elements<Text>().First(); t.Text = txt; } }For more information, please review the following link:
How to: Change text in a table in a word processing document (Open XML SDK)
How to Preserve string with formatting in OpenXML Paragraph, Run, Text?
Best Regards,
这篇关于使用OpenXML将表格中的Word文档内容复制到带有段落格式的Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!