fengling2001 你好
这和你数据源中数据的类型有关,你需要将 public string P1 { get; set; } 修改为 public int P1 { get; set; },最终代码如下:
- private void Form1_Load(object sender, EventArgs e)
- {
- fpSpread1.Sheets[0].SheetName = "T1";
- List<MyClass> strList = new List<MyClass>();
- List<MyClass> strList1 = new List<MyClass>();
- strList.Add(new MyClass() { P1 = 111 });
- strList.Add(new MyClass() { P1 = 222 });
- strList.Add(new MyClass() { P1 = 333 });
- strList.Add(new MyClass() { P1 = 444 });
- strList1.Add(new MyClass() { P1 = 11 });
- strList1.Add(new MyClass() { P1 = 22 });
- strList1.Add(new MyClass() { P1 = 33 });
- strList1.Add(new MyClass() { P1 = 44 });
- FarPoint.Win.Spread.Data.SpreadDataBindingAdapter data = new FarPoint.Win.Spread.Data.SpreadDataBindingAdapter();
- data.DataSource = strList;
- data.Spread = fpSpread1;
- data.SheetName = "T1";
- data.MapperInfo = new FarPoint.Win.Spread.Data.MapperInfo(15, 3, strList.Count, 1);
- data.FillSpreadDataByDataSource();
- data.Dispose();
- FarPoint.Win.Spread.Data.SpreadDataBindingAdapter data1 = new FarPoint.Win.Spread.Data.SpreadDataBindingAdapter();
- data1.DataSource = strList1;
- data1.Spread = fpSpread1;
- data1.SheetName = "T1";
- data1.MapperInfo = new FarPoint.Win.Spread.Data.MapperInfo(15, 4, strList1.Count, 1);
- data1.FillSpreadDataByDataSource();
- data1.Dispose();
- //单元格类型必须在绑定之后设置才有效
- FarPoint.Win.Spread.CellType.NumberCellType numCellType = new FarPoint.Win.Spread.CellType.NumberCellType();
- numCellType.DecimalPlaces = 2;
- this.fpSpread1.ActiveSheet.Cells[15, 3, 15 + strList.Count, 3 + 1].CellType = numCellType;
- this.fpSpread1.ActiveSheet.Cells[15, 4, 15 + strList1.Count, 4 + 1].CellType = numCellType;
- for (int t = 15; t <= 15 + strList.Count; t++)
- {
- CellSumFormula(fpSpread1.ActiveSheet.Cells[t - 1, 5], 'D', 1, t);
- }
- }
- private void CellSumFormula(FarPoint.Win.Spread.Cell cell, char startColumn, int span, int row)
- {
- char strEnd = (char)(startColumn + span);
- string strFormula = "SUM(" + startColumn.ToString() + row.ToString() + ":" + strEnd.ToString() + row.ToString() + ")";
- cell.Formula = strFormula;
- }
- public class MyClass
- {
- public int P1 { get; set; }
- }
复制代码 |