[英]How to get a value from a dynamic type variable obtained from the “value” property of Microsoft.Office.Interop.Excel.Range object
I have a problem when i'm trying to obtain the values from a dynamic type variable, which is result of the "value" property of Microsoft.Office.Interop.Excel.Range object. 当我试图从动态类型变量获取值时,我遇到了问题,这是Microsoft.Office.Interop.Excel.Range对象的“value”属性的结果。 This is my scenario:
这是我的情景:
I'm using c#, and i have this values in an excel document: 我正在使用c#,我在excel文档中有这个值:
11010001 123 0 11010002 0 23 11010003 0 120
When i do a copy with control+c command, i need to capture those values and make something with them, and this is the way that de Range object returns the selected values: 当我使用control + c命令执行复制时,我需要捕获这些值并使用它们创建一些内容,这就是de Range对象返回所选值的方式:
//C# Code
//Get the active and open Excel
var excel = Marshal.GetActiveObject("Excel.Application") as Microsoft.Office.Interop.Excel.Application;
//Get the selected rows range
Range range = excel.Selection as Range;
//Get the values of selected rows
dynamic cellValue = range.Value;
In the cellValue variable i have the values in the following format: Code fragment 在cellValue变量中,我有以下格式的值: 代码片段
So, my problem is that i don't know how to get those values by separated and insert them in my database. 所以,我的问题是我不知道如何通过分离获取这些值并将它们插入我的数据库中。
Could you help me whith a suggestion or code example of how to get those values from the dynamic type variable? 你能帮我提一下如何从动态类型变量中获取这些值的建议或代码示例吗?
Regards. 问候。
After so much trying i found the solution when i put it the dynamic variable into the watch window and right there was the way to access to the values contained, that is the following way: 经过这么多尝试后,我找到了解决方案,当我将动态变量放入监视窗口时,正确的方法是访问包含的值,这是以下方式:
dynamic cellValue = range.Value;
int longitud = ((object[,])cellValue).Length;
for (int i = 1; i <= longitud/3; i++)
{
for (int j = 1; j <= 3; j++)
{
object texto = ((object[,])cellValue)[i, j];
}
}
but note that you have to import the Microsoft.CSharp.dll and using Microsoft.CSharp.RuntimeBinder; 但请注意,您必须导入Microsoft.CSharp.dll并使用Microsoft.CSharp.RuntimeBinder; to make possible that work fine.
使工作正常。
Regards. 问候。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.