C# Form.Transparencykey Working Different for Different Colors, Why

OpenXML SDK having borders for cell

I recommend installing the Open XML 2.5 productivity tool. Then create a blank Excel document that contains the border and color you desire. Open that file in the productivity tool and then click reflect code. It will then give you the C# code that is required to produce that border and background color. The code is a bit lengthy for posting, but if you follow those steps you should be able to use it.

**Edit**

The border and fill properties are stored in a separate part called the WookbookStylesPart. This part is where you will insert the type of border, fill, font, etc that you want applied to a cell within a workbook. These properties are stored in an array type structure where you access the style you inserted via an index. Since you can have multiple styles applied to a cell, a CellFormat object is where all the indices for the various styles are stored. Once you have a CellFormat for a cell, its index needs to be referenced on the actual cell via the StlyeIndex property. That is how the cell knows how to apply the various styles on itself.

Here is the code to create the border:

public Border GenerateBorder()
{
Border border2 = new Border();

LeftBorder leftBorder2 = new LeftBorder(){ Style = BorderStyleValues.Thin };
Color color1 = new Color(){ Indexed = (UInt32Value)64U };

leftBorder2.Append(color1);

RightBorder rightBorder2 = new RightBorder(){ Style = BorderStyleValues.Thin };
Color color2 = new Color(){ Indexed = (UInt32Value)64U };

rightBorder2.Append(color2);

TopBorder topBorder2 = new TopBorder(){ Style = BorderStyleValues.Thin };
Color color3 = new Color(){ Indexed = (UInt32Value)64U };

topBorder2.Append(color3);

BottomBorder bottomBorder2 = new BottomBorder(){ Style = BorderStyleValues.Thin };
Color color4 = new Color(){ Indexed = (UInt32Value)64U };

bottomBorder2.Append(color4);
DiagonalBorder diagonalBorder2 = new DiagonalBorder();

border2.Append(leftBorder2);
border2.Append(rightBorder2);
border2.Append(topBorder2);
border2.Append(bottomBorder2);
border2.Append(diagonalBorder2);

return borders2;
}

Here is the code to add a fill:

public Fill GenerateFill()
{
Fill fill = new Fill();

PatternFill patternFill = new PatternFill(){ PatternType = PatternValues.Solid };
ForegroundColor foregroundColor1 = new ForegroundColor(){ Rgb = "FFFFFF00" };
BackgroundColor backgroundColor1 = new BackgroundColor(){ Indexed = (UInt32Value)64U };

patternFill.Append(foregroundColor1);
patternFill.Append(backgroundColor1);

fill.Append(patternFill);

return fill;
}

You will need this code to insert the border and fill into the style part:

public uint InsertBorder(WorkbookPart workbookPart, Border border)
{
Borders borders = workbookPart.WorkbookStylesPart.Stylesheet.Elements<Borders>().First();
borders.Append(border);
return (uint)borders.Count++;
}

public uint InsertFill(WorkbookPart workbookPart, Fill fill)
{
Fills fills = workbookPart.WorkbookStylesPart.Stylesheet.Elements<Fills>().First();
fills.Append(fill);
return (uint)fills.Count++;
}

You will need to first get a reference to the cell you want to add the fill and border to where the cellAddress is in the form of "B2":

public Cell GetCell(WorksheetPart workSheetPart, string cellAddress)
{
return workSheetPart.Worksheet.Descendants<Cell>()
.SingleOrDefault(c => cellAddress.Equals(c.CellReference));
}

Then once you get your cell you need to get the CellFormat that belong to that cell and also to add a new CellFormat:

public CellFormat GetCellFormat(WorkbookPart workbookPart, uint styleIndex)
{
return workbookPart.WorkbookStylesPart.Stylesheet.Elements<CellFormats>().First().Elements<CellFormat>().ElementAt((int)styleIndex);
}

public uint InsertCellFormat(WorkbookPart workbookPart, CellFormat cellFormat)
{
CellFormats cellFormats = workbookPart.WorkbookStylesPart.Stylesheet.Elements<CellFormats>().First();
cellFormats.Append(cellFormat);
return (uint)cellFormats.Count++;
}

Once you have the CellFormat you can now alter the fill and border properties. Once those are altered you need to insert the new CellFormat and then point the index of that CellFormat to the StyleIndex on the cell. This is how the cell will know what styles to apply to itself.

 public void SetBorderAndFill(WorkbookPart workbookPart, WorksheetPart workSheetPart)
{
Cell cell = GetCell(workSheetPart, "B2");

CellFormat cellFormat = cell.StyleIndex != null ? GetCellFormat(workbookPart, cell.StyleIndex).CloneNode(true) as CellFormat : new CellFormat();
cellFormat.FillId = InsertFill(workbookPart, GenerateFill());
cellFormat.BorderId = InsertBorder(workbookPart, GenerateBorder());

cell.StyleIndex = InsertCellFormat(workbookPart, cellFormat);
}

How to add border to cell with openxml in PowerPoint?

In order to set the top border of a cell in the middle of a PowerPoint table, you have to complete 2 steps:

Step 1: set the bottom border of the cell directly above the cell in question and

Step 2: set the top border of the cell in question (you have that part)

I determined this by using the OpenXML Productivity Tool. I took a simple 1 slide PowerPoint file named Before.pptx with a table cell that had the left, bottom and right borders.

enter image description here

Then I added the top border (using PowerPoint 2016) and saved the file as After.pptx. I then used the Productivity Tool to diff the 2 files and reverse engineer the C# code required to make Before.pptx look like After.pptx. The important code you need is displayed here:

        //STEP 1 CODE STARTS HERE
