Spread ASP.NET自定义条形码CellType

发布时间:2014/01/29 00:01 发布者:roger.wang

返回博客中心

正如您所知,Spread是非常灵活的,当涉及到自定义单元格类型的时候--即使条形码单元格并不是Spread原生的。用ASP.NET的创建它将是一个非常轻便的任务。本文选择的是Aspose条形码生成器.NET控件(可生成任何条码生成器)。当然您也可以使用一个在线生成器:传递一个查询字符串的URL即可。

barcode-300x152

首先,我们自定义一个BarcodeCellType:

  1: [Serializable()]
  2: public class BarcodeCellType : FarPoint.Web.Spread.TextCellType
  3: {
  4:  public override Control PaintCell(string id, TableCell parent, Appearance style, Inset margin, object value, bool upperLevel)
  5:  {
  6:   parent.Attributes.Add("FpCellType", "BarcodeCellType");
  7:   ApplyStyleTo(parent, style, margin, true);
  8:  
  9:   System.Web.UI.WebControls.Image c = new System.Web.UI.WebControls.Image();
 10:   c.AlternateText = "";
 11:   c.ImageUrl = "~/bcimagemanager.aspx?value=" + value.ToString();
 12:   return c;
 13:  }
 14: }

 

我们继承了TextCellType, 因为它GetEditorControl类是可接受单元格的编辑模式字符串值,我们还需要override就是paintCell函数。

通过后台代码生成barecode图片。 添加一个新的ASPX页面到。在这个新页面的服务器端代码,我们将引用的产生我们的条码图像组件:

  1: using Aspose.BarCode;

 

  1: public partial class bcimagemanager : System.Web.UI.Page
  2:  {
  3:   protected void Page_Load(object sender, EventArgs e)
  4:   {
  5:    if (Request.QueryString["value"].ToString() == null) return;
  6:    byte[] imgContent = GenerateBarCode(Request.QueryString["value"].ToString());
  7:    Response.ContentType = "image/jpeg";
  8:    Response.BinaryWrite(imgContent);
  9:   }
 10: public byte[] GenerateBarCode(string codeInfo)
 11:   {
 12:   using (MemoryStream ms = new MemoryStream())
 13:    {
 14:     BarCodeBuilder bb = new BarCodeBuilder();
 15:     bb.CodeText = codeInfo;
 16:     bb.SymbologyType = Symbology.Code128;
 17:     bb.BarCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
 18:     byte[] imgContent = new Byte[ms.Length];
 19:     ms.Position = 0;
 20:     ms.Read(imgContent, 0, (int)ms.Length);
 21:     return imgContent;
 22:    }
 23:   }
 24:  }
 25: 

 

使用URL中的查询字符串,我们将单元格的值传递给这个aspx页面。图像在页面上生成的页面的URL是由Image控件的ImageUrl属性引用。

见源码:


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网