/// <summary> /// Método que monta o arquivo html. O arquivo é gerado de forma tabulada, então as linhas do arquivo de importação se~rão referentes a: primeria serão os nomes das colunas e demais as outras linhas da tabela /// </summary> /// <param name="linhas_arquivo">linhas que estão no arquivo para plotagem do html</param> /// <param name="nome_arquivoSaida">Nome do arquivo de saída</param> /// <param name="mensagemErro">Controle da mensagem caso haja algum erro</param> /// <returns></returns> protected bool MontaHTMLTabulado(List <string> linhas_arquivo, string nome_arquivoSaida, out string mensagemErro, Info_principaisTexto text_info) { Util.CL_Files.WriteOnTheLog("Documento.MontaHTMLTabulado", Global.TipoLog.DETALHADO); mensagemErro = ""; try { string html_start = "<!DOCTYPE html><html lang=\"pt-br\">"; string head = "<head> " + " <title></title> " + " <meta charset=\"UTF-8\"> " + " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> " + " <link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\"> " + " <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js\"></script> " + " <script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script> " + "</head> "; string styles = @"<style> .titulo{ margin-bottom: 20px; text-align: left; float: left; } .informacoes{ text-align: right; float: right; } table { table-layout: fixed; float: center; } .img { padding-left: 20px; } body { text-align: justify; -webkit-hyphens: auto; -moz-hyphens: auto; -ms-hyphens: auto; hyphens: auto; } </style>"; string body = html_start + head + styles + "<body>" + " <div class=\"container-fluid\">" + " <div id=\"menu\" class=\"container-fluid\"> " + " <table> " + " <tr>" + " <td>" + " <div class=\"container-fluid\"> " + " <h1 class=\"titulo\"> " + text_info.TITULO + " </h1> " + " <h4 class=\"informacoes\"> " + text_info.INFORMACOES + "</h4> " + " </div>" + " </td>" + " </tr>"; if (!string.IsNullOrEmpty(text_info.DADOS_INFO)) { body += " <tr><td><div class=\"container-fluid\"><br> " + text_info.DADOS_INFO.Replace("\\tabelatitulos", "<thead>").Replace("\\etabelatitulos", "</thead>").Replace("\\tabelacorpo", "<tbody>").Replace("\\etabelacorpo", "</tbody>").Replace("\\tabela", "<table class=\"table table-striped\">").Replace("\\etabela", "</table>").Replace("\\linha", "<tr>").Replace("\\elinha", "</tr>").Replace("\\coluna", "<td>").Replace("\\ecoluna", "</td>").Replace("\t", "<p>").Replace("\n", "</p><br>").Replace("\\t", "<b>").Replace("\\et", "</b>").Replace("\\i", "<i>").Replace("\\ei", "</i>").Replace("\\center", "<center>").Replace("\\ecenter", "</center>").Replace("\\u", "<u>").Replace("\\eu", "</u>") + "</div></td></tr>"; } else { body += " <tr><td><div class=\"container-fluid\"><br></div></td></tr>"; } if (linhas_arquivo.Count > 0) { body += " <tr><td><div class=\"container-fluid\">" + " <table class=\"img\">"; bool first = true; foreach (string text in linhas_arquivo) { body += " <tr>"; List <string> texto_completo = text.Split(';').ToList(); foreach (string conteudo in texto_completo) { if (first) { body += " <th scope=\"col\">" + conteudo + "</th>"; } else if (conteudo.Contains("<img")) { body += " <td width=\"700px\" height=\"700px\">" + conteudo + "</td>"; } else { body += " <td><p style=\"margin-right: 10px;\">" + conteudo + "</p></td>"; } } body += "</tr>"; first = false; } body += " " + " </table>" + " </div></td></tr> " + ( text_info.paginacao ? " <tr><td><div class=\"form-group\">" + " <div class=\"col-sm-offset-11 col-sm-10\">" + " Page " + text_info.numero_pagina + " </div>" + " </div></td></tr>" : "") + " </table>" + " </div>"; } body += " </div>" + " </body>" + "</html>"; if (File.Exists(nome_arquivoSaida)) { File.Delete(nome_arquivoSaida); } File.AppendAllText(nome_arquivoSaida, body); return(true); } catch (Exception e) { mensagemErro = e.Message; Util.CL_Files.WriteOnTheLog(mensagemErro, Global.TipoLog.SIMPLES); return(false); } }
/// <summary> /// Método que gera o relátorio a partir do arquivo instanciado na classe /// </summary> /// <param name="caminho_saida"></param> /// <param name="mensagemErro"></param> /// <returns></returns> public override bool GerarRelatorio(string caminho_saida, out string mensagemErro) { Util.CL_Files.WriteOnTheLog("Documento_From_XML.GerarRelatorio", Global.TipoLog.DETALHADO); mensagemErro = ""; bool sucesso = true; try { if (!VerificaExisteFile(file_selected.FullName)) { mensagemErro = "File " + file_selected.FullName + " do not exists"; return(false); } if (!VerificaExisteDirectory(caminho_saida)) { Directory.CreateDirectory(caminho_saida); } List <Info_principaisTexto> informacoes = new List <Info_principaisTexto>(); Info_principaisTexto info = new Info_principaisTexto(); info.paginacao = false; XmlTextReader xml_file = new XmlTextReader(file_selected.FullName); bool element = false, text = false, endelement = false; string tag = ""; while (xml_file.Read()) { switch (xml_file.NodeType) { case XmlNodeType.Element: text = false; element = true; endelement = false; break; case XmlNodeType.EndElement: text = false; element = false; endelement = true; break; case XmlNodeType.Text: text = true; endelement = false; break; } if (text) { if (tag.Equals("title")) { info.TITULO = xml_file.Value; } else if (tag.Equals("informacoes")) { info.INFORMACOES = xml_file.Value; } else if (tag.Equals("text")) { info.DADOS_INFO = xml_file.Value; } else if (tag.Equals("table_lines")) { info.numero_linhas = int.Parse(xml_file.Value); } else if (tag.Equals("csv_file")) { info.csv_file = new FileInfo(xml_file.Value); } element = text = false; } if (element) { tag = xml_file.LocalName; element = false; } if (endelement) { tag = xml_file.LocalName; if (tag.Equals("page")) { int totalPossible = 5391; List <Info_principaisTexto> lista = new List <Info_principaisTexto>(); string texto = info.DADOS_INFO; if (!string.IsNullOrEmpty(texto)) { List <string> mensagens = texto.Split('|').ToList(); bool entrei = false; foreach (string t in mensagens) { if (entrei) { info.TITULO = ""; } entrei = true; info.DADOS_INFO = t; int numero_quebralinha = info.DADOS_INFO.Substring(0, info.DADOS_INFO.Length < totalPossible ? info.DADOS_INFO.Length : totalPossible).Split('\n').Count(); totalPossible -= (numero_quebralinha * 200); if (totalPossible < 0) { totalPossible = 2300; } if (info.DADOS_INFO.Count() > totalPossible) { string dados = info.DADOS_INFO; int pos_ponto, pos_excla, pos_inte, pos_doisp, pos_pontov; pos_ponto = pos_excla = pos_inte = pos_doisp = pos_pontov = 0; info.DADOS_INFO = dados.Substring(0, dados.Length < totalPossible ? dados.Length : totalPossible); pos_ponto = info.DADOS_INFO.LastIndexOf('.'); pos_excla = info.DADOS_INFO.LastIndexOf('!'); pos_inte = info.DADOS_INFO.LastIndexOf('?'); pos_doisp = info.DADOS_INFO.LastIndexOf(':'); pos_pontov = info.DADOS_INFO.LastIndexOf(';'); int maior = Maior(pos_ponto, pos_excla, pos_inte, pos_doisp, pos_pontov); // mais um porque o ponto entra info.DADOS_INFO = dados.Substring(0, maior + 1); dados = dados.Remove(0, info.DADOS_INFO.Count()); lista.Add(info); while (!string.IsNullOrEmpty(dados)) { totalPossible = 5391; numero_quebralinha = dados.Substring(0, (dados.Count() >= totalPossible ? totalPossible : dados.Count())).Split('\n').Count(); totalPossible -= (numero_quebralinha * 200); if (totalPossible < 0) { totalPossible = 2300; } Info_principaisTexto info2 = new Info_principaisTexto(); info2.INFORMACOES = ""; info2.TITULO = ""; info2.DADOS_INFO = dados.Substring(0, (dados.Count() >= totalPossible ? totalPossible : dados.Count())); pos_ponto = pos_excla = pos_inte = pos_doisp = pos_pontov = 0; pos_ponto = info2.DADOS_INFO.LastIndexOf('.'); pos_excla = info2.DADOS_INFO.LastIndexOf('!'); pos_inte = info2.DADOS_INFO.LastIndexOf('?'); pos_doisp = info2.DADOS_INFO.LastIndexOf(':'); pos_pontov = info2.DADOS_INFO.LastIndexOf(';'); maior = Maior(pos_ponto, pos_excla, pos_inte, pos_doisp, pos_pontov); info2.DADOS_INFO = dados.Substring(0, (maior == 0 ? dados.Count() + 1 : maior + 1)); dados = dados.Remove(0, info2.DADOS_INFO.Count()); if (string.IsNullOrEmpty(info2.DADOS_INFO)) { break; } lista.Add(info2); } } else { lista.Add(info); } } } else { lista.Add(info); } foreach (Info_principaisTexto i in lista) { informacoes.Add(i); } info = new Info_principaisTexto(); } endelement = false; } } xml_file.Close(); xml_file.Dispose(); List <string> arquivo = new List <string>(); List <string> imagens = new List <string>(); int cont = 0; foreach (Info_principaisTexto inf in informacoes) { info = inf; if (info.csv_file != null) { if (!info.csv_file.Exists) { mensagemErro = "Arquivo csv " + info.csv_file.FullName + " não existe!"; return(false); } } if (info.csv_file != null) { StreamReader streamReader = new StreamReader(info.csv_file.FullName); bool acabei_entrar = false; while (!streamReader.EndOfStream) { try { string linha = streamReader.ReadLine(); arquivo.Add(linha); acabei_entrar = false; if ((arquivo.Count >= info.numero_linhas && sucesso)) { acabei_entrar = true; if (MontaHTMLTabulado(arquivo, Global.app_temp_html_file, out mensagemErro, info)) { imagens.Add(Global.app_temp_image_file.Split('.')[0] + cont + ".jpeg"); if (MontaImagemFromHtml(Global.app_temp_html_file, Global.app_temp_image_file.Split('.')[0] + cont + ".jpeg", out mensagemErro)) { sucesso = true; } else { sucesso = false; } info.DADOS_INFO = ""; } else { sucesso = false; } arquivo = new List <string>(); cont++; } } catch (Exception e) { mensagemErro = "Error: " + e.Message; return(false); } } if (!acabei_entrar) { if (MontaHTMLTabulado(arquivo, Global.app_temp_html_file, out mensagemErro, info)) { imagens.Add(Global.app_temp_image_file.Split('.')[0] + cont + ".jpeg"); if (MontaImagemFromHtml(Global.app_temp_html_file, Global.app_temp_image_file.Split('.')[0] + cont + ".jpeg", out mensagemErro)) { cont++; sucesso = true; } else { sucesso = false; } } else { sucesso = false; } } streamReader.Close(); streamReader.Dispose(); } else { arquivo = new List <string>(); if (MontaHTMLTabulado(arquivo, Global.app_temp_html_file, out mensagemErro, info)) { imagens.Add(Global.app_temp_image_file.Split('.')[0] + cont + ".jpeg"); if (MontaImagemFromHtml(Global.app_temp_html_file, Global.app_temp_image_file.Split('.')[0] + cont + ".jpeg", out mensagemErro)) { cont++; sucesso = true; } else { sucesso = false; } } else { sucesso = false; } } } if (sucesso) { Info_PDF_file info2 = GetInfo(); sucesso = MontaPDFFromImages(imagens, caminho_saida + "\\" + file_selected.Name.Split('.')[0] + ".pdf", info2, out mensagemErro); foreach (string cam in imagens) { File.Delete(cam); } } } catch (Exception e) { Util.CL_Files.WriteOnTheLog("Erro ao gerar o relatório. Erro: " + e.Message, Global.TipoLog.SIMPLES); return(false); } return(sucesso); }
/// <summary> /// Método que monta o arquivo html. O arquivo é gerado de forma tabulada, então as linhas do arquivo de importação se~rão referentes a: primeria serão os nomes das colunas e demais as outras linhas da tabela /// </summary> /// <param name="linhas_arquivo">linhas que estão no arquivo para plotagem do html</param> /// <param name="nome_arquivoSaida">Nome do arquivo de saída</param> /// <param name="mensagemErro">Controle da mensagem caso haja algum erro</param> /// <returns></returns> protected bool MontaHTML(List <string> linhas_arquivo, string nome_arquivoSaida, out string mensagemErro, Info_principaisTexto text_info) { Util.CL_Files.WriteOnTheLog("Documento.MontaHTML", Global.TipoLog.DETALHADO); mensagemErro = ""; try { string html_start = "<!DOCTYPE html><html lang=\"pt-br\">"; string head = "<head> " + " <title></title> " + " <meta charset=\"UTF-8\"> " + " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> " + " <link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\"> " + " <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js\"></script> " + " <script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script> " + "</head> "; string styles = @"<style> .titulo{ margin-bottom: 20px; text-align: left; float: left; } .informacoes{ text-align: right; float: right; } </style>"; string body = html_start + head + styles + "<body>" + " <div class=\"container\">" + " <div id=\"menu\"> " + " <div> " + " <h1 class=\"titulo\"> " + text_info.TITULO + " </h1> " + " <h4 class=\"informacoes\"> " + text_info.INFORMACOES + "</h4> " + " </div>"; body += " <table class=\"table table-striped\">"; bool first = true; foreach (string text in linhas_arquivo) { if (first) { body += "<thead>" + " <tr>"; } else { body += " <tr>"; } List <string> texto_completo = text.Split(';').ToList(); foreach (string conteudo in texto_completo) { if (first) { body += " <th scope=\"col\">" + conteudo + "</th>"; } else { body += " <td>" + conteudo + "</td>"; } } if (first) { body += " </tr>" + "</thead>"; body += "<tbody>"; } else { body += "</tr>"; } first = false; } body += " </tbody>" + " </table>" + ( text_info.paginacao ? " <div class=\"form - group\">" + " <div class=\"col-sm-offset-11 col-sm-10\">" + " Page " + text_info.numero_pagina + " </div>" + " </div>" : "") + " </div>" + "</div>" + "</body>" + "</html>"; if (File.Exists(nome_arquivoSaida)) { File.Delete(nome_arquivoSaida); } File.AppendAllText(nome_arquivoSaida, body); return(true); } catch (Exception e) { mensagemErro = e.Message; Util.CL_Files.WriteOnTheLog(mensagemErro, Global.TipoLog.SIMPLES); return(false); } }
/// <summary> /// Método que gera o relátorio a partir do arquivo instanciado na classe /// </summary> /// <param name="caminho_saida"></param> /// <param name="mensagemErro"></param> /// <returns></returns> public override bool GerarRelatorio(string caminho_saida, out string mensagemErro) { Util.CL_Files.WriteOnTheLog("Documento_From_CSV.GerarRelatorio", Global.TipoLog.DETALHADO); mensagemErro = ""; bool sucesso = true; try { if (!base.VerificaExisteFile(file_selected.FullName)) { mensagemErro = "File " + file_selected.FullName + " do not exists"; return(false); } if (!VerificaExisteDirectory(caminho_saida)) { Directory.CreateDirectory(caminho_saida); } if (!VerificaExisteDirectory(Global.app_main_directoty)) { Directory.CreateDirectory(Global.app_main_directoty); } if (!VerificaExisteDirectory(Global.app_temp_directory)) { Directory.CreateDirectory(Global.app_temp_directory); } Model.MD_PDFInformations informations = new Model.MD_PDFInformations(0); Info_principaisTexto info = new Info_principaisTexto(); info.TITULO = informations.Title; info.INFORMACOES = ""; info.paginacao = false; StreamReader streamReader = new StreamReader(file_selected.FullName); List <string> arquivo = new List <string>(); bool acabei_entrar = false; int cont = 0; List <string> imagens = new List <string>(); while (!streamReader.EndOfStream) { try { string linha = streamReader.ReadLine(); arquivo.Add(linha); acabei_entrar = false; if ((arquivo.Count >= 24 && sucesso)) { acabei_entrar = true; if (MontaHTML(arquivo, Global.app_temp_html_file, out mensagemErro, info)) { imagens.Add(Global.app_temp_image_file.Split('.')[0] + cont + ".jpeg"); if (MontaImagemFromHtml(Global.app_temp_html_file, Global.app_temp_image_file.Split('.')[0] + cont + ".jpeg", out mensagemErro)) { sucesso = true; } else { sucesso = false; } } else { sucesso = false; } arquivo = new List <string>(); cont++; } } catch (Exception e) { mensagemErro = "Error: " + e.Message; return(false); } } if (!acabei_entrar) { if (MontaHTML(arquivo, Global.app_temp_html_file, out mensagemErro, info)) { imagens.Add(Global.app_temp_image_file.Split('.')[0] + cont + ".jpeg"); if (MontaImagemFromHtml(Global.app_temp_html_file, Global.app_temp_image_file.Split('.')[0] + cont + ".jpeg", out mensagemErro)) { sucesso = true; } else { sucesso = false; } } else { sucesso = false; } } if (sucesso) { Info_PDF_file info2 = GetInfo(); sucesso = MontaPDFFromImages(imagens, caminho_saida + "\\" + file_selected.Name.Split('.')[0] + ".pdf", info2, out mensagemErro); foreach (string cam in imagens) { File.Delete(cam); } } streamReader.Close(); streamReader.Dispose(); } catch (Exception e) { Util.CL_Files.WriteOnTheLog("Erro ao gerar o relatório. Erro: " + e.Message, Global.TipoLog.SIMPLES); return(false); } return(sucesso); }