我已经写了一个程序来从电子表格中提取一些数据,但是我想让它在单元格中存在时忽略括号和单词。电子表格包含城市名称,县名和税率。如何解析不包含单词和/或括号的数据?
对于大多数情况下的城市来说,单元格只包含城市名称,但有时也会在里面带有“城市”一词的括号。
Benton (city)
我正在寻找一种解决方案,使我的程序只检索没有括号的城市。
Benton
对于县里的每一条记录都包含县名和县名。
Crawford County
我想只检索县的名称。
Crawford
对于县我知道我可以使用拆分方法,但我想建议为处理这两种情况的最佳方式。脚本的其余部分工作正常。我只需要改进数据。
对于任何想看我如何处理数据的人。下面是代码:
public static List<CityTax> getCityTaxRates(string fileName)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@fileName);
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
List<CityTax> cityTaxList = new List<CityTax>();
Console.WriteLine("City Tax Rates----------------------------------------------------------------");
for (int i = 10; i <= 312; i++)
{
CityTax cityTaxRate = new CityTax();
cityTaxRate.city = xlRange.Cells[i, 2].Value2.ToString();
cityTaxRate.cityRate = Convert.ToDecimal(xlRange.Cells[i, 5].Value2);
cityTaxList.Add(cityTaxRate);
}
return cityTaxList;
}
public static List<CountyTax> getCountyTaxRates(string fileName)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@fileName);
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
List<CountyTax> countyTaxList = new List<CountyTax>();
Console.WriteLine("County Tax Rates----------------------------------------------------------------");
for (int i = 316; i <= 390; i++)
{
CountyTax countyTaxRate = new CountyTax();
countyTaxRate.county = xlRange.Cells[i, 2].Value2.ToString();
countyTaxRate.countyRate = Convert.ToDecimal(xlRange.Cells[i, 5].Value2);
countyTaxList.Add(countyTaxRate);
}
return countyTaxList;
}
public static void testTaxRates(string fileName)
{
List<CityTax> cityTaxList = new List<CityTax>();
cityTaxList = ParseTaxRates.getCityTaxRates(fileName);
foreach (CityTax cityTax in cityTaxList)
{
Console.WriteLine("City: " + cityTax.city);
Console.WriteLine("Rate: " + cityTax.cityRate);
}
List<CountyTax> countyTaxList = new List<CountyTax>();
countyTaxList = ParseTaxRates.getCountyTaxRates(fileName);
foreach (CountyTax countyTax in countyTaxList)
{
Console.WriteLine("City: " + countyTax.county);
Console.WriteLine("Rate: " + countyTax.countyRate);
}
}
如果你想看到我检索数据从电子表格可以从以下网站下载: http://www.arkansas.gov/dfa/excise_tax_v2/st_zip.html
没有理由在我的环路的静态指标是由于电子表格的布局。我愿意接受如何处理这个问题的建议。现在我知道我必须在运行之前始终检查电子表格,以确保行号仍然相同。不必担心这一点真是太好了,但我不知道如何以其他方式处理它。
你有一个像'等等等等city'什么? – DavidG 2014-11-06 16:43:47
如果你的意思是一个名字中有一个空格的城市名称,那么是“Ash Flat”。然后其中一些像“布拉德利(城市)”和“富兰克林(城市)”。尽管没有引号。 – billabrian6 2014-11-06 16:47:24
为什么不从字符串的末尾删除“(城市)”和“县”? – Dialecticus 2014-11-06 17:09:11