C# 获取excel中选中的值

 //鼠标所在单元格的值
 public static string GetSheetRangeData()
 {
      Excel.Application xlsApp = (Excel.Application)ExcelDnaUtil.Application;           
      Excel.Range range = xlsApp.ActiveCell;
      string str = string.Empty;
      if (range != null)
             str = range.Value2.ToString();
      xlsApp = null;
      return str;
 }
 //获取选中范围内所有单元格的值
 public static void GetRange()
{
      Excel.Application xlsApp = (Excel.Application)ExcelDnaUtil.Application;
      //获取选中单元格的范围(b的值格式为"$B$4:$B$7")
      var b = ((Excel.Range)xlsApp.Selection).get_Address(Type.Missing, Type.Missing, Excel.XlReferenceStyle.xlA1,
                  Type.Missing, Type.Missing);
      //获取选中范围内所有单元格的值
      var c = ((Excel.Range)xlsApp.Selection).Value2;
      //第一个值
      var d = c[1, 1];
      //一共选中多少个值
      var f = e.Count;
      var g = e.Cells.Count;
      var h = e.Rows.Count;
 }
 //今天加点内容,获取选中范围内数据的每个值
  public static void GetRange()
{
	Excel.Application xlsApp = (Excel.Application)ExcelDnaUtil.Application;
 	Excel.Worksheet xSheet = xlsApp.ActiveSheet;
 	Excel.Range range = xSheet.get_Range("B2:G12");
 	//获取选中数组中的单个值
	 object d = ((object[,])range.Value2).GetValue(1, 1);
	 int row = xSheet.UsedRange.Rows.Count;
 	 int col = xSheet.UsedRange.Columns.Count;
                for (int i = 0; i < row; i++)
                {
                    for (int j = 0; j < col; j++)
                    {
                        **object d = Convert.ToInt16(((object[,])range.Value2).GetValue(i, j));**
                    }
                }
 }

你可能感兴趣的:(笔记,c#,开发语言,后端)