在 Spread for WinForms 中提供了FpSpread1.ActiveSheet.DisplayZero 属性,可以直接设置是否显示单元格中的 0 值,然而,在Spread for ASP.NET 中没有提供相应的属性,如果要实现同样的功能,我们可以通过自定义 CellType 来实现。
首先,创建自定义 CellType,重写 Format 和 Parse 方法:
[Serializable] public class CDoubleCellType : FarPoint.Web.Spread.DoubleCellType { public override string Format(object obj) { if (obj != null && Convert.ToDouble(obj.ToString()) == 0) { return ""; } else { return base.Format(obj); } } public override object Parse(string s) { if (s == "" || s == "0") { return 0; } else { return base.Parse(s); } } }
使用 CDoubleCellType 类型:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { FpSpread1.ActiveSheetView.RowCount = 10; FpSpread1.ActiveSheetView.ColumnCount = 10; FpSpread1.ActiveSheetView.Columns[0].CellType = new CDoubleCellType(); FpSpread1.ActiveSheetView.Columns[0].Formula = "B1*C1"; FpSpread1.ClientAutoCalculation = true; FpSpread1.ActiveSheetView.Columns[3].CellType = new CDoubleCellType(); } }
源码下载:VS2010 + Spread .NET 6.0.3505
DisplayZero_CellType.zip (10.84 kb)