SetBorder(OpenXmlDocumentTableCell) Метод (OpenXmlDocumentTableRow)
Устанавливает границу этой строка из указанной ячейки таблицы.
Вот C#/VB.NET код, который демонстрирует, как установить границы таблицы из ячейки образца таблицы (в примере используется шаблон документа)SetTableBorders_template.docx):
Public Shared Sub DocxSetTableBordersExample()
Dim templateFilename As String = "SetTableBorders_template.docx"
Dim outFilename As String = "SetTableBorders.docx"
Dim outPdfFilename As String = "SetTableBorders.pdf"
' create DocxDocumentEditor that allows to edit file "SetTableBorders_template.docx"
Using editor As New Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentEditor(templateFilename)
' get cell border templates
Dim bordersTemplateTable As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTable = editor.Tables(0)
Dim boldBorderTemplate As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableCell = bordersTemplateTable.FindCell("[bold]")
Dim colorBorderTemplate As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableCell = bordersTemplateTable.FindCell("[color]")
' remove border template table
bordersTemplateTable.Remove()
' get test table
Dim table As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTable = editor.Tables(0)
' set borders from template cells
table.FindRow("[bold_row]").SetBorder(boldBorderTemplate)
table.FindCell("[bold_cell]").SetBorder(boldBorderTemplate)
table.FindRow("[color_row]").SetBorder(colorBorderTemplate)
table.FindCell("[color_cell]").SetBorder(colorBorderTemplate)
' set outside border inside table
table.SetOutsideBorder(table.FindCell("[bold_first]"), table.FindCell("[bold_last]"), boldBorderTemplate)
' save changed document to a DOCX file
editor.Save(outFilename)
' export changed document to a PDF document
editor.Export(outPdfFilename)
End Using
End Sub
public static void DocxSetTableBordersExample()
{
string templateFilename = "SetTableBorders_template.docx";
string outFilename = "SetTableBorders.docx";
string outPdfFilename = "SetTableBorders.pdf";
// create DocxDocumentEditor that allows to edit file "SetTableBorders_template.docx"
using (Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentEditor editor =
new Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentEditor(templateFilename))
{
// get cell border templates
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTable bordersTemplateTable =
editor.Tables[0];
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableCell boldBorderTemplate =
bordersTemplateTable.FindCell("[bold]");
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableCell colorBorderTemplate =
bordersTemplateTable.FindCell("[color]");
// remove border template table
bordersTemplateTable.Remove();
// get test table
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTable table = editor.Tables[0];
// set borders from template cells
table.FindRow("[bold_row]").SetBorder(boldBorderTemplate);
table.FindCell("[bold_cell]").SetBorder(boldBorderTemplate);
table.FindRow("[color_row]").SetBorder(colorBorderTemplate);
table.FindCell("[color_cell]").SetBorder(colorBorderTemplate);
// set outside border inside table
table.SetOutsideBorder(table.FindCell("[bold_first]"), table.FindCell("[bold_last]"), boldBorderTemplate);
// save changed document to a DOCX file
editor.Save(outFilename);
// export changed document to a PDF document
editor.Export(outPdfFilename);
}
}
Целевые платформы: .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5