首页 > 其他分享 >NPOI Excel指定范围内插入图片(纵横比)

NPOI Excel指定范围内插入图片(纵横比)

时间:2024-05-16 16:21:10浏览次数:15  
标签:width int image float Excel NPOI height 插入 var

NPOI Excel指定范围内插入图片(纵横比),考虑列宽,行高比。

// I assume you want to insert the image into the worksheet loaded in the workbook parameter
// You can adjust the ratio variable accordingly to control the ratio of width and height
// You can also adjust maxWidth and maxHeight to set the maximum size of the image on the worksheet
internal static void InsertImageWithRatio(IWorkbook workbook, int sheetIndex, int firstColumn, int firstRow, int lastColumn, int lastRow, string imagePath)
{
    ISheet sheet = workbook.GetSheetAt(sheetIndex);

    var width = 0.0;
    // 获取列宽度的像素大小
    for (int column = firstColumn; column < lastColumn; column++)
    {
        width += Math.Round(sheet.GetColumnWidthInPixels(column) * 1.285, 0);
        //Console.WriteLine($"Column {column} width in pixels: {width}");
    }
    var height = 0.0;
    // 获取行高度的像素大小
    var heightInPoints = sheet.GetRow(firstRow)?.HeightInPoints ?? sheet.DefaultRowHeightInPoints;
    for (int row = firstRow; row < lastRow; row++)
    {
        height += Math.Round(heightInPoints * 1.666f, 0);
        //Console.WriteLine($"Row {row} height in pixels: {height}");
    }

    var drawing = sheet.CreateDrawingPatriarch();
    // 图片位置和大小
    var anchor = drawing.CreateAnchor(0, 0, 0, 0, firstColumn, firstRow, lastColumn, lastRow);
    anchor.AnchorType = AnchorType.MoveDontResize;

    FileStream file = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
    byte[] buffer = new byte[file.Length];
    file.Read(buffer, 0, (int)file.Length);

    Image image = Image.Load(buffer);
    int imageWidth = image.Width;
    int imageHeight = image.Height;

    var pictureIndex = workbook.AddPicture(buffer, PictureType.PNG);
    var picture = drawing.CreatePicture(anchor, pictureIndex);

    // 根据图片的宽高比例进行等比例缩放
    float widthRatio = imageWidth / (float)width;
    float heightRatio = imageHeight / (float)height;
    float scaleRatio = Math.Min(widthRatio, heightRatio);

    // 根据图片的宽高比例进行等比例缩放
    float colWidth = (float)width / imageWidth * scaleRatio;
    float rowHeight = (float)height / imageHeight * scaleRatio;

    picture.Resize(rowHeight, colWidth); // 锁定纵横比
}

标签:width,int,image,float,Excel,NPOI,height,插入,var
From: https://www.cnblogs.com/Ezio/p/18196169

相关文章

  • 已卸载Tuxera NTFS for Mac后仍在ntfs磁盘插入后仍有提示
    “Microsoft NTFS by Tuxeracouldnotvalidateyourproductkey.PleasecontactTuxera'semailsupportforhelp.”我之前安装过MicrosoftNTFS但是过期了,我就给它卸载了,但是每次插入u盘还是会弹出上面一行内容的提示框。于是我找到这个朋友发的[文章](https://zhuanla......
  • 使用 PHP 创建 Excel 读取器类
    介绍:PHPExcel-1.8.1读取excel创建ExcelReader类:ExcelReader类旨在从Excel文件中读取数据。它以文件路径作为输入,并提供一个方法来从Excel文件中读取数据。<?phprequire_once"lib/PHPExcel-1.8.1/Classes/PHPExcel.php";classExcelReader{protected$file;......
  • 接口自动化测试框架【python+requests+pytest+excel+allure+jenkins】
    一.在整个框架中需要用到哪些东西?1.python环境安装https://www.python.org/downloads/windows/下载解压后直接安装,选择Addpythontopath2.JAVA环境配置安装包下载地址:https://www.oracle.com/java/technologies/downloads/环境变量设置参数如下:●变量名:JAVA_HOME●变......
  • gorm实现MySQL的INSERT INTO ... ON DUPLICATE KEY UPDATE差异化插入和更新
    比如插入f_create_uid,更新时忽略f_create_uid,只更新f_update_uid。可使用gorm的BeforeCreate和BeforeUpdate钩子,这两个钩子分别在创建和更新记录之前被调用。//BeforeCreate在创建记录之前调用func(dob*MyStruct)BeforeCreate(tx*gorm.DB)(errerror){dob......
  • 广东各高校2023/2022/2021近三年录取分数线(excel文件下载)
    为了帮助考生更好地进行志愿填报,更好的对数据筛选,故整理广东各高校2023/2022/2021三年录取分数excel文件,部分数据及文件见下图,数据根据历年录取分数线汇总,仅供参考,详细请登陆各高校网站查询。如有需要,可根据步骤下载文件:文件列表及数据如下图所示,真实有效。关注上述公众......
  • NPOI读取模板文件生成Excel
    前不久实现了用NPOI组件替代Microsoft.Office.Interop.Excel原生组件实现导出数据到Excel的需求,其中踩了几个坑,这里记录一下。不能使用wps创建模板文件不能使用一个文件流,对已存在Excel文件进行修改NPOI中sheet、row、cell都是以0作为起始序号,Office原生组件是以1作为起始序......
  • openGauss 使用合并方式更新和插入数据
    使用合并方式更新和插入数据在用户需要将一个表中所有的数据或大量的数据添加至现有表的场景下,openGauss提供了MERGEINTO语句通过两个表合并的方式高效地将新数据添加到现有表。MERGEINTO语句将目标表和源表中数据针对关联条件进行匹配,若关联条件匹配时对目标表进行UPDATE,关联......
  • SQL 复杂查询更新插入
    通过现有数据快速插入构造新数据1INSERTINTO`merge`.`ods_binlog_traffic_vehicle_tracks`(2`id`,3`vehicle_no`,4.5.6.7`gps_datetime`,8.9.10.11`update_time`,12.13.14.15)......
  • Mysql批量插入数据(python脚本)
    有个需求:需要在table1中插入205条数据,role_id固定为65,menu_id从91开始方法二:python脚本实现PyMySQL是一个纯Python编写的MySQL客户端库,用于连接和操作MySQL数据库。以下是PyMySQL的主要功能和优点:功能:数据库连接:PyMySQL提供了connect()方法,用于建立与MySQL数据库的连接。一旦......
  • Mysql批量插入数据
    有个需求:需要在table1中插入205条数据,role_id固定为65,menu_id从91开始方法一:存储过程1、什么是存储过程?MySQL存储过程(StoredProcedure)是一组为了完成特定功能的SQL语句集合。用户通过指定的存储过程名称和参数(如果有的话)来调用并执行它。存储过程可以在程序中被多次调用,并......