Remove cell borders in table in Word document (OpenXml.Wordprocessing)











up vote
0
down vote

favorite












I'm using DocumentFormat.OpenXml.Wordprocessing to add a table in a Word document. What I need is to remove the border of the first 4(/6) cells in the last 3(/N) rows of the table. These rows are added like:



t.Append(new TableRow(
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text("Total:")))),
new TableCell(new Paragraph(new Run(new Text(priceTotal.ToString()))))
));


How do I set the TableCellBorders? I've tried a few things like:



TableCell cell = new TableCell();
cell.TableCellProperties.TableCellBorders.LeftBorder.Size.Value = 0;
cell.TableCellProperties.TableCellBorders.RightBorder.Size.Value = 0;
cell.TableCellProperties.TableCellBorders.TopBorder.Size.Value = 0;
cell.TableCellProperties.TableCellBorders.BottomBorder.Size.Value = 0;


However, everything that I've tried returns System.NullReferenceException. What is the proper way of removing the cell borders?










share|improve this question
























  • Probably because the borders are defined under your style sheet..
    – FortyTwo
    Nov 7 at 13:27












  • There are two ways you can define Table borders in a Word document. One way is to define the borders in a Table element, and another way is to define the borders in TableCell elements.
    – FortyTwo
    Nov 7 at 13:28










  • I want to define them for each TableCell, since they differ from one to another. How can I achieve that ?
    – I.Manev
    Nov 7 at 13:35















up vote
0
down vote

favorite












I'm using DocumentFormat.OpenXml.Wordprocessing to add a table in a Word document. What I need is to remove the border of the first 4(/6) cells in the last 3(/N) rows of the table. These rows are added like:



t.Append(new TableRow(
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text("Total:")))),
new TableCell(new Paragraph(new Run(new Text(priceTotal.ToString()))))
));


How do I set the TableCellBorders? I've tried a few things like:



TableCell cell = new TableCell();
cell.TableCellProperties.TableCellBorders.LeftBorder.Size.Value = 0;
cell.TableCellProperties.TableCellBorders.RightBorder.Size.Value = 0;
cell.TableCellProperties.TableCellBorders.TopBorder.Size.Value = 0;
cell.TableCellProperties.TableCellBorders.BottomBorder.Size.Value = 0;


However, everything that I've tried returns System.NullReferenceException. What is the proper way of removing the cell borders?










share|improve this question
























  • Probably because the borders are defined under your style sheet..
    – FortyTwo
    Nov 7 at 13:27












  • There are two ways you can define Table borders in a Word document. One way is to define the borders in a Table element, and another way is to define the borders in TableCell elements.
    – FortyTwo
    Nov 7 at 13:28










  • I want to define them for each TableCell, since they differ from one to another. How can I achieve that ?
    – I.Manev
    Nov 7 at 13:35













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm using DocumentFormat.OpenXml.Wordprocessing to add a table in a Word document. What I need is to remove the border of the first 4(/6) cells in the last 3(/N) rows of the table. These rows are added like:



t.Append(new TableRow(
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text("Total:")))),
new TableCell(new Paragraph(new Run(new Text(priceTotal.ToString()))))
));


How do I set the TableCellBorders? I've tried a few things like:



TableCell cell = new TableCell();
cell.TableCellProperties.TableCellBorders.LeftBorder.Size.Value = 0;
cell.TableCellProperties.TableCellBorders.RightBorder.Size.Value = 0;
cell.TableCellProperties.TableCellBorders.TopBorder.Size.Value = 0;
cell.TableCellProperties.TableCellBorders.BottomBorder.Size.Value = 0;


However, everything that I've tried returns System.NullReferenceException. What is the proper way of removing the cell borders?










share|improve this question















I'm using DocumentFormat.OpenXml.Wordprocessing to add a table in a Word document. What I need is to remove the border of the first 4(/6) cells in the last 3(/N) rows of the table. These rows are added like:



t.Append(new TableRow(
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text("Total:")))),
new TableCell(new Paragraph(new Run(new Text(priceTotal.ToString()))))
));


How do I set the TableCellBorders? I've tried a few things like:



TableCell cell = new TableCell();
cell.TableCellProperties.TableCellBorders.LeftBorder.Size.Value = 0;
cell.TableCellProperties.TableCellBorders.RightBorder.Size.Value = 0;
cell.TableCellProperties.TableCellBorders.TopBorder.Size.Value = 0;
cell.TableCellProperties.TableCellBorders.BottomBorder.Size.Value = 0;


However, everything that I've tried returns System.NullReferenceException. What is the proper way of removing the cell borders?







c# openxml wordprocessingml






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 12:31

























asked Nov 7 at 11:11









I.Manev

471315




