/// <summary> /// Percorre tabela em excel referente a carga /// </summary> /// <typeparam name="T"></typeparam> /// <param name="path"></param> /// <param name="obj"></param> /// <returns></returns> public void LePlanilhaExcelEInsereDados(string path, string nomePlanilha = "Sheet1") { string nomeArquivo = Path.GetFileName(path); ENLog.MapaArquivos mapArq = new ENLog.MapaArquivos(nomeArquivo, ENLog.TipoArquivo.ACSOPRGCR, path, 0); Log.AtualizaArquivo<ENLog.MapaArquivos>(mapArq, ENLog.StatusProcessamentoArquivo.NaoProcessado, mapArq.Arquivo, "Inicia processamento de arquivo"); int idArquivo = mapArq.IdArquivo; //using (OleDbConnection conexao = new OleDbConnection(String.Format(ConfigurationManager.AppSettings["ConexaoExcel"], path))) using (OleDbConnection conexao = new OleDbConnection(String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1';", path))) { conexao.Open(); Log.AtualizaArquivo<ENLog.MapaArquivos>(mapArq, ENLog.StatusProcessamentoArquivo.EmProcessamento, mapArq.Arquivo, "Em processamento de arquivo"); ENLog.TipoLog tpLog = ENLog.TipoLog.Informação; using (OleDbDataAdapter adapter = new OleDbDataAdapter(String.Format("SELECT * FROM [{0}]", String.Format("{0}$", nomePlanilha)), conexao)) { DataSet ds = new DataSet(); int linhas = adapter.Fill(ds); try { InsereCartoes(ds, idArquivo, tpLog, mapArq); } finally { conexao.Close(); } ACSPRGCRBD.InsereNovoServico(idArquivo); } } }
/// <summary> /// Dado o arquivo ele é realizado o parser e seus dados são salvos nas tabelas correspondentes /// </summary> /// <param name="path"></param> public void ProcessaArquivoCarga(string path, int idEdntidade, int idArquivo = 0) { string nomeArquivo = Path.GetFileName(path); ENLog.MapaArquivos mapArq = new ENLog.MapaArquivos(nomeArquivo, ENLog.TipoArquivo.ACSOPRGCR, path, 0, idArquivo); Log.AtualizaArquivo<ENLog.MapaArquivos>(mapArq, ENLog.StatusProcessamentoArquivo.NaoProcessado, mapArq.Arquivo, "Inicia processamento de arquivo"); int novoidArquivo = mapArq.IdArquivo; int linhaAtual = 0; int qtdCarga = 0; using (StreamReader sr = new StreamReader(path)) { DataSet ds = new DataSet(); DataTable dt = new DataTable(); ENLog.TipoLog tpLog = ENLog.TipoLog.Informação; Log.AtualizaArquivo<ENLog.MapaArquivos>(mapArq, ENLog.StatusProcessamentoArquivo.EmProcessamento, mapArq.Arquivo, "Em processamento de arquivo"); string descErro = String.Empty; while (!sr.EndOfStream) { try { linhaAtual++; string linha = sr.ReadLine(); switch (linha.Substring(0, 1)) { case "0": var cab = new ACSOPRGCRCabecalhoEN(novoidArquivo, linha); cab.Insere(); break; case "1": var lot = new ACSOPRGCRLoteEN(novoidArquivo, linha); lot.Insere(); break; case "2": var det = new ACSOPRGCRDetalheEN(novoidArquivo, linha); dt.Rows.Add(ACSOPRGCRDetalheEN.MapeiaTXT(linha, novoidArquivo, dt, idEdntidade)); //det.Insere(); qtdCarga++; break; case "9": var rdp = new ACSOPRGCRRodapeEN(novoidArquivo, linha); rdp.Insere(); break; } } catch (Exception e) { descErro = String.Format("Erro ao processar arquivo de Carga de Cartões. Linha: {0}", linhaAtual); this.InsereLog(mapArq, linhaAtual, ENLog.TipoLog.Alerta, descErro); throw; } } ds.Tables.Add(dt); InsereCartoes(ds, idArquivo, tpLog, mapArq); //Log.AtualizaArquivo<ENLog.MapaArquivos>(mapArq, ENLog.StatusProcessamentoArquivo.ProcessadoOk, mapArq.Arquivo, "Finaliza processamento de arquivo"); //descErro = String.Format("Total de cargas processadas: {0}", qtdCarga); //this.InsereLog(mapArq, linhaAtual, ENLog.TipoLog.Informação, descErro); } ACSPRGCRBD.InsereNovoServico(idArquivo); }