我已經寫了一個程序來從電子表格中提取一些數據,但是我想讓它在單元格中存在時忽略括號和單詞。電子表格包含城市名稱,縣名和稅率。如何解析不包含單詞和/或括號的數據?
對於大多數情況下的城市來說,單元格只包含城市名稱,但有時也會在裏面帶有「城市」一詞的括號。
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