找回密码
 立即注册

QQ登录

只需一步,快速开始

iceman

社区贡献组

270

主题

1万

帖子

1万

积分

社区贡献组

积分
19311

活字格认证微信认证勋章元老葡萄

iceman
社区贡献组   /  发表于:2012-2-29 15:44  /   查看:5401  /  回复:0
在 Excel 的使用中,我们都会用到复制某一单元格数据,粘贴到多个单元格中的功能。对于我们来说这个功能是很方便的。这个功能和 Excel 中的拖拽功能类似(拖拽有数据单元格到其他单元格域)。

下面我们就介绍怎么用 Spread 实现以上两种该功能。

1.Spread 的拖拽功能。我们仅仅需要添加一行代码:
  1. FpSPread1.AllowDragFill=true;
复制代码
2.Spread 实现复制某一单元格数据,粘贴到多个单元格中的功能:
  
    在 Ctrl + C 赋值单元格之后,需要在 FpSpread1_ClipBoardPasting 事件中添加以下代码:
  1. private void fpSpread1_ClipboardPasting(object sender, FarPoint.Win.Spread.ClipboardPastingEventArgs e)
  2.         {
  3.             FarPoint.Win.Spread.Model.CellRange cr = default(FarPoint.Win.Spread.Model.CellRange);
  4.             string textdata = null;
  5.             string[] a = null;
  6.             string[] b = null;
  7.             int rowcount = 0;
  8.             int colcount = 0;
  9.             cr = fpSpread1.Sheets[0].GetSelection(0);
  10.             if (cr.RowCount > 1 | cr.ColumnCount > 1)
  11.             {
  12.                 e.Handled = true;
  13.                 if (System.Windows.Forms.Clipboard.GetDataObject().GetDataPresent(System.Windows.Forms.DataFormats.Text))
  14.                 {
  15.                     textdata = (string)System.Windows.Forms.Clipboard.GetDataObject().GetData(System.Windows.Forms.DataFormats.Text);
  16.                     a = textdata.Split(new char[] { (char)13 });
  17.                     rowcount = a.Length - 1;
  18.                     b = a[0].Split(new char[] { (char)9 });
  19.                     colcount = b.Length;
  20.                     for (int i = cr.Row; i <= cr.Row + cr.RowCount - 1; i += rowcount)
  21.                     {
  22.                         for (int x = 0; x <= rowcount - 1; x++)
  23.                         {
  24.                             b = a[x].Split(new char[] { (char)9 });
  25.                             for (int j = cr.Column; j <= cr.Column + cr.ColumnCount - 1; j += colcount)
  26.                             {
  27.                                 for (int y = 0; y <= colcount - 1; y++)
  28.                                 {
  29.                                     string myStr;
  30.                                     myStr = b[0];
  31.                                     fpSpread1.Sheets[0].SetValue(i + x, j + y, myStr.Trim((char)10, (char)30));
  32.                                 }
  33.                             }
  34.                         }
  35.                     }
  36.                 }
  37.             }
  38.         }
  39.     }
复制代码
Demo 下载:
编辑环境:Spread for WinForm 5.0 &amp;&amp; VS 2010
MultipleCellsPaste.zip (40.02 KB, 下载次数: 755)

0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部