对具有相同值的单元格合并,Spread for ASP.Net提供了在后端代码实现:

  1: // Create some content in two cells.
  2: FpSpread1.ActiveSheetView.Cells[1,1].Text = "These six cells are spanned.";
  3: FpSpread1.ActiveSheetView.Cells[2,2].Text = "This is text in 2,2.";
  4: // Span six cells including the ones with different content.
  5: FpSpread1.ActiveSheetView.AddSpanCell(1, 1, 2, 3);

 

那么能否通过前端实现单元格合并呢? 这个博客介绍如何能够利用Spread单元格的ROWSPAN属性,来合并单元格: 即,在客户端把相同的值的单元格进行合并。

下面是一个简单的JavaScript函数,遍历行找到具有相同值的连续行的特定列。一旦我们得到这个单元格的范围,可简单地设定第一个单元格的RowSpan属性, 这会创建一个跨区单元格,然而看起来好像是单元格具有相同值已经被合并。

  1: <script type="text/javascript">
  2:      function Button2_onclick()
  3:      {
  4:         var spread = document.getElementById("FpSpread1");
  5:         var rc = spread.GetTotalRowCount();
  6:         var r = 0;
  7:         while (r != rc - 1)
  8:         {
  9:             r1 = r;
 10:             var inc = 0;
 11:             while (r1 != -1)
 12:             {
 13:                var val1 = spread.GetValue(r1, 1);
 14:                var val2 = spread.GetValue(r1 + 1, 1);
 15:                if (val1 == val2)
 16:                {
 17:                   inc++;
 18:                   r1++;
 19:                }
 20:                else
 21:                {
 22:                   var cell = spread.GetCellByRowCol(r, 1);
 23:                   cell.rowSpan = inc + 1;
 24:                   r = r1 + 1;
 25:                   r1 = -1;
 26:                 }
 27:              }
 28:         }
 29:      alert('Cells with same values merged');
 30:   }
 31: </script>

 

Spread_JS_Merge

源码: