I'm using EPPlus and C# and trying to autosize/autofit the height of a row to accommodate the height needed to show all of the contents of a merged cell with text wrapping. However no matter what I try the text always truncates. Since I'm repeating this process with various text sizes on various worksheets, I don't want to hard code the row height (except to enforce a minimum height for the row). If possible I'd like to do this within EPPlus/C#.
With the cells A2:E2 merged and WrapText = true:
Cell with Text Truncated
Here's what it should look like with desired Cell Height
Here's my relevant and short C# code
Int32 intToCol;
intToCol = 5;
eppWorksheet.Cells[2, 1, 2, intToCol].Merge = true;
eppWorksheet.Cells[2, 1].Style.WrapText = true;
//Check if at the minimum height. If not, resize the row
if (eppWorksheet.Row(2).Height < 35.25)
{
eppWorksheet.Row(2).Height = 35.25;
}
I've looked at Autofit rows in EPPlus and it didn't seem to directly answer my question unless I'm reading it wrong.
Here is the solution in a reusable method. Pass in the text value, font used for the cell, summed width of the columns merged, and receive back the row height. Set the row height with the result.
Use of Method
eppWorksheet.Row(2).Height = MeasureTextHeight(cell.Value, cell.Style.Font, [enter the SUM of column widths A-E]);
Reuseable Method
public double MeasureTextHeight(string text, ExcelFont font, double width)
{
if (text.IsNullOrEmpty()) return 0.0;
var bitmap = _bitmap ?? (_bitmap = new Bitmap(1, 1));
var graphics = _graphics ?? (_graphics = Graphics.FromImage(bitmap));
var pixelWidth = Convert.ToInt32(width * 7); //7 pixels per excel column width
var fontSize = font.Size * 1.01f;
var drawingFont = new Font(font.Name, fontSize);
var size = graphics.MeasureString(text, drawingFont, pixelWidth, new StringFormat { FormatFlags = StringFormatFlags.MeasureTrailingSpaces });
//72 DPI and 96 points per inch. Excel height in points with max of 409 per Excel requirements.
return Math.Min(Convert.ToDouble(size.Height) * 72 / 96, 409);
}
I have used a workaround for this and I a had print area A:Q.
I copied merged cells value to column z.
set width of column z to merge cells width.
Then set auto row height true in format.
Hide the z column.
Set print area A:Q
Cons:
There are duplicate data. But we are okay since report is printing and not print z column.
Pros:
Row height works correctly not like calculation method.
Had to tweak the code a little bit by removing the multiplication factor at the return line. May be because i am using this code to get the width of the column.
ws1.Column(colIndx).Width * 7
The multiplication factor is the number of columns been merged.
Related
I intend to write some information to Excel from C#. The code I am using is:
var oXL = new Microsoft.Office.Interop.Excel.Application();
oXL.Visible = false;
var oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(""));
var oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet;
oSheet.Columns.ColumnWidth = 5;
oSheet.Rows.RowHeight = 5;
oSheet.Cells[1, 1] = "Smith";
...
The problem is that I need to set an exact size in centimeters (5) for all the cells. The ColumnWidth property receives the number of points and not centimeters. In various articles I found that 1 point = 0.035cm, meaning that 1cm = 28.57p, but when I pass 5*28.57 for ColumnWidth and for RowHeight, Excel's columns are set to 28.58cm and rows are set to 5.04cm.
How can I solve this case?
Thank you,
The column width in Excel is specified in "Characters", not "Points". The default column width is 8.37 "Characters", 1 character being the width of the 1 character of the default font used to display normal text in Excel.
In order to specify the column width in cm, you need to first convert cm to points using the following function:
double x = xl.Application.CentimetersToPoints(5);
Then use one loop to repeatedly decrease the column width by 0.1 points until it matches the number of points (x). (For columns whose width is greater than 'x' points)
while(ws.Columns.ColumnWidth - 0.1 > points)
{
ws.Columns.ColumnWidth = ws.Columns.ColumnWidth - 0.1;
}
Then use another loop to repeatedly increase the column width by 0.1 points until it matches the number of points (x). (For columns whose width is lesser than 'x' points)
while(ws.Columns.ColumnWidth + 0.1 < points)
{
ws.Columns.ColumnWidth = ws.Columns.ColumnWidth + 0.1;
}
The above method takes a few seconds to execute because of the loops. However, it achieves the solution (setting column width in cm).
Note: In the above code,
xl : Excel application object
ws : Worksheet object
I'm using C# for a VSTO Excel Add In.
Currently I'm setting the columns to be auto fitted with:
newWorkSheet.Columns.AutoFit();
How can I set the Column width to have some additional padding?
In psuedocode it would be:
Column.Width = columnCurrentWidth + 10
For a certain column, make it a bit wider than the auto fit width
I've figure out how to get the width of a column but not set it
int width = ((Excel.Range)newWorkSheet.Cells[rowCount, 2]).Width;
You have to set ColumnWidth and RowHeight of Range.
This should work:
int width = ((Excel.Range)newWorkSheet.Cells[rowCount, 2]).Width;
((Excel.Range)newWorkSheet.Cells[rowCount, 2]).ColumnWidth = width + 10;
I need to draw a histogram, and columns with minimum and maximum values should have other colors that are different from other columns and have different widths!
Now I have done this in two sets, but it's the wrong outcome.
Below is my code:
chart1.Series[0].Color = Color.Blue;
int y;
for (y = 0; y < lbls.Length; y++)
{
chart1.Series[0].Points.AddY(costs[y]);
chart1.ChartAreas[0].AxisX.CustomLabels.Add(new CustomLabel(y, y + 2, lbls[y], 0, LabelMarkStyle.LineSideMark));
}
chart1.Series[1].Color = Color.Red;
chart1.Series[1].Points.AddY(1500);
chart1.ChartAreas[1].AxisX.CustomLabels.Add(new CustomLabel(y+4, y + 6, "lol", 0, LabelMarkStyle.LineSideMark));
its give me:
Not too sure how to change specific column width but and to change a specific i points color do the below.
'Loop through series to find min and max and then color label
Chart1.Series[0].Points[i].Color = Color.Red
P.S.
I'm not sure if you can change just one columns width. I sometimes use Chart1.Series[0]("PixelPointWidth") = some integer to change the width of column series but looking at the msdn page it seems it can only be applied to the series and not individual datapoints.
PixelPointWidth # MSDN
You could just create a new series and add it to the chartarea with just the max and min values and then do Chart1.Series[1]("PixelPointWidth") = some integer to make them wider than the first series
How to set up paragraph width in MigraDoc? All what I imagine is create table and set the column width and then paragraph populate all width. But I need something like next:
var paragraph016 = section.AddParagraph();
paragraph016.Format.Borders.Bottom.Visible = true;
paragraph016.Format.WidowControl = true;
//here must be define paragraph width
Or maybe anybody know how can I draw line on the page, where I can setup width and position of my line?
I use paragraph width as a part of my 'add a horizontal rule' helper method. Using left and right indent works great:
public static void AddHorizontalRule(Section section, double percentWidth, Color? color = null)
{
double percent = (percentWidth < 0.0 || percentWidth > 1.0) ? 1.0 : percentWidth;
Color hrColor = color ?? new Color(96, 96, 96); // Lt Grey default
Unit contentWidth = section.PageSetup.PageWidth - section.PageSetup.LeftMargin - section.PageSetup.RightMargin;
Unit indentSize = (contentWidth - (percent * contentWidth)) / 2.0;
Paragraph paragraph = section.AddParagraph();
paragraph.Format.LeftIndent = indentSize;
paragraph.Format.RightIndent = indentSize;
paragraph.Format.Borders.Top.Visible = true;
paragraph.Format.Borders.Left.Visible = false;
paragraph.Format.Borders.Right.Visible = false;
paragraph.Format.Borders.Bottom.Visible = false;
paragraph.Format.Borders.Top.Color = hrColor;
}
Note that because a section's PageSetup values are 0 and therefore use the default document settings, that to use the client area width as shown above, you need to explicitly set these values in the section.PageSetup before calling this method. I do it this way so that I don't have to pass around the document nor depend on document.LastSection being the section that I am working on. I just pass in a Section object and have at it.
Enjoy!
Brian
You can set the width indirectly by specifying left and right indent. I don't know if this leads to the desired line, but it's worth a try.
A table will work.
An image would also work - best with a vector image (could be PDF), but a raster image with a single pixel in the desired color should also work.
I'm setting the following properties for a DataGridView in my C# project ...
sampleDataGridView.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
sampleDataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
But I notice that the heading's (i.e. text in header cell is offset slightly to the left, for some reason .. The data rows alignment in perfectly in center though ...
What can be causing this ?
It is because there is a sorting glyph (little arrow) for which the DataGridView reserves some space to show the sort order. If you want to disable the sorting glyph, set the SortMode on your column to NotSortable, and your text should then be centered.
Old topic,
i had this problem, and i found out that the problem was that i had the grid datasource-linked to my stuff, and when the columns where created they put the ALLOW SORT ON by default, which created the arrow down button to sort, and the text gets pushed to the left.
small fix to this is:
private void MyDataGridView_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{
e.Column.SortMode = DataGridViewColumnSortMode.NotSortable;
}
I had the same problem as you and it seems it's a framework issue: MS Connect
Figure out the glyph width without refection and supress when out of line:
In the following code I turn on and off sorting while autoadjusting column width to the column header text width. The difference in width between sorting on/off will reveal the width used by the column sort glyph.
As column looses the glyph when column width is set less than autoadjusted width I supress it by inserting symetric left and right column padings.
I also use a dictionary to store ColumnWidth events by datagridview instance to turn width events on and off while seting the width.
I call this crazy code both to autoadjust initial column widths and to manage the column header padings when user drags column widths.
void AdaptColumnHeaderText(DataGridViewColumn column, bool autoSize=false)
{
//Supress column width events
if (dataGridViewColumnWidthEventHandlers.ContainsKey(column.DataGridView))
{
dataGridView1.ColumnWidthChanged -= dataGridViewColumnWidthEventHandlers[column.DataGridView];
}
//Save initial column with as preferred
var w_preferred = column.Width;
if (
column.SortMode.Equals(DataGridViewColumnSortMode.Automatic) &&
column.HeaderCell.Style.Alignment.Equals(DataGridViewContentAlignment.MiddleCenter))
{
//remove all header padding
column.HeaderCell.Style.Padding = new Padding(0, 0, 0, 0);
//Fit column width to header text with AND sort glyph
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
//save column width sort enabled
var w_sort = column.Width;
//Fit column width to header text with NO sort glyph
column.SortMode = DataGridViewColumnSortMode.NotSortable;
//save column width when sort disable
var w_nosort = column.Width;
//Calculate width consumed by sort glyph
var w_glyph = w_sort - w_nosort;
//Nominal column width if glyph width applied left and right of header text
var w_nominal = w_glyph + w_nosort + w_glyph;
//Disable column width autosize
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
//Enable column autosorting
column.SortMode = DataGridViewColumnSortMode.Automatic;
//If autosize option - ignore preferred width and set to nominal
if (autoSize)
{
w_preferred = w_nominal;
}
//Pad depending on final column width
if (w_preferred >= w_nominal)
{
//Preferred width greater than nominal - pad left of text to balance width used by glyph
column.HeaderCell.Style.Padding = new Padding(w_glyph, 0, 0, 0);
}
else
{
//Two symetric glyphs will not fit - pad left and right to supress glyph
w_glyph = (w_preferred - w_nosort) / 2;
column.HeaderCell.Style.Padding = new Padding(w_glyph, 0, w_glyph, 0);
}
column.Width = w_preferred;
Console.WriteLine("name:{0}, glyph:{1}, w_preferred:{2}", column.Name, w_glyph, w_preferred);
}
//re-enable column width events
if (dataGridViewColumnWidthEventHandlers.ContainsKey(column.DataGridView))
{
dataGridView1.ColumnWidthChanged += dataGridViewColumnWidthEventHandlers[column.DataGridView];
}
}
You can add two spaces at the beginning of the column name to compensate the space reseved for the glyph.