C# 实现复制Excel内容到DataGridview中

时间:2023-03-09 00:17:58
C# 实现复制Excel内容到DataGridview中

业务要求:复制:将Excel内容复制到datagridview中

最终效果:复制Excel内容,点击datagridview中的某个单元格,顺着这个单元格自动填充自动增加行。偷懒了,没写填充在选择哪些行就填充到哪些行。

1、添加方法

 1 #region
2 protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
3 {
4 //回车新增行 个人笔记
5 switch (keyData)
6 {
7
8 case System.Windows.Forms.Keys.Enter:
9 {
10
11 if (this.Text.IndexOf("订单") > -1)
12 {
13 try
14 {
15 if (edcol > 0 && edrow == dsMainFilter1.Tables[0].Rows.Count - 1)
16 {
17
18 DataRow row = dsMainFilter1.Tables[0].NewRow();
19 str = SYSVARS.vars.userId + System.DateTime.Now.ToString("yyyyMMddHHmmss").ToString().Trim() + dsMainFilter1.Tables[0].Rows.Count.ToString();//以时间标识代码不同的单据号 28 dsMainFilter1.Tables[0].Rows.Add(row);
29 }
30
31 this.dataGridView3.CurrentCell = dataGridView3[3, dsMainFilter1.Tables[0].Rows.Count - 1];
32 dataGridView3.BeginEdit(true);
33 }
34 catch { }
35 }
36 }
41
42 return true;
43 }
44
45 #region excel复制粘贴功能
46 try
47 {
48 if (this.Text.IndexOf("订单")>-1)
49 {
50 if (keyData == (Keys.V | Keys.Control)) // ctrl+V
51 {
52 bool bd = dataGridView3.Focus();//避免影响到界面上其他功能使用粘贴
53 if (bd == true)
54 {
55 IDataObject idataObject = Clipboard.GetDataObject();
56 string da = Clipboard.GetText();
57 string[] s = idataObject.GetFormats();
58 copydata(da);
59 return true;//很重要,不写将会把所有值填充在最后一个单元格里面
60 }
61
62 }
63 }
64 }
65 catch { }
66 #endregion
67 return base.ProcessCmdKey(ref msg, keyData);
68 }
69 #endregion

2、处理剪切板的数据

 private void copydata(string data1) {
string clipboardText = Clipboard.GetText(); //获取剪贴板中的内容 if (data1.Trim().Length < 1) { return; }
try {
int colnum = 0;
int rownum = 0;
for (int i = 0; i < clipboardText.Length; i++)
{
if (clipboardText.Substring(i, 1).Equals("\t"))
{
colnum++;
}
if (clipboardText.Substring(i, 1).Equals("\n"))
{
rownum++;
}
}
//粘贴板上的数据来源于EXCEL时,每行末尾都有\n,来源于DataGridView是,最后一行末尾没有\n
if (clipboardText.Substring(clipboardText.Length - 1, 1) == "\n")
{
rownum--;
}
colnum = colnum / (rownum + 1);
object[,] data; //定义object类型的二维数组
data = new object[rownum + 1, colnum + 1]; //根据剪贴板的行列数实例化数组
string rowStr = "";
//对数组各元素赋值
for (int i = 0; i <= rownum; i++)
{
for (int j = 0; j <= colnum; j++)
{
//一行中的其它列
if (j != colnum)
{
rowStr = clipboardText.Substring(0, clipboardText.IndexOf("\t"));
clipboardText = clipboardText.Substring(clipboardText.IndexOf("\t") + 1);
}
//一行中的最后一列
if (j == colnum && clipboardText.IndexOf("\r") != -1)
{
rowStr = clipboardText.Substring(0, clipboardText.IndexOf("\r"));
}
//最后一行的最后一列
if (j == colnum && clipboardText.IndexOf("\r") == -1)
{
rowStr = clipboardText.Substring(0);
}
data[i, j] = rowStr;
}
//截取下一行及以后的数据
clipboardText = clipboardText.Substring(clipboardText.IndexOf("\n") + 1);
}
clipboardText = Clipboard.GetText();
int start, end = -1, index, rowStart = 0, columnStart = 0; rowStart = r;//选中单元格的行号
columnStart = Icol;//选中单元格的列号 for (int i = 0; i <=rownum; i++)
{
#region 如果datagridview中行数不够,就自动增加行
if ((i + rowStart) > dataGridView3.Rows.Count - 1)
{
              //添加新行            

DataRow row = dsMainFilter1.Tables[0].NewRow();
str = SYSVARS.vars.userId + System.DateTime.Now.ToString("yyyyMMddHHmmss").ToString().Trim() + dsMainFilter1.Tables[0].Rows.Count.ToString();//以时间标识代码不同的单据号 dsMainFilter1.Tables[0].Rows.Add(row);
} #endregion for (int j = 0; j <= colnum; j++)//将值赋值过去---如果datagridview中没有自动增加列
{
#region 需要判断单元格是不是只读的,是只读的就不用不赋值
bool iszd = this.dataGridView3.Rows[i + rowStart].Cells[j + columnStart].ReadOnly;
if (iszd == true)
{
continue;
}
#endregion string sjz = "";
try
{
sjz = data[i, j].ToString();
}
catch { sjz = ""; }
if (sjz.Trim().Length < 1) { continue; }//直接复制this.dataGridView3.Rows[i + rowStart].Cells[j + columnStart].Value = sjz;
}
} }
catch { }
}