|
楼主 |
发表于 2015-10-17 15:00
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
自己摸索着解决了这个问题,代码如下(除了Sheet1_Startup、button1_Click两个过程需手工编写外,其他均为VSTO自动生成):
- 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 ExcelWorkbook1
- {
- public partial class Sheet1
- {
- private void Sheet1_Startup(object sender, System.EventArgs e)
- {
- txtResult.Height = 120;
- }
- private void Sheet1_Shutdown(object sender, System.EventArgs e)
- {
- }
- #region VSTO 设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要
- /// 使用代码编辑器修改此方法的内容。
- /// </summary>
- private void InternalStartup()
- {
- this.button1.Click += new System.EventHandler(this.button1_Click);
- this.Startup += new System.EventHandler(this.Sheet1_Startup);
- this.Shutdown += new System.EventHandler(this.Sheet1_Shutdown);
- }
- #endregion
- private void button1_Click(object sender, EventArgs e)
- {
- // 清除textbox中的内容
- txtResult.Clear();
- // 从复选框中获得选择的科目索引
- int subjectIndex = cbxsubjects.SelectedIndex;
- if (subjectIndex == -1)
- {
- MessageBox.Show("请先选择一个科目");
- return;
- }
- // 获得选择的科目名称
- string subjectName = cbxsubjects.SelectedItem.ToString();
- // 获得工作表对象
- Excel.Worksheet worksheet = (Excel.Worksheet)Globals.ThisWorkbook.ActiveSheet;
- for (int row = 2; row < worksheet.UsedRange.Rows.Count + 1; row++)
- {
- Excel.Range rng = (Excel.Range)worksheet.Cells[row, subjectIndex + 2];
- Excel.Range rng1 = (Excel.Range)worksheet.Cells[row, 1];
- if (rng.Value < 60)
- {
- txtResult.Text += rng1.Value + "; ";
- }
- }
- if (txtResult.Text.Length == 0)
- {
- txtResult.Text = subjectName + "没有不及格的同学";
- }
- }
- }
- }
复制代码 |
|