如何解决使用 Open XML 和 C#
我正在尝试将表格从一个文档复制到另一个文档。在源文档中,我有 20 多个表,我需要为每个表创建一个新文档。我已经尝试了以下。
正在创建文件,但由于数据存在问题,我无法打开该文件。请帮我将表格数据从一个文档复制到另一个文档
var doc = WordprocessingDocument.Open(@"C:\Users\vnadendla\Downloads\TestWordDoc.docx",false);
var parts = doc.MainDocumentPart.Document.Descendants().FirstOrDefault();
if (parts != null)
{
foreach (var node in parts.ChildElements)
{
if (node is Table)
{
var tableData = (Table)node.Clone();
await CreateWordprocessingDocument(@"C:\Users\vnadendla\Desktop\GeneratedDocuments\tablename.docx",node,log);
}
}
}
public static async Task CreateWordprocessingDocument(string filepath,Table tableData,ILogger log)
{
try
{
// Create a document by supplying the filepath.
using (WordprocessingDocument wordDocument =
WordprocessingDocument.Create(filepath,WordprocessingDocumentType.Document))
{
// Add a main document part.
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
// Create the document structure and add some text.
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
Paragraph para = body.AppendChild(new Paragraph());
Table table = para.AppendChild(new Table(tableData));
Run run = para.AppendChild(new Run());
wordDocument.Save();
}
}
catch(Exception ex)
{
log.LogError(ex.Message);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。