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 { }
}
C# 实现复制Excel内容到DataGridview中的更多相关文章
- C#-导入Excel 内容到 DataTable中
C#-导入Excel 内容到 DataTable中 直接传入文件路径,支持所有Excel格式. 缺点:如果数据量庞大会很占内存. public static DataTable ImportExcel ...
- 将简单Excel表格显示到DataGridView中
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- excel中的数据粘贴不全到plsql中,excel 粘贴后空白,Excel复制粘贴内容不全
http://zhidao.baidu.com/link?url=pHZQvfWJzI-lQjl4uP86q4GLcpYHu4o-fdjiYegJS0Cy5HEq5oz0YrUye3iHjmv5CJ3 ...
- Excel 内容粘贴到DataGridView, DataGridView 粘贴到 Excel
void dataGridView1_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Ke ...
- excel数据复制到html表格<textarea>中
方案一 多行文本框接收到复制的excel值后,在文本框的chage事件中,将excel内容分割到二维数组中,然后填充到html的表格的input或textarea中. 数据格式: 单元格复制后的数据格 ...
- C#使用NPOI读取电子表格Excel到DataGridView中
上篇博文中已经介绍了如何写入Excel文件.这篇再介绍一下 如何从Excel中读取数据并保存到DataGridView中. 从Excel中读取数据并保存至DataGridView中,Excel文件第一 ...
- C# 将DataGridView中显示的数据导出到Excel(.xls和.xlsx格式)—NPOI
前言 https://blog.csdn.net/IT_xiao_guang_guang/article/details/104217491 本地数据库表中有46785条数据,测试正常 初次运行程 ...
- ZeroMQ接口函数之 :zmq_msg_copy - 把一个消息的内容复制到另一个消息中
ZeroMQ 官方地址 :http://api.zeromq.org/4-1:zmq_msg_copy zmq_msg_copy(3) ØMQ Manual - ØMQ/3.2.5 Name zm ...
- C#读取Excel表格数据到DataGridView中和导出DataGridView中的数据到Excel
其实想在datagridview中显示excel表格中的数据跟读取数据库中的数据没什么差别,只不过是创建数据库连接的时候连接字段稍有差别. private void btnShow_Click(obj ...
随机推荐
- [DB] 大数据概述
什么是大数据 电商推荐系统 大量订单如何存储(十年) 大量的订单如何计算(不关心算法) 天气预报 大量的天气数据如何存储 大量天气数据如何计算 核心问题 数据的存储:分布式文件系统(HDFS) 数据的 ...
- 016.Python闭包函数以及locals和globals
一 闭包函数 内函数使用了外函数的局部变量,并且外函数把内函数返回出来的过程叫做闭包,这个内函数叫做闭包函数 1.1 闭包函数语法 def outer(): a = 5 def inner(): pr ...
- 010.kubernets的调度系统之daemonset
daemonset简单操作使用 Deployment 是 Kubernetes 中用于处理无状态服务的资源,而 StatefulSet 是用于支持有状态服务的资源,这两种不同的资源从状态的角度对服务进 ...
- IT菜鸟之思科模拟实验(PT)
思科官方的模拟软件:cisco packet tracer 网卡端口类型: Ethernet(以太网) 十兆 FastEthernet 百兆 GigabitEthernet 千兆 交换机的端口默认都是 ...
- mysql基础之数据库变量(参数)管理
数据库的数据存放路径:[root@ren7 mysql]# pwd /var/lib/mysql [root@ren7 mysql]# ls aria_log.00000001 ibdata1 mul ...
- 记一次zabbix-server故障恢复导致的事故 zabbix-server.log -- One child process died
前言 zabbix-server昨天出了个问题,不停的重启.昨天摆弄到晚上也不搞清楚原因,按照网上说的各种操作,各种CacheSize.TimeOut.StartPollers都改了,还有什么Incl ...
- Git如何下载clone指定的tag
Git如何下载clone指定的tag 如上图,我想下载Tags标签为solution-4 的代码,如何处理呢? 命令如下: git clone --branch solution-4 git@gith ...
- 聊聊 apt sources.list 文件格式
前言 之前玩 ubuntu 需要切换国内源地址时,都是网上复制别人提供好的,也不知道是什么意思,拿来就用. 这次花点时间来看一下 apt sources.list 的格式,以及其表示的含义. 格式 s ...
- 中国摄像头CMOS需求潜力旺盛
中国摄像头CMOS需求潜力旺盛 CMOS是Complementary Metal Oxide Semiconductor(互补金属氧化物半导体)的缩写.它是指制造大规模集成电路芯片用的一种技术或用这种 ...
- nvGRAPH三角形计数和遍历示例
nvGRAPH三角形计数和遍历示例 #include " stdlib.h" #include" inttypes.h" #include" stdi ...