protected void btnGerarCsv_Click(object sender, EventArgs e) { var dataTable = _estoqueDAO.GerarCsvEstoque(GerarFiltro()); StringBuilder builder = new StringBuilder(); List <string> rows = new List <string>(); List <string> colunas = new List <string>(); //foreach (DataColumn column in dataTable.Columns) //{ // colunas.Add(column.ColumnName); //} //foreach (DataRow row in dataTable.Rows) //{ // List<string> campos = new List<string>(); // foreach (DataColumn column in dataTable.Columns) // { // object item = row[column]; // campos.Add(item.ToString()); // } // rows.Add(string.Join(";", campos.ToArray())); //} //builder.Append(string.Join(";", colunas.ToArray())); //builder.Append("\n"); //builder.Append(string.Join("\n", rows.ToArray())); var agora = DateTime.Now; var nomeArquivo = Path.Combine(Server.MapPath("."), "Uploads", $"LM_{agora.ToString("ddMMyyyy")}_{agora.ToString("HHmmss")}.csv"); //try //{ var sw = new StreamWriter(nomeArquivo, false); string Head = ""; for (int j = 0; j < dataTable.Columns.Count; j++) { if (Head.Trim() != "") { Head += ";" + dataTable.Columns[j].ColumnName + ""; } else { Head += "" + dataTable.Columns[j].ColumnName + ""; } } sw.WriteLine(Head); for (int j = 0; j < dataTable.Rows.Count; j++) { string[] dataArr = new String[dataTable.Rows[j].ItemArray.Length]; for (int i = 0; i < dataTable.Rows[j].ItemArray.Length; i++) { object o = dataTable.Rows[j].ItemArray[i].ToString(); dataArr[i] = "" + o.ToString() + ""; } sw.WriteLine(string.Join(";", dataArr)); } sw.Close(); //Response.Clear(); //Response.ContentType = "text/csv"; //Response.AddHeader("Content-Disposition", $"attachment;filename={nomeArquivo}.csv"); //Response.Write(builder.ToString()); //Response.End(); Response.Clear(); Response.Buffer = true; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ClearContent(); Response.ClearHeaders(); Response.AppendHeader(@"Content-Disposition", "attachment; filename=" + Path.GetFileName(nomeArquivo)); Response.ContentType = "application/csv"; Response.WriteFile(nomeArquivo); Response.End(); //} //catch (Exception ex) //{ // Response.Write(ex.Message); //} //finally //{ // System.GC.Collect(); // System.GC.WaitForPendingFinalizers(); // if ((System.IO.File.Exists(nomeArquivo))) // System.IO.File.Delete(nomeArquivo); //} }
protected void btnGerarCsv_Click(object sender, EventArgs e) { LogsService.Logar("ConsultaEstoquePreACD_NEW.aspx", $"Iniciou consulta no Banco as {DateTime.Now.ToString()}"); var dataTable = _estoqueDAO.GerarCsvEstoque(GerarFiltro()); LogsService.Logar("ConsultaEstoquePreACD_NEW.aspx", $"Finalizou consulta no Banco as {DateTime.Now.ToString()}"); var agora = DateTime.Now; var nomeArquivo = Path.Combine(Server.MapPath("."), "Uploads", $"LM_{agora.ToString("ddMMyyyy")}_{agora.ToString("HHmmss")}.csv"); LogsService.Logar("ConsultaEstoquePreACD_NEW.aspx", $"Começou montar o arquivo as {DateTime.Now.ToString()}"); var sw = new StreamWriter(nomeArquivo, false); string Head = ""; for (int j = 0; j < dataTable.Columns.Count; j++) { if (Head.Trim() != "") { Head += ";" + dataTable.Columns[j].ColumnName + ""; } else { Head += "" + dataTable.Columns[j].ColumnName + ""; } } sw.WriteLine(Head); for (int j = 0; j < dataTable.Rows.Count; j++) { string[] dataArr = new String[dataTable.Rows[j].ItemArray.Length]; for (int i = 0; i < dataTable.Rows[j].ItemArray.Length; i++) { object o = dataTable.Rows[j].ItemArray[i].ToString(); dataArr[i] = "" + o.ToString() + ""; } sw.WriteLine(string.Join(";", dataArr)); } sw.Close(); LogsService.Logar("ConsultaEstoquePreACD_NEW.aspx", $"Finalizou a montagem do arquivo as {DateTime.Now.ToString()}"); Response.Clear(); Response.Buffer = true; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ClearContent(); Response.ClearHeaders(); Response.AppendHeader(@"Content-Disposition", "attachment; filename=" + Path.GetFileName(nomeArquivo)); Response.ContentType = "application/csv"; Response.WriteFile(nomeArquivo); Response.End(); //} //catch (Exception ex) //{ // Response.Write(ex.Message); //} //finally //{ // System.GC.Collect(); // System.GC.WaitForPendingFinalizers(); // if ((System.IO.File.Exists(nomeArquivo))) // System.IO.File.Delete(nomeArquivo); //} }
public static string ImportarNFs(List <string> chaves, string guid) { try { logger.Info($"{DateTime.Now} - Início consulta NF's: {String.Join(",", chaves)}"); //ConsultaNfs cnf = new ConsultaNfs(); var _notaFiscalDAOx = new NotaFiscalDAO(); foreach (var chave in chaves) { // Levi alterou aqui 08/01/2021 - pesquisava qualque coisa do txt. Se fizesse upload de arquivo errado poderia gerar milhares de consultas no siscomex. string chaveaux = chave.Trim(); if (chaveaux.Length == 44) { var nf = new NotaFiscalConsultaCCT() { ChaveNF = chaveaux, SaldoCCT = "0.0", OBS = string.Empty }; var chaveNf = chave.Replace(";", ""); var cnpjNf = chave.Substring(6, 14); var numeroNf = chave.Substring(25, 9); var anoNf = chave.Substring(2, 2); var mesNf = chave.Substring(4, 2); var DataEmissaoNF = $"{anoNf}{mesNf}"; var filtro = (" AND CnpjDestinatario ='" + cnpjNf + "' AND NumeroNF = " + numeroNf + " AND [NotaFiscalEmissao] = " + DataEmissaoNF); var _estoqueDAO = new EstoqueDAO(false); var ds = _estoqueDAO.GerarCsvEstoque(filtro.ToString()); if (ds.Rows.Count > 0) { nf.DataRegistro = ds.Rows[0].ItemArray[7].ToString(); //DataHoraEntradaEstoque nf.SaldoCCT = String.Format("{0:#########.000}", ds.Rows[0].ItemArray[53]); //saldoatual ("#########.###") nf.ChaveNF = chaveaux.Substring(0, 44); nf.Recinto = ds.Rows[0].ItemArray[19].ToString(); // CodigoRA; nf.UnidadeReceita = ds.Rows[0].ItemArray[23].ToString(); //CodigoURF; nf.OBS = "Pesquisado no banco de dados interno"; nf.Item = Convert.ToInt32(ds.Rows[0].ItemArray[52].ToString()); //item; nf.PesoEntradaCCT = ds.Rows[0].ItemArray[5].ToString(); //saldo; nf.PesoAferido = ds.Rows[0].ItemArray[4].ToString(); //pesoaferido nf.DUE = ""; nf.QtdeAverbada = ""; _notaFiscalDAOx.CadastrarNFImportacaoConsulta(nf, guid); } else { ValidarNotasSiscomexUnitario(nf, guid); } } else { chaveaux = chaveaux + " "; var nf = new NotaFiscalConsultaCCT() { ChaveNF = chaveaux.Substring(0, 44), DataRegistro = System.DateTime.Now.ToString(), SaldoCCT = "", OBS = "Chave inválida, somente chaves de 44 dígitos podem se consutadas. Notas formulários não estão disponíveis no Siscomex." }; _notaFiscalDAOx.CadastrarNFImportacaoConsulta(nf, guid); } // até aqui } logger.Info($"{DateTime.Now} - Término consulta NF's"); return(string.Empty); } catch (Exception ex) { throw new Exception("Não foi possível importar NFs"); } }