|
本帖最后由 faunus 于 2014-2-28 16:42 编辑
这是刚撸的代码,新鲜热辣。作用嘛就是为了将"数据表"中A列的单位名称去重后放到碗里去(combobox控件)。闲话不讲,直接上代码:
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Xml.Linq;
- using Microsoft.Office.Tools.Excel;
- using Microsoft.VisualStudio.Tools.Applications.Runtime;
- using Excel = Microsoft.Office.Interop.Excel;
- using Office = Microsoft.Office.Core;
- namespace Test
- {
- public partial class Sheet1
- {
- private void Sheet1_Startup(object sender, System.EventArgs e)
- {
- Excel.Worksheet shD = Application.ActiveWorkbook.Worksheets["数据表"];
- Excel.Range rnAll = shD.Range["A2:A" + Convert.ToString(shD.Range["A1"].End[Excel.XlDirection.xlDown].Row)];
- Dictionary<String,Int32> dicName1 = new Dictionary<string,Int32>();
- String valStr;
- foreach (Excel.Range rn in rnAll)
- {
- valStr = Convert.ToString(rn.Value);
- dicName1[valStr] = rn.Row;
- }
- Dictionary<string, Int32>.KeyCollection keyColl = dicName1.Keys;
- foreach (string s in keyColl)
- {
- comboBox1.Items.Add(s);
- }
- }
- private void Sheet1_Shutdown(object sender, System.EventArgs e)
- {
- }
- #region VSTO 设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要
- /// 使用代码编辑器修改此方法的内容。
- /// </summary>
- private void InternalStartup()
- {
- this.Startup += new System.EventHandler(this.Sheet1_Startup);
- this.Shutdown += new System.EventHandler(this.Sheet1_Shutdown);
- }
- #endregion
- public EventHandler comboBox1_SelectedIndexChanged { get; set; }
- public MouseEventHandler comboBox1_MouseClick { get; set; }
- public Excel.DocEvents_ActivateEventHandler Sheet1_ActivateEvent { get; set; }
- public Excel.DocEvents_ChangeEventHandler Sheet1_Change { get; set; }
- }
- }
复制代码 下面是附件,有兴趣的盆友可下载了试一试。
能想到的运行环境(office2010-vs2010),在此谢谢(佛爷、汪老师、西西)
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
评分
-
查看全部评分
|