2

I will not intentionally post any codes in this question as it is not needed to answer this question, for some reasons I need to set Height and Width of the cells in my worksheet via pixels value and not the standard activeWorksheet.Rows.RowHeight which accept standard value. How can I successfully set Height and Width of a cell via pixels? Thanks.

FabioEnne
  • 732
  • 1
  • 14
  • 43

1 Answers1

3

The formula:

pixels = points * DPI / 72

can be reversed to:

points = pixels / DPI * 72

This way you can set Row Height using pixels.

To get the DPI in C# see this.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • But the question is how you set it in Excel Interop: excWorksheet.Columns[1].ColumnWidth = pixels / DPI * 72; is way to many pixels when you put your mouse over the column in excel. – John Kurtz Jan 22 '19 at 22:54
  • @JohnKurtz you're talking about Column width, the question is about RowHeight. – Jeremy Thompson Jan 23 '19 at 03:52
  • Sorry, I just assumed that they would be measured the same way! Any insights on how the Columns Width IS handled? Title asks about height and width. – John Kurtz Jan 23 '19 at 15:15
  • For Arial 10, I found a good approximation to be: excWorksheet.Columns[1].ColumnWidth = 0.14286f * (pixels - 5); – John Kurtz Jan 23 '19 at 16:40