EPPlus - AutoFitColumns() method fails when a column has merged cells - c#

I was wondering if anyone has come up with a workaround to this problem. I've noticed that the AutoFitColumns() method is failing on columns with merged cells. I've included a basic code example below:
var cellRange = worksheet.Cells[1, column, 2, column];
cells.Merge = true;
cells.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
cells.Value = "Some Text Goes Here";
worksheet.Cells.AutoFitColumns();
The resulting worksheet will have the cells in rows 1 and 2 merged (correctly) for the column in the column variable, but that particular cell is disregarded by the AutoFitColumns() method.
Any help would be appreciated.

Basically...
AutoFitColumns is documented to ignore merged cells. It isn't failing, at least in the sense of being defective.
AutoFit within Excel ignores merged cells, too.
Apparently up through Excel 2007, you cannot use the AutoFit feature for rows or columns that contain merged cells at all.
I've only tested with Excel 2013, which seems to behave differently:
Auto-fitting a row ignores all row-merged cells in that row.
Auto-fitting a column ignores all column-merged cells in that column.
In other words, you can use AutoFit in rows and columns with merged cells, it'll just ignore any cells that have been merged along the same dimension you're auto-fitting.
Desired effect of AutoFit w/r/t merged cells? (+ workarounds)
Finally, I'm not sure whether I see how it makes sense to auto-fit with respect to a merged cell. For example, suppose you have a merged cell at A1:B1 with content that fills a default two-column width.
If you auto-fit on column A, what is supposed to happen? Is column A supposed to become wide enough to fit all of A1:B1, sort of like treating the merged cell as if it's content existed only in A1, the top-left original cell? That might be reasonable, insofar as I can't immediately see whether strange behaviors might be implied in some circumstances.
If something like that is desired, I'd insert the content, auto-fit, and only then merge.
But what if you want to auto-fit on column B, in a situation like this:
Here, you might want column B to get wide enough so that all of the content in A1:B1 shows. (The content is just the text "well hello world".)
There's no one-liner for this, I'm afraid. Here's one way to do it, though:
Insert the content in the as-yet-unmerged top-left cell.
Save the current width of this cell's column.
Auto-fit the column.
Save the new column width.
Reset the column width to what you saved in step 2.
Merge the cells you want to merge.
Total up the widths of all your merged columns but the last one.
Subtract this total from the width you saved in step 4.
Set the last merged column's width to the result of step 8.
More generally, you can split up the total auto-fitted width from step 4 among the merged columns in any way you see fit.
To do all this, you'll want to use ExcelColumn.AutoFit and ExcelColumn.Width (and ExcelWorksheet.Column to grab the ExcelColumn objects).
But most simply...
If your content is static (or at least dynamic but not too variable in length), you can simply set reasonable fixed width(s) for the column(s) in question, distributed however you'd like.

Related

How to show a cell value in two cells in a row when the length of the cell value is large in datagridview in c#

In C# Windows Application DataGridView, the value of a cell is a text with more than 40 characters. The characters are not showing when the column size is less. If the column size is increased, then other column values got disturbed. So I want a solution that, the cell value with more characters overlap or display in the next cell(ie.next column). (Like in Excel)
I already tried Wrap mode and autosizecolumnsmode.
I expect an output shown below
(source: fordevs.com)
You can use a custom implementation using
Canvas.SetZIndex(item,index);

How to set data to resize depending on columns and vise versa inside datagridview using C#?

I have one problem. When ever I try to print my datagridview it always splits in half, so what I wanna do is to find a way to set length of column name adapt to data inside the column or to set that data adapts to column name. Its not the best explanation but take a look at the pictures:
This is what I have now:
This is what I want:
So basically I want the name of the column and data inside the rows to be visible but to take as minimum space as they can, depending on what length is bigger, If column name is bigger, adapt to that, if data length is bigger, adapt to that. Any solution for my problem?
Set the following property:
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
See here the documentation for DataGridViewAutoSizeColumnsMode member AllCells:
The column widths adjust to fit the contents of all cells in the
columns, including header cells.

AutoFitColumns() with EPPlus slightly short

I generate an Excel sheet with EPPlus and at the end, call AutoFitColumns() on my range:
sheet.DefaultColWidth = 10;
sheet.Cells[1,1,myRow,myCol].AutoFitColumns();
It works fine, except all the columns that are adjusted end up slightly short of the actual column width. Microsoft Excel adjusts the width properly when I double click on the column header, but with EPPlus, the last character will often by half-hidden.
The size difference between EPPlus and Excel's auto-adjust seems to vary based on the width of the contents, but it is not linear, so I assume EPPlus does not calculate the content width accurately based on the font.
Is there a way to fix this problem, other than arbitrarily increasing column width after auto-adjusting?
I conclude that this is a bug with EPPlus. Setting column width via the default parameter is also inaccurate (the resulting width is lower). I have not tested manually setting column width.
As a workaround, I have calculated the difference between each column's width as set with EPPlus and Excel, and have determined that for my application, it differs by up to 6-7%. So, after calling AutoFitColumns(), I do the following:
for(int i = 1; i < myRow; i++
sheet.Column(i).Width *= 1.06;

AutoResize Not Working Properly When ColumnHeader Is Longest String

dgvSampled.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
I'm trying to size the columns with the AllCells mode. The problem is that when the longest string in the column is the header, a buffer is left over.
Compare the Requested column to the Coil # column. Why does the Coil # column size itself perfectly to its content, but the Requested column sizes itself too large for its header title?
After making the columns unable to be sorted, they are now being sized as I expected them to be in the first place.
From the documentation (emphasis mine):
AllCells: The column widths adjust to fit the contents of all cells in the columns, including header cells.
From your description, it seems that what you want is DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader

change merged cells height

I create C# programmatically excel file with merged cell. User OpenXml library. I need AutoFit merge cells as single cell. But I can't change merged cells hight. Is it possible to programmatically change?
You can't change the cell height as far as i know. But, a nice workaround would be to change the row heigth instead of the cell heigth.
Remember that in vb you can do something like this: Rows(3).RowHeight = 25;.For the C# way you should take a look at this: MSDN Reference. Hope it helps!

Categories