OpenXML SDK border on merged cells only applied to first cell
Asked Answered
G

1

7

When using the OpenXML SDK to apply a border to a merged cell range, the border is only showing up for the first (top left) cell in the merge range. I have absolutely no idea and this is driving me crazy. Using the productivity tool I can't find any meaningful difference in what I have and something that works as expected. This is my generated XML for the stylesheet:

    <x:borders count="2" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
      <x:border>
        <x:left />
        <x:right />
        <x:top />
        <x:bottom />
        <x:diagonal />
      </x:border>
      <x:border>
        <x:left />
        <x:right />
        <x:top />
        <x:bottom style="thick">
          <x:color auto="1" />
        </x:bottom>
        <x:diagonal />
      </x:border>
    </x:borders>
    <x:cellXfs xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
      <x:xf fontId="0" />
      <x:xf fontId="1" fillId="2" applyFont="1" applyFill="1" />
      <x:xf borderId="1" applyBorder="1" />
    </x:cellXfs>

I've tried everything, applying the merge before the border, after, etc.

Georgie answered 5/11, 2015 at 23:37 Comment(2)
Can you post the XML that works as expected? Maybe the difference between the two doesn't seem meaningful but others will be able to see the significance.Voigt
@pnuts. Yeah I didn't post it because it's (the stylesheet) literally identical. The problem was actually with the cells themselves as I've figured out.Georgie
G
10

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.

Georgie answered 6/11, 2015 at 16:0 Comment(4)
can confirm in 2018, it's stated in the official Microsoft documentation for MergeCells classTranslunar
Do you happen to have an example of how you set the border on each cell? When I set the border on each, it changed it to no longer be merged, the crazy part is if you then highlight all those cells in Excel the Merge button lights up as if they are merged.Monogenetic
Actually, strike that, I must have had something wrong the first time I did this, it is working now. Thank you for the answer.Monogenetic
The bad thing: you have to apply the same style manually to each cell. The good thing: the borders are applied correctly to the merged range as a whole. So, if you have different borders for top, left, right or bottom set for the top left cell and use this style for all cells, the borders will appear as expected for the whole range.Malemute

© 2022 - 2024 — McMap. All rights reserved.