向指定文本框写入数据,对每个WorkSheet操作

来源:互联网 发布:知乎中药学读后感 编辑:程序博客网 时间:2024/05/17 01:39

/// <summary>
  /// 向指定文本框写入数据,对每个WorkSheet操作
  /// </summary>
  /// <param name="textboxName">文本框名称</param>
  /// <param name="text">要写入的文本</param>
  public void SetTextBox(string textboxName,string text)
  {
   for(int i=1;i<=this.WorkSheetCount;i++)
   {
    workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);


    try
    {
     textBox = (Excel.TextBox)workSheet.TextBoxes(textboxName);
     textBox.Text = text;
    }
    catch
    {
     this.KillExcelProcess();
     throw new Exception("不存在ID为\"" + textboxName + "\"的文本框!");
    }
   }
  }

  /// <summary>
  /// 向指定文本框写入数据,对指定WorkSheet操作
  /// </summary>
  /// <param name="sheetIndex">工作表索引</param>
  /// <param name="textboxName">文本框名称</param>
  /// <param name="text">要写入的文本</param>
  public void SetTextBox(int sheetIndex,string textboxName,string text)
  {
   workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);

   try
   {
    textBox = (Excel.TextBox)workSheet.TextBoxes(textboxName);
    textBox.Text = text;
   }
   catch
   {
    this.KillExcelProcess();
    throw new Exception("不存在ID为\"" + textboxName + "\"的文本框!");
   }
  }

  /// <summary>
  /// 向文本框写入数据,对每个WorkSheet操作
  /// </summary>
  /// <param name="ht">Hashtable的键值对保存文本框的ID和数据</param>
  public void SetTextBoxes(Hashtable ht)
  {
   if(ht.Count == 0) return;

   for(int i=1;i<=this.WorkSheetCount;i++)
   {
    workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);

    foreach(DictionaryEntry dic in ht)
    {
     try
     {
      textBox = (Excel.TextBox)workSheet.TextBoxes(dic.Key);
      textBox.Text = dic.Value.ToString();
     }
     catch
     {
      this.KillExcelProcess();
      throw new Exception("不存在ID为\"" + dic.Key.ToString() + "\"的文本框!");
     }
    }
   }
  }

  /// <summary>
  /// 向文本框写入数据,对指定WorkSheet操作
  /// </summary>
  /// <param name="ht">Hashtable的键值对保存文本框的ID和数据</param>
  public void SetTextBoxes(int sheetIndex,Hashtable ht)
  {
   if(ht.Count == 0) return;

   if(sheetIndex > this.WorkSheetCount)
   {
    this.KillExcelProcess();
    throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
   }

   workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);

   foreach(DictionaryEntry dic in ht)
   {
    try
    {
     textBox = (Excel.TextBox)workSheet.TextBoxes(dic.Key);
     textBox.Text = dic.Value.ToString();
    }
    catch
    {
     this.KillExcelProcess();
     throw new Exception("不存在ID为\"" + dic.Key.ToString() + "\"的文本框!");
    }
   }
  }