Excel行高与像素值,列宽与像素值之间的转换,绘制Excel像素画。

private static float GetDpiX()
{
    try
    {
        using (Bitmap image = new Bitmap(1, 1))
        {
            using (Graphics graphics = Graphics.FromImage(image))
            {
                return graphics.DpiX;
            }
        }
    }
    catch
    {
        return 72;
    }
}

传入像素numPixel

int numPixel = (int)numericUpDown1.Value;
Bitmap image = ConvertToFormat(fileImage.Image, PixelFormat.Format24bppRgb);
//Bitmap image = (Bitmap)fileImage.Image;
g = pictureBox1.CreateGraphics();


double rowHeight = (double)((float)numPixel * 72f / (float)GetDpiX());
                 
for (int i = 0; i < 100; i++)
{


    double colWidth = Math.Round((numPixel < 12 ? (numPixel / 12.00) : (1 + ((numPixel - 12) / 7.00))), 2);
    int _numPixel = (int)Math.Round(colWidth < 1 ? colWidth * 12.0 : 12 + (colWidth - 1) * 7.00); 
    Console.WriteLine(string.Format($"{numPixel}  {colWidth}   {_numPixel}"));
    numPixel = numPixel + 1;
    //double width = (double)((float)numPixel * 72f / (float)GetDpiX());
}

 打印出100个与excel里面对比

 

10.08
20.17
30.25
40.33
50.42
60.5
70.58
80.67
90.75
100.83
110.92
121
131.14
141.29
151.43
161.57
171.71
181.86
192
202.14
212.29
222.43
232.57
242.71
252.86
263
273.14
283.29
293.43
303.57
313.71
323.86
334
344.14
354.29
364.43
374.57
384.71
394.86
405
415.14
425.29
435.43
445.57
455.71
465.86
476
486.14
496.29
506.43
516.57
526.71
536.86
547
557.14
567.29
577.43
587.57
597.71
607.86
618
628.14
638.29
648.43
658.57
668.71
678.86
689
699.14
709.29
719.43
729.57
739.71
749.86
7510
7610.14
7710.29
7810.43
7910.57
8010.71
8110.86
8211
8311.14
8411.29
8511.43
8611.57
8711.71
8811.86
8912
9012.14
9112.29
9212.43
9312.57
9412.71
9512.86
9613
9713.14
9813.29
9913.43
10013.57
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Python中,可以使用openpyxl库来设置Excel表格的行高列宽。根据引用\[1\]和引用\[2\]的示例代码,可以通过以下方式设置行高列宽: 1. 设置单行和一列的长和宽: - 调整列宽:使用`ws.column_dimensions\['A'\].width = 20.0`来设置A列的宽度为20.0。 - 调整行高:使用`ws.row_dimensions\[1\].height = 40`来设置第1行的高度为40。 2. 设置所有行和全部列的长和宽: - 首先,获取表格的最大行数和最大列数,可以使用`ws.max_row`和`ws.max_column`。 - 然后,使用循环来设置每一行的高度和每一列的宽度。例如,使用`ws.row_dimensions\[i\].height = height`来设置第i行的高度,使用`ws.column_dimensions\[get_column_letter(i)\].width = width`来设置第i列的宽度。 需要注意的是,行高列宽的单位是以字符为基准的,可以根据需要进行调整。另外,设置完行高列宽后,需要使用`wb.save('test.xlsx')`来保存修改后的Excel文件。 以上是使用openpyxl库来设置Excel表格的行高列宽的方法。希望对你有帮助! #### 引用[.reference_title] - *1* *2* [Python使用OpenPyXl设置Excel表格中的单元格大小(行高列宽)](https://blog.csdn.net/COCO56/article/details/108237690)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Python openpyxl 实现Excel自适应列宽的N种方法,亲测可用](https://blog.csdn.net/qq_33704787/article/details/124722917)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

功克

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值