检查Excel单元格数据类型 [英] Check an Excel Cell Data Type

查看:40
本文介绍了检查Excel单元格数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以确定Excel单元格的数据类型和格式?

我知道有.NumberFormat,但它返回的是格式而不是类型.

我需要知道它是不是客户,那么它应该返回客户,币种应该返回币种,依此类推。

Excel

在幕后,推荐答案以一种特殊的方式存储值(大多数数据类型实际上是双精度的),这使得在没有Excel帮助的情况下很难检测单元格格式。

因此,我建议您使用内置的ExcelCELL函数,而不是自己查询数据类型:

private void button1_Click(object sender, EventArgs e) 
{
    //C1 is a cell I use to evaluate the Format of Cells A1 thru to A7
    using (var rnEvaluate = xlApp.Range["C1:C1"].WithComCleanup())
    {
        for (int i = 1; i < 8; i++)
        {
            rnEvaluate.Resource.Value2 = "=CELL("format",A" + i.ToString() + ")";
            string cellFormat = GetExcelCellFormat(rnEvaluate.Resource.Value2);
            System.Diagnostics.Debug.Write(cellFormat);
        }
    } 
}

private string GetExcelCellFormat(string cellFormat = "G") 
{
    switch (cellFormat.Substring(0, 1))
    {
        case "F" :
            return "Number";
            break;
        case "C":
            return "Currency";
            break;
        case "D":
            return "Date";
            break;
        default :
            return "General";
            break;
    } 
}

ps.WithComCleanup()是因为我正在使用VSTO Contrib

这篇关于检查Excel单元格数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