0

我在创建一个类来读取和写入 Excel 文件时遇到问题。我需要从文件中读取数据(只有一列)。然后我需要在字符串输入中搜索方括号 [] 和/或括号 ()。如果该行包含这些,我需要获取其中的信息并用正斜杠 (/) 分隔(拆分)答案。我不断收到关于变量拆分的错误。有人能指出我正确的方向吗?谢谢!

using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Text.RegularExpressions;

namespace DataExcelApp
{
    public class ExcelApp
    {
        private static void Main()
        {
            //make connection to document
            var fileName = string.Format("C:/Users/kbangert/Desktop/Karpel/ChargeLanguage.xlsx", Directory.GetCurrentDirectory());
            var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", fileName);

            //first query to document
            var adapter = new OleDbDataAdapter("SELECT * FROM [ChargeLanguage] WHERE [description] != string.Empty", connectionString);
            var ds = new DataSet();
            adapter.Fill(ds, "descriptions");

            //DataTable data = ds.Tables["descriptions"];
            var data = ds.Tables["descript"].AsEnumerable();

            if (data != null)
            {
                var entry = data.ToString();
                var pattern = @"\[(.*?)\]";
                var matches = Regex.Matches(entry, pattern);

                foreach (Match m in matches)
                {
                    Console.WriteLine(m.Groups[1]);
                }

                string[] words = matches.Split('/');
                foreach (string word in words)
                {
                    Console.WriteLine(word);
                }
            }    
4

1 回答 1

2

在这一行

string[] words = matches.Split('/');

matchesMatch对象的集合。没有Split办法。

你可能想要这样做:

foreach (Match m in matches)
{
    Console.WriteLine(m.Groups[1]);

     string[] words = m.Groups[1].Value.Split('/');
     foreach (string word in words)
        Console.WriteLine(word);
}
于 2013-09-26T13:22:16.790 回答