You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用SpreadSheetLight库设置图片属性(C#)

解决SpreadSheetLight中图片“随单元格移动和调整大小”的设置问题

要在SpreadSheetLight中开启图片的“随单元格移动和调整大小”属性,只需设置SLPicture实例的Placement属性为SLPicturePlacement.MoveAndSizeWithCells即可。

修改你的代码如下:

byte[] imgdata = File.ReadAllBytes(sTempSnapName);
SLPicture pic = new SLPicture(imgdata, ImagePartType.Png);

// 设置图片尺寸(英寸转EMU)
pic.ResizeInEMU((long)SLConvert.FromInchToEmu(3.51), (long)SLConvert.FromInchToEmu(2.71));
pic.SetPosition(27, 0);

// 设置图片随单元格移动和调整大小
pic.Placement = SLPicturePlacement.MoveAndSizeWithCells;

oMainReportXlsx.InsertPicture(pic);

补充说明

  • SLPicturePlacement枚举对应Excel的图片位置属性,包含三个选项:
    • MoveAndSizeWithCells:随单元格移动和调整大小(你需要的选项)
    • MoveButDontSizeWithCells:随单元格移动但不调整大小
    • DontMoveOrSizeWithCells:不随单元格移动或调整大小
  • 设置该属性后,图片会与绑定的单元格同步变化,同时能解决打印时图片被压缩的问题,因为图片尺寸会跟随单元格的打印布局自动适配。

内容的提问来源于stack exchange,提问作者DK2014

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 13:43:09