我要把EXCEL中的4个数量,如长 20 宽 21.5 高 40 数量 1008 一起复制到我自己开发的软件上来,窗口上一样放了四个文本框控件 我要把他们一起复制过来,同时一并对应输入到窗口上的各个文本框中。
一般是一次只能复制一个,就是你4个一起复制了,到时候粘贴也只能粘贴一个,怎么4个一起粘贴下来的同时对号入坐呢???
一般是一次只能复制一个,就是你4个一起复制了,到时候粘贴也只能粘贴一个,怎么4个一起粘贴下来的同时对号入坐呢???
string[] texts = text.Split(' ');
将texts中的各个分别赋给各个控件
你可以写一个剪切板监测的功能,如果发现ClipBoard里有你需要的数据,直接取出来,挨个填写TextBoxprivate const int WM_DRAWCLIPBOARD = 776;
[DllImport("user32.dll")]
public static extern int SetClipboardViewer(int windowHandle);protected override void WndProc(ref Message message)
{
base.WndProc(ref message);
if (message.Msg == WM_DRAWCLIPBOARD)
{
string text = Clipboard.GetText();
//判断text是否符合数据的要求,如果符合,分割字符,挨个赋值到TextBox中去
...
}
}
public partial class Form1 : Form
{
CusTextBox tb1, tb2, tb3, tb4; public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//向当前窗体加入4个CusTextBox控件 tb1 = new CusTextBox();
tb1.Bounds = new Rectangle(100, 60, 80, 21);//设置文本框大小及位置
tb1.PaseExcelText += new CusTextBox.EventPaseExcelText(tb_PaseExcelText);//注册文本粘贴事件
this.Controls.Add(this.tb1);
tb1.Visible = true; tb2 = new CusTextBox();
tb2.Bounds = new Rectangle(tb1.Right + 10, tb1.Location.Y, tb1.Width, tb1.Height);//设置文本框大小及位置
tb2.PaseExcelText += new CusTextBox.EventPaseExcelText(tb_PaseExcelText);//注册文本粘贴事件
this.Controls.Add(tb2);
tb2.Visible = true; tb3 = new CusTextBox();
tb3.Bounds = new Rectangle(tb2.Right + 10, tb1.Location.Y, tb1.Width, tb1.Height);//设置文本框大小及位置
tb3.PaseExcelText += new CusTextBox.EventPaseExcelText(tb_PaseExcelText);//注册文本粘贴事件
this.Controls.Add(tb3);
tb3.Visible = true; tb4 = new CusTextBox();
tb4.Bounds = new Rectangle(tb3.Right + 10, tb1.Location.Y, tb1.Width, tb1.Height);//设置文本框大小及位置
tb4.PaseExcelText += new CusTextBox.EventPaseExcelText(tb_PaseExcelText);//注册文本粘贴事件
this.Controls.Add(tb4);
tb4.Visible = true;
}
//文本框粘贴事件
void tb_PaseExcelText(string str)
{
if (str == null || str == "")
return;
//这里已经写死,必须是复制至少四个Excel单元格的值.
string[] s = str.Split('\t');
tb1.Text = s[0];
tb2.Text = s[1];
tb3.Text = s[2];
tb4.Text = s[3];
}
}
//有粘贴事件的,自定义文本框控件
public class CusTextBox : TextBox
{
//粘贴委托,参数为复制的所有文本
public delegate void EventPaseExcelText(string str);
//粘贴事件
public event EventPaseExcelText PaseExcelText;
//消息处理
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x302)//粘贴消息
{
//当存在粘贴事件时,文本框将不接收粘贴
if (PaseExcelText != null)
{
PaseExcelText(Clipboard.GetText());
return;
}
}
base.WndProc(ref m);
}
}
string[] s = str.Split('\t');
tb1.Text = s[0];
tb2.Text = s[1];
tb3.Text = s[2];
tb4.Text = s[3];
===================================
那如果只有三个的话,他不会出错吧,就只前面三个显示,第四个我们自己输,可以吧?
void tb_PaseExcelText(string str)
{
if (str == null || str == "")
return;
string[] s = str.Split('\t');
if (s.Length > 0)
tb1.Text = s[0];
if (s.Length > 1)
tb2.Text = s[1];
if (s.Length > 2)
tb3.Text = s[2];
if (s.Length > 3)
tb4.Text = s[3];
}
public class CusTextBox : TextBox
{
============================================那如果没有粘贴事件呢?,我不能从工具箱中直接拉文本框过来使用吗
这个是继承自TextBox文本框的自定义控件. 其比文本框本身多了一个粘贴事件.编译通过后你可以在工具箱里找到 可以拖过来用.但是你还需要注册粘贴事件,在里面自己处理.还有.粘贴事件对应的委托EventPaseExcelText(string str);没有传递是哪一个文本框,你要的话自己加了. 呵呵
http://hi.baidu.com/%C3%CFtezi/blog/item/e6bd9622d6dc2aac4723e82f.html
这里面有例子
得到的结果为: "2009-10-26\t全部表格\t全部字段\r\n"
竖向复制的EXCEL
得到的结果为: "2009-10-26\r\n2009-11-9\r\n2009-11-13\r\n2009-11-17\r\n"
自己去分割 该会吧?