471315












  • Probably because the borders are defined under your style sheet..
    – FortyTwo
    Nov 7 at 13:27












  • There are two ways you can define Table borders in a Word document. One way is to define the borders in a Table element, and another way is to define the borders in TableCell elements.
    – FortyTwo
    Nov 7 at 13:28










  • I want to define them for each TableCell, since they differ from one to another. How can I achieve that ?
    – I.Manev
    Nov 7 at 13:35


















  • Probably because the borders are defined under your style sheet..
    – FortyTwo
    Nov 7 at 13:27












  • There are two ways you can define Table borders in a Word document. One way is to define the borders in a Table element, and another way is to define the borders in TableCell elements.
    – FortyTwo
    Nov 7 at 13:28










  • I want to define them for each TableCell, since they differ from one to another. How can I achieve that ?
    – I.Manev
    Nov 7 at 13:35
















Probably because the borders are defined under your style sheet..
– FortyTwo
Nov 7 at 13:27






Probably because the borders are defined under your style sheet..
– FortyTwo
Nov 7 at 13:27














There are two ways you can define Table borders in a Word document. One way is to define the borders in a Table element, and another way is to define the borders in TableCell elements.
– FortyTwo
Nov 7 at 13:28




There are two ways you can define Table borders in a Word document. One way is to define the borders in a Table element, and another way is to define the borders in TableCell elements.
– FortyTwo
Nov 7 at 13:28












I want to define them for each TableCell, since they differ from one to another. How can I achieve that ?
– I.Manev
Nov 7 at 13:35




I want to define them for each TableCell, since they differ from one to another. How can I achieve that ?
– I.Manev
Nov 7 at 13:35












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You can create a table in word with no borders like this:



public static void CreateTable(string fileName)
{
// Use the file name and path passed in as an argument
// to open an existing Word 2007 document.

using (WordprocessingDocument doc
= WordprocessingDocument.Open(fileName, true))
{
// Create an empty table.
Table table = new Table();

// Create a TableProperties object and specify its border information.
TableProperties tblProp = new TableProperties(
new TableBorders(
new TopBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new BottomBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new LeftBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new RightBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new InsideHorizontalBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new InsideVerticalBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
}
)
);

// Append the TableProperties object to the empty table.
table.AppendChild<TableProperties>(tblProp);

// Create a row.
TableRow tr = new TableRow();

// Create a cell.
TableCell tc1 = new TableCell();

// Specify the width property of the table cell.
tc1.Append(new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));

// Specify the table cell content.
tc1.Append(new Paragraph(new Run(new Text("some text"))));

// Append the table cell to the table row.
tr.Append(tc1);

// Create a second table cell by copying the OuterXml value of the first table cell.
TableCell tc2 = new TableCell(tc1.OuterXml);

// Append the table cell to the table row.
tr.Append(tc2);

// Append the table row to the table.
table.Append(tr);

// Append the table to the document.
doc.MainDocumentPart.Document.Body.Append(table);
}
}


Customize and optimize it, to your needs :)






share|improve this answer

















  • 1




    Actually, the problem was that I had to use Nil instead of Null. Idk why, but that is what fixed my issue. However, I'll mark your answer, since it really helped me and pushed me in the right direction! Thanks! new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Nil), }
    – I.Manev
    Nov 7 at 14:47













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53188332%2fremove-cell-borders-in-table-in-word-document-openxml-wordprocessing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










You can create a table in word with no borders like this:



public static void CreateTable(string fileName)
{
// Use the file name and path passed in as an argument
// to open an existing Word 2007 document.

using (WordprocessingDocument doc
= WordprocessingDocument.Open(fileName, true))
{
// Create an empty table.
Table table = new Table();

// Create a TableProperties object and specify its border information.
TableProperties tblProp = new TableProperties(
new TableBorders(
new TopBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new BottomBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new LeftBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new RightBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new InsideHorizontalBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new InsideVerticalBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
}
)
);

// Append the TableProperties object to the empty table.
table.AppendChild<TableProperties>(tblProp);

// Create a row.
TableRow tr = new TableRow();

// Create a cell.
TableCell tc1 = new TableCell();

// Specify the width property of the table cell.
tc1.Append(new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));

// Specify the table cell content.
tc1.Append(new Paragraph(new Run(new Text("some text"))));

// Append the table cell to the table row.
tr.Append(tc1);

// Create a second table cell by copying the OuterXml value of the first table cell.
TableCell tc2 = new TableCell(tc1.OuterXml);

// Append the table cell to the table row.
tr.Append(tc2);

// Append the table row to the table.
table.Append(tr);

// Append the table to the document.
doc.MainDocumentPart.Document.Body.Append(table);
}
}


Customize and optimize it, to your needs :)






share|improve this answer

















  • 1




    Actually, the problem was that I had to use Nil instead of Null. Idk why, but that is what fixed my issue. However, I'll mark your answer, since it really helped me and pushed me in the right direction! Thanks! new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Nil), }
    – I.Manev
    Nov 7 at 14:47

















up vote
1
down vote



accepted










You can create a table in word with no borders like this:



public static void CreateTable(string fileName)
{
// Use the file name and path passed in as an argument
// to open an existing Word 2007 document.

using (WordprocessingDocument doc
= WordprocessingDocument.Open(fileName, true))
{
// Create an empty table.
Table table = new Table();

// Create a TableProperties object and specify its border information.
TableProperties tblProp = new TableProperties(
new TableBorders(
new TopBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new BottomBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new LeftBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new RightBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new InsideHorizontalBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new InsideVerticalBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
}
)
);

// Append the TableProperties object to the empty table.
table.AppendChild<TableProperties>(tblProp);

// Create a row.
TableRow tr = new TableRow();

// Create a cell.
TableCell tc1 = new TableCell();

// Specify the width property of the table cell.
tc1.Append(new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));

