Welcome

首页 / 软件开发 / C# / C#进行Visio二次开发的常见问题处理

C#进行Visio二次开发的常见问题处理2011-05-23 www.iqidi.com 伍华聪1. Visio属性值的转换问题

做过Visio开发的人知道,Visio中的属性值也就是Cell.Formula的值通常包含两对双引号的(如""XX""), 如果要将属性的值转换正常的字符串值,那么需要去除双引号。因此从Visio的Cell的Formula值中得到的字符串需要经过下面方法处理一下:

public static string FormulaStringToString(string formula){const string OneQuote = """;const string TwoQuotes = """";string convertedFormula = "";try{convertedFormula = formula;if (convertedFormula.StartsWith(OneQuote) && convertedFormula.EndsWith(OneQuote)){convertedFormula = convertedFormula.Substring(1, (convertedFormula.Length - 2));convertedFormula = convertedFormula.Replace(TwoQuotes, OneQuote);}}catch (Exception err){convertedFormula = "";}return convertedFormula;}
如果是写入到Visio的Cell的Formula中,那么要经过反过程,如下所示:

public static string StringToFormulaForString(string input){const string quote = """;string result = "";if (input == null){return null;}result = input.Replace(quote, (quote + quote));result = quote + result + quote;return result;}
2、获取指定形状指定Cell的值。除了方法1,还有下面一种方法可以获取Cell的Value值。这种方法比使用Formula获取字符串的方式要好,是因为在Visio2007中下拉列表“资产归属”.对应的Cell的Value可能是INDEX(0,Prop.资产归属.Format),但是如果使用下面的方法就可以正常获取到它具体的值了。

public static string GetShapeCellValue(Shape shapeTarget, string strCellType){const string CUST_PROP_PREFIX = "Prop.";string shapeCellValue = string.Empty;if (shapeTarget.get_CellExistsU(CUST_PROP_PREFIX + strCellType, (short)VisExistsFlags.visExistsAnywhere) != 0){shapeCellValue = FormulaStringToString(shapeTarget.get_CellsU(CUST_PROP_PREFIX + strCellType).get_ResultStr(VisUnitCodes.visNoCast));}return shapeCellValue;}