C# 读取Excel的剪贴板
2012-03-26 16:38
225 查看
1. Form1.Designer.cs, 创建winform应用程序,并在form1中替添加一个button 和一个richtext box:
2. 给button 加上click 事件
3. 启动应用程序
4. 打开一个Excel, 在其中输入一些测试数据,选中测试数据,按Ctrl +C
5. 点击read 按钮,效果如下:
namespace WindowsFormsApplication1 { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(12, 12); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "read"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // richTextBox1 // this.richTextBox1.Location = new System.Drawing.Point(12, 60); this.richTextBox1.Name = "richTextBox1"; this.richTextBox1.Size = new System.Drawing.Size(268, 201); this.richTextBox1.TabIndex = 1; this.richTextBox1.Text = ""; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.richTextBox1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion private System.Windows.Forms.Button button1; private System.Windows.Forms.RichTextBox richTextBox1; } }
2. 给button 加上click 事件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.richTextBox1.Text = ""; try { var fmt_csv = System.Windows.Forms.DataFormats.CommaSeparatedValue; //read the CSV var dataobject = System.Windows.Forms.Clipboard.GetDataObject(); var stream = (System.IO.Stream)dataobject.GetData(fmt_csv); var enc = System.Text.Encoding.GetEncoding(1252); var reader = new System.IO.StreamReader(stream, enc); string data_csv = reader.ReadToEnd(); this.richTextBox1.AppendText(data_csv); //read the Unicode String string data_string = System.Windows.Forms.Clipboard.GetText(); this.richTextBox1.AppendText(data_string); } catch(Exception ex) { } } } }
3. 启动应用程序
4. 打开一个Excel, 在其中输入一些测试数据,选中测试数据,按Ctrl +C
5. 点击read 按钮,效果如下:
相关文章推荐
- C#读取Excel并换算生成新的Excel
- [C#新手入门 九] C#读取Excel表中的数据时,有的数据读取不到
- C#读取EXCEL数据
- 用c#读取Excel的方法
- C#读取Excel到DataSet的方法
- C#读取Excel中的数据的类
- C#读取Excel的三种方式以及比较
- OLE Automation (C#读取EXCEL)
- C#读取Excel数据动态生成对象并进行序列化
- 关于在C#中读取导入的Excel一些值为空的解决方法
- C#读取Excel文件时提示:无法指出的错误
- ASP.NET(C#)读取Excel的文件内容
- C#中创建、打开、读取、写入、保存Excel的一般性代码
- C# 对Excel操作时,单元格值的读取
- C#中Excel文件的读取的两种方法:Excel类和OleDb
- C# 读取Excel
- C#读取Excel文档
- C# 读取EXCEL文件的三种经典方法
- C#读取Excel中合并单元格解决办法
- C#读取Excel文件数据