public static SplitCsv ( string line, bool isTsv ) : string[] | ||
line | string | CSV行 |
isTsv | bool | 区切りがタブの場合は true, カンマの場合は false |
return | string[] |
/// <summary> /// アカウント情報を読み込む /// </summary> /// <param name="line">アカウント情報行</param> /// <returns></returns> private bool readAccountInfo(string line, Hashtable nameHash) { string[] columns = CsvUtil.SplitCsv(line, false); if (columns.Length < 3) { return(false); } if (columns.Length >= 5 && columns[4] != "JPY") { return(false); } if (!isCreditCard) { // 銀行口座 string bankName = columns[0]; string branchName = columns[1]; string accountId = columns[2]; if (columns.Length >= 4) { try { balance = int.Parse(columns[3]); hasBalance = true; } catch { hasBalance = false; } } mIdent = bankName; mBankId = bankName; mBranchId = branchName; // getDummyId(branchName).ToString(); mAccountId = accountId; } else { // クレジットカード string cardName = columns[0]; try { // 借入額 balance = -int.Parse(columns[2]); hasBalance = true; } catch { hasBalance = false; } // 末尾の 'カード' という文字を抜く cardName = "CARD_" + Regex.Replace(cardName, @"カード$", ""); // 2カラム目は空の模様 //string balance = columns[2]; mIdent = ""; mBankId = ""; mBranchId = ""; // 重複しないよう、連番を振る int counter; if (!nameHash.ContainsKey(cardName)) { counter = 1; } else { counter = (int)nameHash[cardName]; } mAccountId = cardName + counter.ToString(); nameHash[cardName] = counter + 1; } return(true); }
/// <summary> /// 取引行の解析 /// </summary> /// <param name="line">取引行</param> /// <returns>成功フラグ</returns> /// public bool readTransaction(string line) { string[] columns = CsvUtil.SplitCsv(line, false); if (columns.Length < 8) { return(false); } Transaction transaction = new Transaction(); // 日付の処理 string[] ary = columns[0].Split(new char[] { '/' }); try { if (ary.Length == 3) { transaction.date = new DateTime(int.Parse(ary[0]), int.Parse(ary[1]), int.Parse(ary[2]), 0, 0, 0); } else if (ary.Length == 2) { DateTime now = DateTime.Now; int n1 = int.Parse(ary[0]); int n2 = int.Parse(ary[1]); if (n1 >= 2000) { // 年と月のみ: 日は1日とする transaction.date = new DateTime(n1, n2, 1, 0, 0, 0); } else { // 月と日のみ。年は推定する。 int mm = n1; int dd = n2; DateTime d = new DateTime(now.Year, mm, dd, 0, 0, 0); // 同一年として、日付が6ヶ月以上先の場合、昨年とみなす。 // 逆に6ヶ月以上前の場合、翌年とみなす。 TimeSpan ts = d - now; if (ts.TotalDays > 366 / 2) { d = new DateTime(now.Year - 1, mm, dd, 0, 0, 0); } else if (ts.TotalDays < -366 / 2) { d = new DateTime(now.Year + 1, mm, dd, 0, 0, 0); } transaction.date = d; } } else { return(false); } } catch { // 日付が範囲外 (ArgumentRangeOutOfException など) return(false); } // 摘要 transaction.desc = columns[1]; // 入金額/出金額 try { transaction.value = int.Parse(columns[2]); } catch { try { transaction.value = -int.Parse(columns[4]); } catch { return(false); } } // 残高 try { transaction.balance = int.Parse(columns[6]); } catch { // Note: 残高は入っていない場合もある transaction.balance = 0; } mTransactions.Add(transaction); return(true); }
// CSV のフィールド分割 private string[] SplitCsv(string line) { return(CsvUtil.SplitCsv(line, mRule.isTSV)); }