从数据库加载图片并呈现在Spread里

发布时间:2013/12/06 00:12 发布者:roger.wang

返回博客中心

Spread提供了数据绑定功能,它允许可以绑定Spread到不同类型的数据源。请参考下面的链接,介绍如何绑定Spread到数据源:

http://helpcentral.componentone.com/NetHelp/SpreadNet6/ASP2/spweb-databound.html

数据库中的数据多种多样,从text到boolean、images。由此,Spread加载不同类型的数据映射为不同的列类型(celltypes)。例如:textual文本数据类型被映射为TextCellType类型,数字类型被映射为CurrencyCellType、IntegerCellType或DoubleCellType类型。

但是,存储在数据库中的images数据不能给直接映射加载。Images在数据库中存储的格式为byte[], 因此Spread加载后会映射为字节流对象。本文接着讨论Spread中如何加载Images数据类型。

Spread加载数据库中图像的方法理论如下:把保存在数据库中的字节数组转换为位图图像,在位图图像创建后,它被保存在项目文件夹下,以便它可以被Spread直接访问。

以下是代码片段:

  1: OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(@"\App_Data\C1NWind.mdb"));
  2: OleDbCommand comm = new OleDbCommand("Select FirstName, Photo from Employees", conn);
  3: OleDbDataAdapter adap = new OleDbDataAdapter(comm);
  4: System.Data.DataSet ds = new System.Data.DataSet();
  5: adap.Fill(ds);
  6: FpSpread1.DataSource = ds;
  7: int index = 0;
  8: foreach (DataRow dr in ds.Tables[0].Rows)
  9: {
 10:      //Code to save the binary image to an image file in the website folder
 11:      barr = (byte[])ds.Tables[0].Rows[index][1];
 12:      TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
 13:      Bitmap bm = (Bitmap)tc.ConvertFrom(barr);
 14:      bm.Save(Server.MapPath("~/Images/Image" + (index + 1).ToString() + ".jpg"));
 15: }
 16: 

最后,你需要修改加载图片的单元格的celltype为ImageCellType,用于在Spread中呈现images。 请通过如下链接了解更多细节:

http://helpcentral.componentone.com/NetHelp/SpreadNet6/ASP2/spweb-setimagecell.html

如上链接所示,ImageCellType类的ImageURL属性用于加载图片,

正如上面的链接所述,ImageCellType的单元格的ImageUrl属性用于加载图像。因此,只要将特定的图像URL到ImageUrl。你将为每个单元创建ImageCellType的一个新实例,作为每个单元格中被加载的新图像。

以下是代码片段描述:

  1: //Code to load the image into the Spread cell
  2: FpSpread1.ActiveSheetView.Cells[index, 1].Value = null;
  3: FarPoint.Web.Spread.ImageCellType ict = new FarPoint.Web.Spread.ImageCellType();
  4: ict.TextOnRight = false;
  5: ict.ImageAlign = ImageAlign.AbsMiddle;
  6: ict.ImageUrl = "~/Images/Image" + (index + 1).ToString() + ".jpg";
  7: FpSpread1.ActiveSheetView.Cells[index, 1].CellType = ict;
  8: 

 

代码如下:

 

 

 


关于葡萄城

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

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