【问题标题】:Get sheet name and cell reference from Excel cell reference从 Excel 单元格引用中获取工作表名称和单元格引用
【发布时间】:2015-02-09 06:48:54
【问题描述】:

我需要一种智能方法来拆分 Excel 单元格引用,以便获取工作表名称和单元格引用。 Excel 引用一般可以采用以下形式:

  • =Sheet1!$A$1 ---> Sheet1, $A$1
  • ='表 1'!$A$1 ---> 表 1,$A$1
  • ='!Sheet 1'!$A$1 ---> !Sheet 1, $A$1
  • ='&Sheet1'!$A$1 ---> &Sheet1, $A$1
  • ='$Sheet1$'!$A$1 ---> $Sheet1$, $A$1

因为 Excel 允许在工作表名称中包含特殊字符和空格,这使得提取工作表名称和单元格地址变得困难。

有没有更好的方法来做到这一点?正则表达式?我有 0 经验。

注意:我通过命名范围获得单元格引用,因此我坚持我所拥有的。

【问题讨论】:

    标签: c# string excel split


    【解决方案1】:

    正则表达式可以处理这项工作。

    首先,您需要解析简单的工作表名称,例如“Sheet1”。此名称以字母开头,包含除“!”之外的任何字符。

    (\w[^!]*)
    

    其次,您需要解析复杂的工作表名称,用单引号括起来:

    '([^']+)'
    

    第三,你需要联合这两个正则表达式:

    (\w[^!]*)|'([^']+)'
    

    最后,写代码:

    private static ExtractSheetName(string cell)
    {
        var match = Regex.Match(cell, @"(\w[^!]*)|('[^']+)'");
        if (match.Success)
        {
            if (!string.IsNullOrEmpty(match.Groups[1].Value))
                return match.Groups[1].Value;
    
            if (!string.IsNullOrEmpty(match.Groups[2].Value))
                return match.Groups[2].Values;
    
        }
    
        return null;
    }
    
    . . .
    // Using:
    var sheetName = ExptractSheetName(cell);
    string cellRef;
    if (sheetName == null)
        cellRef = cell;
    else
        cellRef = cell.Substring(sheetName.Length + 1);
    

    【讨论】:

    • 我尝试了这个并调用了方法 ExtractSheetName(cell),将单词 cell 替换为“'$Sheet1$'!A1”。我不确定我在期待什么,因为它会输出相同的输入。除非我用错了,否则我也期待单元格引用。这会给出输入的后半部分吗?
    • 哦。对不起,我没有检查代码。 Regex.Match 方法参数必须颠倒。我更正了我的答案。
    【解决方案2】:

    试试这个

    app.config 文件:

     <appSettings>
    
    <add key="connectionstrings" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\16-07-2014\BDLS_Backup\TVM - Employee List-New.xls;Extended Properties='Excel 8.0;HDR=Yes;;IMEX=2'"/>
    </appSettings>
    

    程序.cs:

    string connectionString = ConfigurationSettings.AppSettings["connectionstrings"];
            OleDbConnection oledbConn = new OleDbConnection(connectionString);
            oledbConn.Open();
    
            OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]", oledbConn);
    
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            da.Fill(ds);
            DataTable dt = ds.Tables[0];
    
    foreach (DataRow row in dt.Rows)
    {
    
                       string EmployeeCode = row["EmployeeCode"].ToString();
    }
    

    其中 ["EmployeeCode"] 是 sheet1 中的列名

    【讨论】:

    • 我不确定这与我的问题有什么关系。
    【解决方案3】:

    ?Application.Evaluate("INDIRECT(""Sheet1!A1"")").Address(false, false)

    ?Application.Evaluate("INDIRECT(""Sheet1!A1"")").WorkSheet.Name

    其中ApplicationExcel.Application 的实例

    【讨论】:

    • 正如我的帖子中提到的,我通过命名范围获得这些参考,所以我坚持我所拥有的。字符串操作是我最好的选择
    • 您能否展示一些代码来解释您所说的“通过命名范围获取这些引用,所以我被卡住了”是什么意思?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多