c# - 将文本插入文字处理文档中的表格 (Open XML )
问题描述
我有带有页眉和页脚部分的文档文件。在页脚部分,我有一张桌子。
所以现在我正在尝试将文本插入表格单元格。但是,每当我尝试通过此代码执行此操作时,它都会附加段落类型并更改表格的高度和重量。
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(ORiFilepath, true))
{
var docPart = wordDoc.MainDocumentPart;
MainDocumentPart mainPart = wordDoc.MainDocumentPart;
if (docPart.FooterParts.Count() > 0)
{
//List<Table> table = docPart.FooterParts..Elements<Table>().ToList();
foreach (FooterPart footer in docPart.FooterParts)
{
List<Table> table = footer.Footer.Elements<Table>().ToList();
if (table.Count > 0)
{
var footertable = table[0];
TableRow row1 = footertable.Elements<TableRow>().ElementAt(1);
string text1 = cell1.InnerText;
if (text1 == "")
{
cell1.Append(new Paragraph(new Run(new Text("TEXT"))));
}
}
}
}
}
我可以在单元格中插入文本吗
cell1.Append(new Paragraph(new Run(new Text("TEXT"))));,
或者应该是什么方法?
解决方案
这是我用来向单元格添加文本的方法。希望它有用。
CreateCellProperties
和方法只是封装了 OpenXML SDK (和) 中的CreateParagraph
相应对象,您实际上不需要编写文本。TableCellProperties
ParagraphProperties
如果您仍有问题,请告诉我:
public TableCell CreateCell(Shading shading, int width, int gridSpan,
TableCellBorders borders, TableVerticalAlignmentValues valign,
JustificationValues justification, Color color, string text,
VericalMergeOptions mergeOptions)
{
// Set up Table Cell and format it.
TableCell cell = new TableCell();
TableCellProperties cellProperties = CreateCellProperties(shading, width, gridSpan, borders, valign, mergeOptions);
ParagraphProperties props = CreateParagraphProperties(justification, color);
// Set cell and paragraph
Run run = CreateRun(color, text);
Paragraph paragraph = new Paragraph(new List<OpenXmlElement>
{
props,
run
});
// Append the run and the properties
cell.Append(new List<OpenXmlElement>
{
cellProperties,
paragraph
});
return cell;
}
protected Run CreateRun(Color color, string text = "")
{
Run run = new Run(new List<OpenXmlElement>{
new RunProperties(color.CloneNode(true)),
});
if (text != "") run.Append(new Text(text));
return run;
}
推荐阅读
- jasper-reports - 我想在 Ireport 的最后一项中用逗号和不带逗号的波段详细信息中将数据与我的字段分开
- javascript - 使用 Node 上传文件而不更改到新页面
- angular - 使用打字稿处理提交中的调用函数
- unity3d - 带有 REST API 的 Unity 中的 Firebase 数据库
- node.js - 使用 NodeJS 和 node-fetch 代理多部分表单数据
- razor - if else 语句没有被视为@foreach 中的代码 - 2sxc v11 DNN 9.8
- c - 如何标记输入并将其添加到字符**
- python - 在 Python 中的 xlsxwriter add_series 中使数据范围动态化?
- javascript - 如何使用 FormatJS CLI 从多个特定文件夹中提取消息?
- docker - TypeError: Object(...) is not a function 当将卷从本地目录挂载到 next.js 容器中时