Aspose.Cell模板制作技巧总结

Aspose.Cells通过制作Xls模板实现导出,被官方称之为“Smart Maker”,文档地址在:https://docs.aspose.com/display/cellsnet/Using+Smart+Markers

一些不错的帖子:

https://www.cnblogs.com/chenwenbin/articles/7001719.html 

https://blog.csdn.net/kongwei521/article/details/41647747 

https://www.cnblogs.com/wuhuacong/archive/2011/02/23/1962147.html

实际工作中用到的场景:

(1)使用动态公式实现合计

&=&=SUMPRODUCT(VALUE(E4:E{-1})),将E4行至合计行上一行单元格中的所有值先转换为数字再合计

(2)在单元格中插入二维码(图片同原理)

目前经过多次试验,Aspose.Cells只支持列表形式的图片数据源,并且是以byte[]形式传入。我这里的处理方式是将二维码字段命名为QRCODE_XXX,并且进行特殊处理。这里使用到了第三方二维码库Gma.QrCodeNet

if (d.Key.IndexOf("qrcode", StringComparison.OrdinalIgnoreCase) > -1)
                        {
                            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
                            qrEncoder.TryEncode(d.Value.ToString(), out QrCode qrCode);
                            FixedModuleSize moduleSize = new FixedModuleSize(1, QuietZoneModules.Zero);
                            GraphicsRenderer render = new GraphicsRenderer(moduleSize, Brushes.Black, Brushes.White);
                            using (MemoryStream ms = new MemoryStream())
                            {
                                render.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms);
                                Bitmap bmp = (Bitmap)Image.FromStream(ms);
                                var tms = new MemoryStream();
                                bmp.Save(tms, ImageFormat.Png);
                                byte[] byteImage = new byte[tms.Length];
                                byteImage = tms.ToArray();
                                List<object> QRds = new List<object>();
                                QRds.Add(new { IMG = byteImage });
                                designer.SetDataSource(d.Key, QRds);
                            }
                        }

在模板中,由于这个二维码属于表头数据,所以需要使用&=$写法。 如果是属于列表中的,则使用&=列表名.字段名的写法:

&=$QRCODE_FHDBH(Picture:FitToCell), 其中Picture:FitToCell是指插入图片后自适应单元格尺寸缩放。

官方支持的参数为:

  • Picture:FitToCell - Auto-fit the image to the cell’s row height and column width.
  • Picture:ScaleN - Scale height and width to N percent.
  • Picture:Width:Nin&Height:Nin - Render the image N inches high and N inches wide. You can also

渲染效果:

原文地址:https://www.cnblogs.com/cdoneiX/p/12832510.html