在导出 Excel 的时候, 常常要须要给列设置宽度或给行设置高度, 在使用 NPOI 或 EppPlus 等组件进行操做的时候, 列宽和行高的单位都不是像素, 好像是英寸,具体是啥也说不清。this
日常在使用单位时, 已经习惯使用像素了, 因此但愿在 EXCEL 里设置列宽或行高时也使用像素。spa
以前在使用 Aspose.Cells 时, 发现这个组件有个 SetRowHeightPixel 方法, 能够使用像素设置行高, 因而使用反编译看了一下实现原理:code
private static float GetDpiX() { try { using (Bitmap image = new Bitmap(1, 1)) { using (Graphics graphics = Graphics.FromImage(image)) { return graphics.DpiX; } } } catch { return 72; } } public void SetRowHeightPixel(int row, int pixels) { double height = (double)((float)pixels * 72f / (float)GetDpiX()); this.SetRowHeight(row, height); }