简体   繁体   English

如何自动格式化包含字符串和数字的 Excel 单元格?

[英]How do I autoformat excel cells that contains both strings and numbers?

I have written an RTD server that I wrap in a UDF - both in C# which looks like this:我写了一个 RTD 服务器,我用 UDF 包装 - 都在 C# 中,如下所示:

public object MyUDF(string ItemID, string TopicName)
{
    return _xlApp.WorksheetFunction.RTD("my_rtdserver", null, TopicID, TopicName);
}

The UDF delivers string, date and numeric data but it all seems to be formatted as strings which doesn't allow me to work with numeric data with excel formulas such as SUM(A:A) . UDF 提供字符串、日期和数字数据,但它们似乎都被格式化为字符串,这不允许我使用诸如SUM(A:A)类的 Excel 公式处理数字数据。

I have tried to format the numeric cells with "Number" format but this doesn't make any difference.我试图用“数字”格式格式化数字单元格,但这没有任何区别。

Anyone have this problem?有人有这个问题吗?

Cheers干杯

I have found the following to work:我发现以下方法有效:

public object MyUDF(string TopicID, string TopicName)
{
    var value = _xlApp.WorksheetFunction.RTD("my_rtdserver", null, TopicID, TopicName);
    double num;
    if (!double.TryParse(value, out num))
        return value;
    return num;
}

See: Checking if an object is a number in C#请参阅: 检查对象是否是 C# 中的数字

Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM