Last active
June 27, 2018 06:14
-
-
Save NNNIC/6e31d983709574e1e44d2b554c2d3947 to your computer and use it in GitHub Desktop.
Excel OLE Object Value Set/Get
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
using System.Runtime.InteropServices; | |
using Microsoft.Office.Interop.Excel; | |
using Microsoft.VisualBasic; | |
using Excel = Microsoft.Office.Interop.Excel; | |
public class Excel_Ole_setget | |
{ | |
public Worksheet m_sheet; | |
private Excel.OLEObject GetObj(string name) | |
{ | |
var sheet = m_sheet; | |
var objs = (Excel.OLEObjects)sheet.OLEObjects(); | |
foreach(var i in objs) | |
{ | |
var o = (Excel.OLEObject)i; | |
if (o.Name == name) | |
{ | |
return o; | |
} | |
} | |
return null; | |
} | |
public string GetText(string name) | |
{ | |
try { | |
var o = GetObj(name); | |
var type = o.Object.GetType(); | |
string val = (string)type.InvokeMember("Text", System.Reflection.BindingFlags.GetProperty, null, o.Object, null).ToString(); | |
return val; | |
} catch { | |
return null; | |
} | |
} | |
public void SetText(string name, string val) | |
{ | |
try { | |
var o = GetObj(name); | |
var type = o.Object.GetType(); | |
type.InvokeMember("Text", System.Reflection.BindingFlags.SetProperty,null, o.Object, new object[] {val}); | |
} catch { | |
} | |
} | |
public bool GetCheckboxValue(string name) | |
{ | |
try { | |
var o = GetObj(name); | |
var type = o.Object.GetType(); | |
string val = (string)type.InvokeMember("Value", System.Reflection.BindingFlags.GetProperty, null, o.Object, null).ToString(); | |
return val == "True"; | |
} catch { | |
return false; | |
} | |
} | |
public void SetCheckboxValue(string name, bool b) | |
{ | |
try { | |
var o = GetObj(name); | |
var type = o.Object.GetType(); | |
type.InvokeMember("Value", System.Reflection.BindingFlags.SetProperty,null, o.Object, new object[] {b}); | |
} catch { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed!