Spread 表格控件拥有丰富的单元格类型,其中 ImageCellType 用于在单元格中展示图片。本篇文章将为你展示,如何通过 ImageCellType 单元格类型,直接从 Excel 中复制图片到单元格中呢?
首先,需要添加 Spread PreviewKeyDown 事件来监听 Ctrl + V 粘贴事件:
1: public Form1()
2: {
3: InitializeComponent();
4: this.fpSpread1.PreviewKeyDown +=
5: new PreviewKeyDownEventHandler(fpSpread1_PreviewKeyDown);
6: }
然后,在事件中获取剪切板中数据转换成图片:
1: Bitmap bitmap = Clipboard.GetData(DataFormats.Bitmap) as Bitmap;
最后,把图片转换为 Shape 的背景图:
1: FarPoint.Win.Spread.CellType.ImageCellType imgType =
new FarPoint.Win.Spread.CellType.ImageCellType();
2: this.fpSpread1.ActiveSheet.ActiveCell.CellType = imgType;
3: this.fpSpread1.ActiveSheet.ActiveCell.Value = bitmap;
4: this.fpSpread1.ActiveSheet.ActiveColumn.Width = 200;
5: this.fpSpread1.ActiveSheet.ActiveRow.Height = 200;
效果图:
Demo 下载:编辑环境 VS2010 && Spread for Winforms 7.0 && C# 点击下载