我在创建一个读取和写入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);
}
}
答案 0 :(得分:2)
在这一行
string[] words = matches.Split('/');
matches
是Match
个对象的集合。那方面没有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);
}