C# 读取Excel的剪贴板

来源:互联网 发布:人工智能行业 编辑:程序博客网 时间:2024/05/21 09:35

1. Form1.Designer.cs, 创建winform应用程序,并在form1中替添加一个button 和一个richtext box:

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 按钮,效果如下:

 

原创粉丝点击