当前位置:编程学习 > C#/ASP.NET >>

获取Excel中选中区域的值,100分.

开发环境 visual studio 2008 ,excel 2007,C#
随机在Excel中选择一个区域(反蓝),如何获得选中区域的值?
--------------------编程问答-------------------- Excel.Excel.Range rng = xSheet.get_Range("C2", Missing.Value);
rng.Value2  --------------------编程问答-------------------- 1.获取单元格数据:string temp=((Range)worksheet.Cells[1,2]).Text;
2.获取选中区域数据

//Get a range of data.
range = objSheet.get_Range("A1", "E5"); //换成选定的区域 .select

//Retrieve the data from the range.
Object[,] saRet;
saRet = (System.Object[,])range.get_Value( Missing.Value );

//Determine the dimensions of the array.
long iRows;
long iCols;
iRows = saRet.GetUpperBound(0);
iCols = saRet.GetUpperBound(1);

//Build a string that contains the data of the array.
String valueString;
valueString = "Array Data\n";

 for (long rowCounter = 1; rowCounter <= iRows; rowCounter++)
   {
   for (long colCounter = 1; colCounter <= iCols; colCounter++)
     {

         //Write the next value into the string.
         valueString = String.Concat(valueString,
        saRet[rowCounter, colCounter].ToString() + ", ");
    }

  //Write in a new line.
  valueString = String.Concat(valueString, "\n");
}

//Report the value of the array.
MessageBox.Show(valueString, "Array Values");

详细可参考:获取某区域中的数据

--------------------编程问答-------------------- ActiveCell.Value --------------------编程问答-------------------- excel 2007 操作 --------------------编程问答-------------------- 1楼:这"C2"是硬编码,我要获得我当前选择的范围啊.
2楼:range = objSheet.get_Range("A1", "E5"); 这里也是硬编码选范围.知道题目中"随机"的含义吗?
3楼:只是一个值,读取不了一个范围.

强调:动态选择范围的值 --------------------编程问答--------------------
引用 5 楼 mozongying2 的回复:
"随机"的含义?


随机选区域,是人选的,还是机器选的? 随机取值的现实意义是什么呢? --------------------编程问答-------------------- 动态选择范围的值。就要在EXCEL中获取使用宏 --------------------编程问答-------------------- 6楼:就是人手随机在Excel里选,再在代码中人手选的范围内容.
7楼:能不能讲详细点啊? --------------------编程问答-------------------- 先得到当前选中的单元的行的索引,再得到当前列主键的值,然后就可以得到随机选中的区域的值了 --------------------编程问答-------------------- 没有高手吗? --------------------编程问答-------------------- 用VBA做个插件 --------------------编程问答-------------------- --------------------编程问答-------------------- 看看 --------------------编程问答-------------------- 你要的“区域的值”,指的是什么,是把区域里每一个格的值都取出来?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,