[Office 2010 development series] [C #] let your Winform can instantly synchronize entered into an Excel spreadsheet! ─ ABC article

[Office 2010 development series] let your Winform can instantly synchronize entered into an Excel spreadsheet! ─ ABC article


This article describes how to open a custom in through the Ribbon WinForm, then directly and synchronized to the text input to the Excel format to save in.

Of course, this paper simply introduces the basic text, but also explain its practices, expansion and Debug As for the other issues not within the scope of this article to explore !!!

※ Picture Preview:

<< This was the example illustrated button in the Ribbon. >>

image

<< C2 saving grid is already entered, or in the situation and the Focus. And B5 is immediately stored input cell, the result is displayed in synchronization B5 saved cell.

image

※ interface, the program practice:

1. Please first create a project and a Ribbon

2. Then, in the Ribbon added oneimage  button (Button).

3. and please the button in the Click event by adding the following procedures.

   1:          private void btnFastKeyIn_Click(object sender, RibbonControlEventArgs e)
   2:          {
   3:              FrmFastKeyIn FFKI = new FrmFastKeyIn();
   4:              FFKI.ShowDialog();
   5:              FFKI.Close();
   6:              FFKI.Dispose();
   7:          }


4. 并请新增一个 WinForm (本命名为 FrmFastKeyIn.cs ),并设计如下:

image

5. 该 WinForm 的对象所属事件中,请分别加入下列程序 [ 即该 Winform 的所有程序 ]:

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.ComponentModel;
   4:  using System.Data;
   5:  using System.Drawing;
   6:  using System.Linq;
   7:  using System.Text;
   8:  using System.Windows.Forms;
   9:   
  10:  using Gma.UserActivityMonitor;
  11:  using Microsoft.Office.Core;
  12:  using Microsoft.Office.Interop.Excel;
  13:  using Microsoft.Office.Tools.Excel;
  14:   
  15:  namespace ExcelWorkbook1
  16:  {
  17:      public partial class FrmFastKeyIn : Form
  18:      {
  19:          public FrmFastKeyIn()
  20:          {
  21:              InitializeComponent();
  22:          }
  23:   
  24:          string get_nowCR = null;
  25:          bool change_Row = false;
  26:          StringBuilder sb_Old = new StringBuilder();
  27:          int sb_old_length = 0;
  28:   
  29:          private void richTextBox1_TextChanged(object sender, EventArgs e)
  30:          {
  31:              // 当按下或放开键盘时进行处理
  32:              //HookManager.KeyDown +=new KeyEventHandler(HookManager_KeyDown);
  33:              //HookManager.KeyUp += new KeyEventHandler(HookManager_KeyUp);
  34:   
  35:              // 此部分是针对字符串处理 (原本还含有当按下 Ctrl+Tab 时的相关字符串处理)
  36:              get_nowCR = T_Column + T_Row;
  37:              if(change_Row == true)
  38:              {
  39:                  sb_Old.Remove(0, sb_Old.Length);
  40:                  sb_old_length = 0;
  41:                  change_Row = false;
  42:              }
  43:              else if (change_Row == false)
  44:              {
  45:   
  46:                  sb_Old.Insert(sb_old_length, ((RichTextBox)sender).Text.Substring(sb_old_length, 1));
  47:                  sb_old_length += 1;
  48:              }
  49:   
  50:              // ※ 核心... 就是把值写到 Excel 的 Sheet1 中所指定的保存格中
  51:              Globals.Sheet1.Range[get_nowCR, Type.Missing].Value2 = sb_Old.ToString();
  52:   
  53:          }
  54:   
  55:          //void HookManager_KeyDown(object sender, KeyEventArgs e)
  56:          //{
  57:          //    ProcessMutilKey(e.KeyCode, true);
  58:          //}
  59:   
  60:          //void HookManager_KeyUp(object sender, KeyEventArgs e)
  61:          //{
  62:          //    ProcessMutilKey(e.KeyCode, false);
  63:          //}
  64:   
  65:          //// 记录 Tab 及 Ctrl 是否被同时 KeyDown 之变量及处理
  66:          //bool IsTabDown = false, IsCtrlDown = false;
  67:          //private void ProcessMutilKey(Keys MutilKey, bool DownOrUp)
  68:          //{
  69:          //    if (MutilKey == Keys.Tab)
  70:          //    {
  71:          //        IsTabDown = DownOrUp;
  72:          //    }
  73:          //    else if (MutilKey == Keys.LControlKey)
  74:          //    {
  75:          //        IsCtrlDown = DownOrUp;
  76:          //    }
  77:   
  78:   
  79:          //    const int t_RowAdd = 1;
  80:          //    int New_Row = 0;
  81:          //    if (IsCtrlDown == true && IsTabDown == true)
  82:          //    {
  83:          //        New_Row = (int.Parse(T_Row)) + t_RowAdd;
  84:          //        T_Row = New_Row.ToString();
  85:          //        change_Row = true;
  86:          //    }
  87:          //}
  88:   
  89:          // 窗口启动时
  90:          private void FrmFastKeyIn_Load(object sender, EventArgs e)
  91:          {
  92:              textBox1.Select();
  93:              textBox1.Focus();
  94:          }
  95:   
  96:          // 取得 栏
  97:          string T_Column= null;
  98:          private void textBox1_Leave(object sender, EventArgs e)
  99:          {
 100:              T_Column = textBox1.Text;
 101:          }
 102:   
 103:          // 取得 列
 104:          string T_Row = null;
 105:          private void textBox2_Leave(object sender, EventArgs e)
 106:          {
 107:              T_Row = textBox2.Text;
 108:          }
 109:      }
 110:  }


※ 结语:

本例原本是设计除可直接输入外,若当 User 按下 Ctrl + Tab 时,会自动跳列 (栏)的设计,但怕过于复杂,而没办法把重点及本篇主旨核心充份说明,故在上述第5步骤的程序是就把另一部分(即 Ctrl+Tab 键的处理) 先暂时标除,仅留下本次所提述的内容。

Of course, the program up and running are generally considered OK !!! If there are other Function, please also self-feeding Hello ~

The content I just mentioned (Ctrl + Tab key), but also refer to: [WinForm, C #] global monitoring mouse (Mouse) and keyboard (Keyboard) Yiwen event.

Original: Big Box  [Office 2010 development series] [C #] let your Winform can instantly synchronize entered into an Excel spreadsheet! ─ ABC article


Guess you like

Origin www.cnblogs.com/petewell/p/11495538.html