向单元格写入数据,对当前WorkSheet操作

这个代码段展示了如何向Excel单元格写入数据。提供了四个方法,包括对当前工作表、指定工作表以及对每个工作表进行操作。方法通过行索引、列索引和文本值作为参数,使用`Cells`属性设置单元格内容,并处理可能的异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/// <summary>
  /// 向单元格写入数据,对当前WorkSheet操作
  /// </summary>
  /// <param name="rowIndex">行索引</param>
  /// <param name="columnIndex">列索引</param>
  /// <param name="text">要写入的文本值</param>
  public void SetCells(int rowIndex,int columnIndex,string text)
  {
   try
   {
    workSheet.Cells[rowIndex,columnIndex] = text;
   }
   catch
   {
    this.KillExcelProcess();
    throw new Exception("向单元格[" + rowIndex + "," + columnIndex + "]写数据出错!");
   }
  }

  /// <summary>
  /// 向单元格写入数据,对指定WorkSheet操作
  /// </summary>
  /// <param name="sheetIndex">工作表索引</param>
  /// <param name="rowIndex">行索引</param>
  /// <param name="columnIndex">列索引</param>
  /// <param name="text">要写入的文本值</param>
  public void SetCells(int sheetIndex,int rowIndex,int columnIndex,string text)
  {
   try
   {
    this.ChangeCurrentWorkSheet(sheetIndex); //改变当前工作表为指定工作表
    workSheet.Cells[rowIndex,columnIndex] = text;
   }
   catch
   {
    this.KillExcelProcess();
    throw new Exception("向单元格[" + rowIndex + "," + columnIndex + "]写数据出错!");
   }
  }

  /// <summary>
  /// 向单元格写入数据,对每个WorkSheet操作
  /// </summary>
  /// <param name="ht">Hashtable的键值对保存单元格的位置索引(行索引和列索引用“,”隔开)和数据</param>
  public void SetCells(Hashtable ht)
  {
   int rowIndex;
   int columnIndex;
   string position;

   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
     {
      position = dic.Key.ToString();
      rowIndex = Convert.ToInt32(position.Split(',')[0]);
      columnIndex = Convert.ToInt32(position.Split(',')[1]);
      
      workSheet.Cells[rowIndex,columnIndex] = dic.Value;
     }
     catch
     {
      this.KillExcelProcess();
      throw new Exception("向单元格[" + dic.Key + "]写数据出错!");
     }
    }
   }
  }

  /// <summary>
  /// 向单元格写入数据,对指定WorkSheet操作
  /// </summary>
  /// <param name="ht">Hashtable的键值对保存单元格的位置索引(行索引和列索引用“,”隔开)和数据</param>
  public void SetCells(int sheetIndex,Hashtable ht)
  {
   int rowIndex;
   int columnIndex;
   string position;

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

   if(ht.Count == 0) return;

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

   foreach(DictionaryEntry dic in ht)
   {
    try
    {
     position = dic.Key.ToString();
     rowIndex = Convert.ToInt32(position.Split(',')[0]);
     columnIndex = Convert.ToInt32(position.Split(',')[1]);
      
     workSheet.Cells[rowIndex,columnIndex] = dic.Value;
    }
    catch
    {
     this.KillExcelProcess();
     throw new Exception("向单元格[" + dic.Key + "]写数据出错!");
    }
   }
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值