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?
c# openxml wordprocessingml
add a comment |
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?
c# openxml wordprocessingml
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
add a comment |
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?
c# openxml wordprocessingml
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
c# openxml wordprocessingml
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
add a comment |
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
add a comment |
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 :)
1
Actually, the problem was that I had to useNil
instead ofNull
. 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
add a comment |
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 :)
1
Actually, the problem was that I had to useNil
instead ofNull
. 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
add a comment |
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 :)
1
Actually, the problem was that I had to useNil
instead ofNull
. 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
add a comment |
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 :)
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 :)
answered Nov 7 at 13:49
FortyTwo
1,42621224
1,42621224
1
Actually, the problem was that I had to useNil
instead ofNull
. 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
add a comment |
1
Actually, the problem was that I had to useNil
instead ofNull
. 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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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