C#Excelのクリップボードを読み込む

4134 ワード

1. Form1.Designer.cs、winformアプリケーションを作成し、form 1にbuttonとrichtext boxを追加します.
namespace WindowsFormsApplication1
{
    partial class Form1
    {
        /// 
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// Clean up any resources being used.
        /// 
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// 
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        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ボタンをクリックすると、次のような効果が得られます.