C#用Openxml把一个word文件中的表格复制到另外一个word文件,表格中的文本格式和源文件的格式对比发生的变化,怎样才能保持源文件中的格式
时间: 2024-03-10 13:44:22 浏览: 338
要保持源文件中的格式,可以尝试使用以下方法:
1. 使用Openxml SDK打开源文件和目标文件。
2. 使用源文件中表格的样式,创建一个新的表格样式对象。
3. 遍历源文件中的表格,将表格复制到目标文件中。
4. 遍历源文件中的每个单元格,将单元格中的内容复制到目标文件中的单元格中。
5. 对于每个单元格,将它的样式与新表格样式对象中的样式进行比较,然后将目标文件单元格的样式设置为匹配的样式。
6. 保存目标文件。
以下是示例代码:
```csharp
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
// 打开源文件和目标文件
using (WordprocessingDocument sourceDoc = WordprocessingDocument.Open("source.docx", false))
using (WordprocessingDocument destDoc = WordprocessingDocument.Open("destination.docx", true))
{
// 获取源文件中的表格样式
TableStyle sourceTableStyle = sourceDoc.MainDocumentPart.StyleDefinitionsPart.Styles.Descendants<TableStyle>()
.FirstOrDefault(s => s.StyleId == "TableGrid");
// 在目标文件中创建相同的表格样式
TableStyle destTableStyle = new TableStyle()
{
StyleId = "TableGridCopy",
BasedOn = sourceTableStyle.BasedOn,
StyleName = new StyleName() { Val = "Table Grid Copy" }
};
// 复制源文件中的格式到新表格样式对象中
foreach (var styleChild in sourceTableStyle.ChildElements)
{
destTableStyle.Append(styleChild.CloneNode(true));
}
// 将新表格样式添加到目标文件的样式定义部分
destDoc.MainDocumentPart.StyleDefinitionsPart.Styles.AppendChild(destTableStyle);
// 遍历源文件中的所有表格
foreach (var sourceTable in sourceDoc.MainDocumentPart.Document.Descendants<Table>())
{
// 在目标文件中创建一个新表格
Table destTable = new Table();
// 复制源文件中表格的属性到新表格中
foreach (var tableChild in sourceTable.ChildElements)
{
destTable.Append(tableChild.CloneNode(true));
}
// 遍历源文件中的所有单元格
foreach (var sourceCell in sourceTable.Descendants<TableCell>())
{
// 获取单元格中的内容
string cellText = sourceCell.InnerText;
// 在目标文件中创建一个新单元格
TableCell destCell = new TableCell();
// 复制源文件中单元格的属性到新单元格中
foreach (var cellChild in sourceCell.ChildElements)
{
destCell.Append(cellChild.CloneNode(true));
}
// 比较单元格样式,设置目标文件中单元格的样式
TableCellStyle sourceCellStyle = sourceDoc.MainDocumentPart.StyleDefinitionsPart.Styles.Descendants<TableCellStyle>()
.FirstOrDefault(s => s.StyleId == sourceCell.TableCellProperties.TableStyleId?.Value && s.StyleIndex == sourceCell.TableCellProperties.StyleIndex?.Value);
TableCellStyle destCellStyle = destDoc.MainDocumentPart.StyleDefinitionsPart.Styles.Descendants<TableCellStyle>()
.FirstOrDefault(s => s.StyleId == destTableStyle.StyleId && s.StyleIndex == sourceCellStyle.StyleIndex?.Value);
if (destCellStyle != null)
{
destCell.TableCellProperties = new TableCellProperties()
{
TableCellWidth = sourceCell.TableCellProperties.TableCellWidth.CloneNode(true) as TableCellWidth,
TableCellMargin = sourceCell.TableCellProperties.TableCellMargin.CloneNode(true) as TableCellMargin,
TableCellBorders = sourceCell.TableCellProperties.TableCellBorders.CloneNode(true) as TableCellBorders,
TableCellVerticalAlignment = sourceCell.TableCellProperties.TableCellVerticalAlignment.CloneNode(true) as TableCellVerticalAlignment,
TableCellJustification = sourceCell.TableCellProperties.TableCellJustification.CloneNode(true) as TableCellJustification,
TableCellShading = sourceCell.TableCellProperties.TableCellShading.CloneNode(true) as TableCellShading,
TableCellHideMark = sourceCell.TableCellProperties.TableCellHideMark.CloneNode(true) as TableCellHideMark,
TableCellLook = sourceCell.TableCellProperties.TableCellLook.CloneNode(true) as TableCellLook,
TableCellFlyAnchor = sourceCell.TableCellProperties.TableCellFlyAnchor.CloneNode(true) as TableCellFlyAnchor,
TableCellOverlap = sourceCell.TableCellProperties.TableCellOverlap.CloneNode(true) as TableCellOverlap,
TableCellIns = sourceCell.TableCellProperties.TableCellIns.CloneNode(true) as TableCellIns,
TableCellDel = sourceCell.TableCellProperties.TableCellDel.CloneNode(true) as TableCellDel,
TableCellContentPartStyle = sourceCell.TableCellProperties.TableCellContentPartStyle.CloneNode(true) as TableCellContentPartStyle,
TableCellContentPart = sourceCell.TableCellProperties.TableCellContentPart.CloneNode(true) as TableCellContentPart,
TableCellVisualStyle = sourceCell.TableCellProperties.TableCellVisualStyle.CloneNode(true) as TableCellVisualStyle,
TableCellId = sourceCell.TableCellProperties.TableCellId.CloneNode(true) as TableCellId,
TableCellWidthType = sourceCell.TableCellProperties.TableCellWidthType.CloneNode(true) as TableCellWidthType,
TableCellFitText = sourceCell.TableCellProperties.TableCellFitText.CloneNode(true) as TableCellFitText,
TableCellPreferredWidth = sourceCell.TableCellProperties.TableCellPreferredWidth.CloneNode(true) as TableCellPreferredWidth,
TableCellDirection = sourceCell.TableCellProperties.TableCellDirection.CloneNode(true) as TableCellDirection,
TableCellGridSpan = sourceCell.TableCellProperties.TableCellGridSpan.CloneNode(true) as TableCellGridSpan,
TableCellVerticalMerge = sourceCell.TableCellProperties.TableCellVerticalMerge.CloneNode(true) as TableCellVerticalMerge,
TableCellNoWrap = sourceCell.TableCellProperties.TableCellNoWrap.CloneNode(true) as TableCellNoWrap,
TableCellRtl = sourceCell.TableCellProperties.TableCellRtl.CloneNode(true) as TableCellRtl,
TableCellTcBorders = sourceCell.TableCellProperties.TableCellTcBorders.CloneNode(true) as TableCellTcBorders,
TableCellTcMar = sourceCell.TableCellProperties.TableCellTcMar.CloneNode(true) as TableCellTcMar,
TableCellTcPr = sourceCell.TableCellProperties.TableCellTcPr.CloneNode(true) as TableCellTcPr,
TableCellTcStyle = sourceCell.TableCellProperties.TableCellTcStyle.CloneNode(true) as TableCellTcStyle,
TableCellTcW = sourceCell.TableCellProperties.TableCellTcW.CloneNode(true) as TableCellTcW,
TableCellTcFitText = sourceCell.TableCellProperties.TableCellTcFitText.CloneNode(true) as TableCellTcFitText,
TableCellTcNoWrap = sourceCell.TableCellProperties.TableCellTcNoWrap.CloneNode(true) as TableCellTcNoWrap,
TableCellTcBordersNoShading = sourceCell.TableCellProperties.TableCellTcBordersNoShading.CloneNode(true) as TableCellTcBordersNoShading,
TableCellTcShd = sourceCell.TableCellProperties.TableCellTcShd.CloneNode(true) as TableCellTcShd,
TableCellTcBordersEastAsian = sourceCell.TableCellProperties.TableCellTcBordersEastAsian.CloneNode(true) as TableCellTcBordersEastAsian,
TableCellTcBordersComplex = sourceCell.TableCellProperties.TableCellTcBordersComplex.CloneNode(true) as TableCellTcBordersComplex,
TableCellTcBordersBidi = sourceCell.TableCellProperties.TableCellTcBordersBidi.CloneNode(true) as TableCellTcBordersBidi,
TableCellTcFitTextId = sourceCell.TableCellProperties.TableCellTcFitTextId.CloneNode(true) as TableCellTcFitTextId,
TableCellTcVert = sourceCell.TableCellProperties.TableCellTcVert.CloneNode(true) as TableCellTcVert,
TableCellTcTextDirection = sourceCell.TableCellProperties.TableCellTcTextDirection.CloneNode(true) as TableCellTcTextDirection,
TableCellTcHideMark = sourceCell.TableCellProperties.TableCellTcHideMark.CloneNode(true) as TableCellTcHideMark,
TableCellTcSmartTagPr = sourceCell.TableCellProperties.TableCellTcSmartTagPr.CloneNode(true) as TableCellTcSmartTagPr,
TableCellTcSmartTagType = sourceCell.TableCellProperties.TableCellTcSmartTagType.CloneNode(true) as TableCellTcSmartTagType,
TableCellTcSmartTag = sourceCell.TableCellProperties.TableCellTcSmartTag.CloneNode(true) as TableCellTcSmartTag,
TableCellTcAudio = sourceCell.TableCellProperties.TableCellTcAudio.CloneNode(true) as TableCellTcAudio,
TableCellTcTrackChange = sourceCell.TableCellProperties.TableCellTcTrackChange.CloneNode(true) as TableCellTcTrackChange,
TableCellTcDataBinding = sourceCell.TableCellProperties.TableCellTcDataBinding.CloneNode(true) as TableCellTcDataBinding,
TableCellTcResId = sourceCell.TableCellProperties.TableCellTcResId.CloneNode(true) as TableCellTcResId,
TableCellTcXML = sourceCell.TableCellProperties.TableCellTcXML.CloneNode(true) as TableCellTcXML,
TableCellTcRPr = sourceCellStyle.TableCellProperties.TableCellRPr.CloneNode(true) as TableCellProperties
};
}
// 将单元格的文本添加到目标文件中的单元格中
destCell.Append(new Paragraph(new Run(new Text(cellText))));
// 将新单元格添加到目标文件中的表格中
destTable.Append(destCell);
}
// 将新表格添加到目标文件中的文档中
destDoc.MainDocumentPart.Document.Body.Append(destTable);
}
// 保存目标文件
destDoc.MainDocumentPart.Document.Save();
}
```
请注意,这只是一个示例代码,并且可能需要进行一些调整以适应您的具体情况。
阅读全文