public virtual IEnumerable <Transaction> ExtractTransactions(OfxDocument ofxDocument) => ofxDocument .Bank .TransactionWrapper .StmTrs .TranList .Transactions .Select(x => x.ToTransaction());
protected override async void OnAppearing() { base.OnAppearing(); labelTitulo.Text = "Mês/Ano: " + MesAno; //se não tiver selecionado o arquivo //verifica se tem dados no banco //se tiver carrega os dados na tela if (SelecionouOFX == false) { CarregarDadosBanco(); } else { //se tiver selecionado o arquivo //apaga os dados no banco para o mês selecionado //persiste os dados no banco //carrega os dados do arquivo na tela try { Stream stream = new MemoryStream(filedata.DataArray); documentOFX = new OfxDocument(stream); await App.ExtratoBancoDB.DeleteAsyncFilter(Filtro); foreach (Transaction trans in documentOFX.Transactions) { // Novo objeto para inserção var extratoBanco = new ExtratoBanco(); // Informa os dados para o objeto extratoBanco.MesAno = MesAno; extratoBanco.DataTransacao = DateTime.Parse(trans.DatePosted.Substring(0, 4) + '-' + trans.DatePosted.Substring(4, 2) + '-' + trans.DatePosted.Substring(6, 2)); extratoBanco.Valor = Decimal.Parse(trans.TransAmount, style, provider); extratoBanco.IdTransacao = trans.FITID; extratoBanco.CheckNum = trans.CheckNum; extratoBanco.NumeroReferencia = trans.FITID; extratoBanco.Historico = trans.Memo; extratoBanco.Conciliado = trans.TransAmount.Substring(0, 1); // Persiste o objeto no banco de dados await App.ExtratoBancoDB.SaveItemAsync(extratoBanco); } // Carrega os dados do banco na tela CarregarDadosBanco(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } }
static void Main(string[] args) { CultureInfo provider = CultureInfo.InvariantCulture; if (args.Length == 0) { Console.WriteLine("Please enter name and path of OFX file. Example ofx2csv c:\\myaccount.ofx"); return; } FileInfo d = new FileInfo(args[0]); OfxDocument document = new OfxDocument(new FileStream(args[0], FileMode.Open)); using (System.IO.StreamWriter fs = new System.IO.StreamWriter(d.DirectoryName + "\\" + d.Name.Replace(".ofx", ".csv"))) { fs.WriteLine("Date,Description,Original Description,Amount,Transaction"); foreach (Transaction trans in document.Transactions) { Console.WriteLine(trans.Name + trans.TransType + trans.TransAmount); string format = "yyyyMMddHHmmss"; string formattedDate = ""; try { DateTime result = DateTime.ParseExact(trans.DatePosted, format, provider); Console.WriteLine("{0} converts to {1}.", trans.DatePosted, result.ToString()); formattedDate = result.ToString(); } catch (FormatException) { Console.WriteLine("{0} is not in the correct format.", trans.DatePosted); } fs.WriteLine(formattedDate + "," + trans.Name + "," + trans.Memo + "," + trans.TransAmount + "," + trans.TransType); } } int i = 0; }
public static OfxDocument Parse(Stream stream) { OfxDocument ofxDocument = new OfxDocument(); using (StreamReader reader = new StreamReader(stream)) { ofxDocument.Transactions = new List <OfxTransaction>(); bool inHeader = true; while (!reader.EndOfStream) { string temp = reader.ReadLine(); if (inHeader) { if (temp.ToLower().Contains("<ofx>")) { inHeader = false; } #region Read Header else { string[] tempSplit = temp.Split(":".ToCharArray()); switch (tempSplit[0].ToLower()) { case "ofxheader": ofxDocument.OfxHeader = tempSplit[1]; break; case "data": ofxDocument.Data = tempSplit[1]; break; case "version": ofxDocument.Version = tempSplit[1]; break; case "security": ofxDocument.Security = tempSplit[1]; break; case "encoding": ofxDocument.Encoding = tempSplit[1]; break; case "charset": ofxDocument.Charset = tempSplit[1]; break; case "compression": ofxDocument.Compression = tempSplit[1]; break; case "oldfileuid": ofxDocument.OldFileUID = tempSplit[1]; break; case "newfileuid": ofxDocument.NewFileUID = tempSplit[1]; break; } } #endregion } if (!inHeader) { string restOfFile = temp + reader.ReadToEnd(); restOfFile = Regex.Replace(restOfFile, Environment.NewLine, ""); restOfFile = Regex.Replace(restOfFile, "\n", ""); restOfFile = Regex.Replace(restOfFile, "\t", ""); ofxDocument.BankID = Regex.Match(restOfFile, @"(?<=bankid>).+?(?=<)", RegexOptions.Multiline | RegexOptions.IgnoreCase).Value; ofxDocument.AccountID = Regex.Match(restOfFile, @"(?<=acctid>).+?(?=<)", RegexOptions.Multiline | RegexOptions.IgnoreCase).Value; ofxDocument.AccountType = Regex.Match(restOfFile, @"(?<=accttype>).+?(?=<)", RegexOptions.Multiline | RegexOptions.IgnoreCase).Value; ofxDocument.StartDate = Regex.Match(restOfFile, @"(?<=dtstart>).+?(?=<)", RegexOptions.Multiline | RegexOptions.IgnoreCase).Value; ofxDocument.EndDate = Regex.Match(restOfFile, @"(?<=dtend>).+?(?=<)", RegexOptions.Multiline | RegexOptions.IgnoreCase).Value; string banktranlist = Regex.Match(restOfFile, @"(?<=<banktranlist>).+(?=<\/banktranlist>)", RegexOptions.Multiline | RegexOptions.IgnoreCase).Value; MatchCollection m = Regex.Matches(banktranlist, @"<stmttrn>.+?<\/stmttrn>", RegexOptions.Multiline | RegexOptions.IgnoreCase); foreach (Match match in m) { foreach (Capture capture in match.Captures) { OfxTransaction trans = new OfxTransaction(); if (Regex.Match(capture.Value, @"(?<=<trntype>).+?(?=<)", RegexOptions.Multiline | RegexOptions.IgnoreCase).Value.ToLower().Equals("credit")) { trans.TransType = TransType.Credit; } if (Regex.Match(capture.Value, @"(?<=<trntype>).+?(?=<)", RegexOptions.Multiline | RegexOptions.IgnoreCase).Value.ToLower().Equals("debit")) { trans.TransType = TransType.Debit; } var datePosted = Regex.Match(capture.Value, @"(?<=<dtposted>).+?(?=<)", RegexOptions.Multiline | RegexOptions.IgnoreCase).Value; trans.DatePosted = DateTime.ParseExact(datePosted.Substring(0, 8), "yyyyMMdd", null); var transAmount = Regex.Match(capture.Value, @"(?<=<trnamt>).+?(?=<)", RegexOptions.Multiline | RegexOptions.IgnoreCase).Value; trans.TransAmount = decimal.Parse(transAmount); trans.Memo = Regex.Match(capture.Value, @"(?<=<memo>).+?(?=<)", RegexOptions.Multiline | RegexOptions.IgnoreCase).Value; ofxDocument.Transactions.Add(trans); } } } } } return(ofxDocument); }