A.Table table1=graphicData1.GetFirstChild<A.Table>();

A.TableRow tableRow1=table1.GetFirstChild<A.TableRow>();
A.TableRow tableRow2=table1.Elements<A.TableRow>().ElementAt(1);

A.TableCell tableCell1=tableRow1.Elements<A.TableCell>().ElementAt(2);

A.TableCellProperties tableCellProperties1=tableCell1.GetFirstChild<A.TableCellProperties>();

A.BottomBorderLineProperties bottomBorderLineProperties1 = new A.BottomBorderLineProperties(){ Width = 12700, CapType = A.LineCapValues.Flat, CompoundLineType = A.CompoundLineValues.Single, Alignment = A.PenAlignmentValues.Center };

A.SolidFill solidFill1 = new A.SolidFill();
A.SchemeColor schemeColor1 = new A.SchemeColor(){ Val = A.SchemeColorValues.Text1 };

solidFill1.Append(schemeColor1);
A.PresetDash presetDash1 = new A.PresetDash(){ Val = A.PresetLineDashValues.Solid };
A.Round round1 = new A.Round();
A.HeadEnd headEnd1 = new A.HeadEnd(){ Type = A.LineEndValues.None, Width = A.LineEndWidthValues.Medium, Length = A.LineEndLengthValues.Medium };
A.TailEnd tailEnd1 = new A.TailEnd(){ Type = A.LineEndValues.None, Width = A.LineEndWidthValues.Medium, Length = A.LineEndLengthValues.Medium };

bottomBorderLineProperties1.Append(solidFill1);
bottomBorderLineProperties1.Append(presetDash1);
bottomBorderLineProperties1.Append(round1);
bottomBorderLineProperties1.Append(headEnd1);
bottomBorderLineProperties1.Append(tailEnd1);
tableCellProperties1.Append(bottomBorderLineProperties1);
//STEP 1 CODE ENDS HERE

//STEP 2 CODE STARTS HERE
A.TableCell tableCell2=tableRow2.Elements<A.TableCell>().ElementAt(2);

A.TableCellProperties tableCellProperties2=tableCell2.GetFirstChild<A.TableCellProperties>();

A.BottomBorderLineProperties bottomBorderLineProperties2=tableCellProperties2.GetFirstChild<A.BottomBorderLineProperties>();

A.TopBorderLineProperties topBorderLineProperties1 = new A.TopBorderLineProperties(){ Width = 12700, CapType = A.LineCapValues.Flat, CompoundLineType = A.CompoundLineValues.Single, Alignment = A.PenAlignmentValues.Center };

A.SolidFill solidFill2 = new A.SolidFill();
A.SchemeColor schemeColor2 = new A.SchemeColor(){ Val = A.SchemeColorValues.Text1 };

solidFill2.Append(schemeColor2);
A.PresetDash presetDash2 = new A.PresetDash(){ Val = A.PresetLineDashValues.Solid };
A.Round round2 = new A.Round();
A.HeadEnd headEnd2 = new A.HeadEnd(){ Type = A.LineEndValues.None, Width = A.LineEndWidthValues.Medium, Length = A.LineEndLengthValues.Medium };
A.TailEnd tailEnd2 = new A.TailEnd(){ Type = A.LineEndValues.None, Width = A.LineEndWidthValues.Medium, Length = A.LineEndLengthValues.Medium };

topBorderLineProperties1.Append(solidFill2);
topBorderLineProperties1.Append(presetDash2);
topBorderLineProperties1.Append(round2);
topBorderLineProperties1.Append(headEnd2);
topBorderLineProperties1.Append(tailEnd2);
tableCellProperties2.InsertBefore(topBorderLineProperties1,bottomBorderLineProperties2);

I ran the code above against my Before.pptx file and the border was complete.

enter image description here

In an effort to double check that the two steps are necessary, I commented out the Step 1 code and ran it against a fresh version of Before.pptx file and the top border was missing. This verifies the problem you were seeing. Therefore, the two steps are necessary to paint the one border.

Open XML - Word Processing Document - Table Cell Border

Flydog57's comment to using the OpenXML Productivity Tool is the correct approach. You will spend way too much time attempting to figure things out on your own. Make a word document and then view it using the OpenXML Productivity Tool. Rinse and repeat.

OpenXML SDK border on merged cells only applied to first cell

I've figured this out. I was under the impression that if you apply a style to the top left cell in a group of merged cells, then said style will apply to the whole merge. I swear I read some documentation that said that, but this is apparently not the case. What worked is that I applied the border style to each cell in the merge range, not just the first.

It seems weird to me that the way I initially had it is even allowed then, considering the generated output is impossible to reproduce other than through the OpenXML SDK.

How to apply outline table border to a cell range using OpenXml?

Taterhead's solution is a nice one. I have another one here. Instead of drawing white borders, just set the background color of the cell range to white.

Thanks everyone's help!

c# openxml excel stylesheet border error

The border elements are defined as a sequence and therefore have an explicit order. LeftBorder has to come before RightBorder so you just need to reverse the order in your code:

new DocumentFormat.OpenXml.Spreadsheet.LeftBorder(
new DocumentFormat.OpenXml.Spreadsheet.Color() { Auto = true }
)
{ Style = BorderStyleValues.Thick },
new DocumentFormat.OpenXml.Spreadsheet.RightBorder(
new DocumentFormat.OpenXml.Spreadsheet.Color() { Auto = true }
)
{ Style = BorderStyleValues.Thick },

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

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 :)



Related Topics



Leave a reply



Submit