// Specify the table cell content.
tc1.Append(new Paragraph(new Run(new Text("some text"))));

// Append the table cell to the table row.
tr.Append(tc1);

// Create a second table cell by copying the OuterXml value of the first table cell.
TableCell tc2 = new TableCell(tc1.OuterXml);

// Append the table cell to the table row.
tr.Append(tc2);

// Append the table row to the table.
table.Append(tr);

// Append the table to the document.
doc.MainDocumentPart.Document.Body.Append(table);
}
}


Customize and optimize it, to your needs :)






share|improve this answer

















  • 1




    Actually, the problem was that I had to use Nil instead of Null. Idk why, but that is what fixed my issue. However, I'll mark your answer, since it really helped me and pushed me in the right direction! Thanks! new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Nil), }
    – I.Manev
    Nov 7 at 14:47















up vote
1
down vote



accepted







up vote
1
down vote



accepted






You can create a table in word with no borders like this:



public static void CreateTable(string fileName)
{
// Use the file name and path passed in as an argument
// to open an existing Word 2007 document.

using (WordprocessingDocument doc
= WordprocessingDocument.Open(fileName, true))
{
// Create an empty table.
Table table = new Table();

// Create a TableProperties object and specify its border information.
TableProperties tblProp = new TableProperties(
new TableBorders(
new TopBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new BottomBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new LeftBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new RightBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new InsideHorizontalBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new InsideVerticalBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
}
)
);

// Append the TableProperties object to the empty table.
table.AppendChild<TableProperties>(tblProp);

// Create a row.
TableRow tr = new TableRow();

// Create a cell.
TableCell tc1 = new TableCell();

// Specify the width property of the table cell.
tc1.Append(new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));

// Specify the table cell content.
tc1.Append(new Paragraph(new Run(new Text("some text"))));

// Append the table cell to the table row.
tr.Append(tc1);

// Create a second table cell by copying the OuterXml value of the first table cell.
TableCell tc2 = new TableCell(tc1.OuterXml);

// Append the table cell to the table row.
tr.Append(tc2);

// Append the table row to the table.
table.Append(tr);

// Append the table to the document.
doc.MainDocumentPart.Document.Body.Append(table);
}
}


Customize and optimize it, to your needs :)






share|improve this answer












You can create a table in word with no borders like this:



public static void CreateTable(string fileName)
{
// Use the file name and path passed in as an argument
// to open an existing Word 2007 document.

using (WordprocessingDocument doc
= WordprocessingDocument.Open(fileName, true))
{
// Create an empty table.
Table table = new Table();

// Create a TableProperties object and specify its border information.
TableProperties tblProp = new TableProperties(
new TableBorders(
new TopBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new BottomBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new LeftBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new RightBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new InsideHorizontalBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new InsideVerticalBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
}
)
);

// Append the TableProperties object to the empty table.
table.AppendChild<TableProperties>(tblProp);

// Create a row.
TableRow tr = new TableRow();

// Create a cell.
TableCell tc1 = new TableCell();

// Specify the width property of the table cell.
tc1.Append(new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));

// Specify the table cell content.
tc1.Append(new Paragraph(new Run(new Text("some text"))));

// Append the table cell to the table row.
tr.Append(tc1);

// Create a second table cell by copying the OuterXml value of the first table cell.
TableCell tc2 = new TableCell(tc1.OuterXml);

// Append the table cell to the table row.
tr.Append(tc2);

// Append the table row to the table.
table.Append(tr);

// Append the table to the document.
doc.MainDocumentPart.Document.Body.Append(table);
}
}


Customize and optimize it, to your needs :)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 7 at 13:49









FortyTwo

1,42621224




1,42621224








  • 1




    Actually, the problem was that I had to use Nil instead of Null. Idk why, but that is what fixed my issue. However, I'll mark your answer, since it really helped me and pushed me in the right direction! Thanks! new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Nil), }
    – I.Manev
    Nov 7 at 14:47
















  • 1




    Actually, the problem was that I had to use Nil instead of Null. Idk why, but that is what fixed my issue. However, I'll mark your answer, since it really helped me and pushed me in the right direction! Thanks! new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Nil), }
    – I.Manev
    Nov 7 at 14:47










1




1




Actually, the problem was that I had to use Nil instead of Null. Idk why, but that is what fixed my issue. However, I'll mark your answer, since it really helped me and pushed me in the right direction! Thanks! new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Nil), }
– I.Manev
Nov 7 at 14:47






Actually, the problem was that I had to use Nil instead of Null. Idk why, but that is what fixed my issue. However, I'll mark your answer, since it really helped me and pushed me in the right direction! Thanks! new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Nil), }
– I.Manev
Nov 7 at 14:47




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53188332%2fremove-cell-borders-in-table-in-word-document-openxml-wordprocessing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini