简体   繁体   English

如何从从Microsoft.Office.Interop.Excel.Range对象的“value”属性获取的动态类型变量中获取值

[英]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.

相关问题 获取Microsoft.Office.Interop.Excel.Range的所有单元格坐标 - Get all cells coordinates for a Microsoft.Office.Interop.Excel.Range 如何使用Moq模拟Microsoft.Office.Interop.Excel.Range? - How to mock Microsoft.Office.Interop.Excel.Range with Moq? 无法将类型“对象”隐式转换为“ Microsoft.Office.Interop.Excel.Range” - Cannot implicitly convert type 'object' to 'Microsoft.Office.Interop.Excel.Range' 使用旧版本 Excel (2003) 的 Microsoft.Office.Interop.Excel.Range 对象获取索引超出范围异常 - Get Index out of range exception using Microsoft.Office.Interop.Excel.Range object for old version of Excel (2003) 使用Mock将属性Row设置为Mocked Microsoft.Office.Interop.Excel.Range - Setting property Row to Mocked Microsoft.Office.Interop.Excel.Range with Mock 无法将透明代理转换为类型“Microsoft.Office.Interop.Excel.Range” - Unable to cast transparent proxy to type 'Microsoft.Office.Interop.Excel.Range' Microsoft.Office.Interop.Excel.Range的Type.GetProperties返回空数组 - Type.GetProperties of Microsoft.Office.Interop.Excel.Range returns empty array &#39;Microsoft.Office.Interop.Excel.Range&#39;不包含&#39;get_Default&#39;的定义 - 'Microsoft.Office.Interop.Excel.Range' does not contain a definition for 'get_Default' C#Excel插件-编译警告-类型库导出器找不到Microsoft.Office.Interop.Excel.Range的类型库 - C# Excel Addon - Compilation warning - Type library exporter could not find the type library for Microsoft.Office.Interop.Excel.Range C#:Microsoft.Office.Interop.Excel.Range的ID字段不会与Excel表一起保留 - C#: ID-field of Microsoft.Office.Interop.Excel.Range is not persisted with an Excel-sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM