private void btnRel1_Click(object sender, EventArgs e) { CAMADAS.BLL.Itens bllItem = new CAMADAS.BLL.Itens(); List <CAMADAS.MODEL.Itens> lstItens = new List <CAMADAS.MODEL.Itens>(); lstItens = bllItem.Select(); string pasta = Funcoes.diretorioPasta(); string arquivo = pasta + @"\RelItens_" + DateTime.Now.ToShortDateString().Replace("/", "_") + "_" + DateTime.Now.ToLongTimeString().Replace(":", "_") + ".xlsx"; string arquivoPDF = pasta + @"\RelItens_" + DateTime.Now.ToShortDateString().Replace("/", "_") + "_" + DateTime.Now.ToLongTimeString().Replace(":", "_") + ".pdf"; FileInfo caminhoNomeArquivo = new FileInfo(arquivo); ExcelPackage arquivoExcel = new ExcelPackage(caminhoNomeArquivo); ExcelWorksheet planilha = arquivoExcel.Workbook.Worksheets.Add("Plan1"); int col = 1; int lin = 1; planilha.Cells[lin, col].Value = "ID"; planilha.Cells[lin, ++col].Value = "EMPRÉSTIMO"; planilha.Cells[lin, ++col].Value = "TÍTULO"; planilha.Cells[lin, ++col].Value = "ENTREGA"; foreach (CAMADAS.MODEL.Itens item in lstItens) { col = 1; planilha.Cells[++lin, col].Value = item.id; planilha.Cells[lin, ++col].Value = item.emprestimoID; planilha.Cells[lin, ++col].Value = item.titulo; planilha.Cells[lin, ++col].Value = item.entrega; planilha.Cells[lin, col].Style.Numberformat.Format = "dd-MM-yyyy"; } ExcelRange rng; using (rng = planilha.Cells["A1:D1"]) { rng.Style.Font.Size = 15; rng.Style.Font.Name = "Arial"; rng.Style.Font.Bold = true; rng.Style.Fill.PatternType = ExcelFillStyle.Solid; rng.Style.Fill.BackgroundColor.SetColor(Color.Gray); rng.Style.Font.Color.SetColor(Color.White); } using (rng = planilha.Cells["A2:D" + lin.ToString()]) { rng.Style.Font.Bold = false; rng.Style.Fill.PatternType = ExcelFillStyle.Solid; rng.Style.Fill.BackgroundColor.SetColor(Color.LightGray); rng.Style.Font.Color.SetColor(Color.Black); } planilha.Cells.AutoFitColumns(); planilha.Cells[++lin, 1].Value = "Gerado em " + DateTime.Now.ToString(); arquivoExcel.Save(); arquivoExcel.Dispose(); Workbook workbook = new Workbook(); //Load excel file workbook.LoadFromFile(arquivo); //Save excel file to pdf file. Worksheet worksheet = workbook.Worksheets[0]; PageSetup setup = worksheet.PageSetup; setup.FitToPagesWide = 1; setup.FitToPagesTall = 1; workbook.SaveToFile(arquivoPDF, Spire.Xls.FileFormat.PDF); System.Diagnostics.Process.Start(arquivo); }
public static void relLivro() { CAMADAS.BLL.Livros bllLivro = new CAMADAS.BLL.Livros(); List <CAMADAS.MODEL.Livros> lstLivros = new List <CAMADAS.MODEL.Livros>(); lstLivros = bllLivro.Select(); string pasta = Funcoes.diretorioPasta(); string arquivo = pasta + @"\RelLivros_" + DateTime.Now.ToShortDateString().Replace("/", "_") + "_" + DateTime.Now.ToLongTimeString().Replace(":", "_") + ".html"; string arquivoPDF = pasta + @"\RelLivros_" + DateTime.Now.ToShortDateString().Replace("/", "_") + "_" + DateTime.Now.ToLongTimeString().Replace(":", "_") + ".pdf"; StreamWriter sw = new StreamWriter(arquivo); using (sw) { sw.WriteLine("<html>"); sw.WriteLine("<head>"); sw.WriteLine("<meta http-equiv='Content-Type' " + "content='text/html; charset=utf-8'/>"); sw.WriteLine("<link rel='stylesheet' href='https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css' integrity='sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T' crossorigin='anonymous'>"); sw.WriteLine("</head>"); sw.WriteLine("<body>"); sw.WriteLine("<h1>Relatório de Livros</h1>"); sw.WriteLine("<hr align='left' border:'5px' />"); sw.WriteLine("<table class='table table-striped'>"); //Cabeçalho da tabela sw.WriteLine("<tr align='right'>"); sw.WriteLine("<th align='right' width='30px'>"); sw.WriteLine("ID"); sw.WriteLine("</th>"); sw.WriteLine("<th align='right' width='250px'>"); sw.WriteLine("TITULO"); sw.WriteLine("</th>"); sw.WriteLine("<th align='right' width='150px'>"); sw.WriteLine("EDITORA"); sw.WriteLine("</th>"); sw.WriteLine("<th align='right' width='150px'>"); sw.WriteLine("AUTOR"); sw.WriteLine("</th>"); sw.WriteLine("<th align='right' width='150px'>"); sw.WriteLine("VALOR"); sw.WriteLine("</th>"); sw.WriteLine("<th align='right' width='60px'>"); sw.WriteLine("SITUAÇÃO"); sw.WriteLine("</th>"); sw.WriteLine("</tr>"); int cont = 0; float soma = 0; foreach (CAMADAS.MODEL.Livros livro in lstLivros.OrderBy(o => o.situacao).ThenBy(t => t.autor).ThenBy(w => w.titulo)) { sw.WriteLine("<tr>"); sw.WriteLine("<td align='right' width='30px'>"); sw.WriteLine(livro.id); sw.WriteLine("</td>"); sw.WriteLine("<td align='right' width='250px'>"); sw.WriteLine(livro.titulo); sw.WriteLine("</td>"); sw.WriteLine("<td align='right' width='150px'>"); sw.WriteLine(livro.editora); sw.WriteLine("</td>"); sw.WriteLine("<td align='right' width='150px'>"); sw.WriteLine(livro.autor); sw.WriteLine("</td>"); sw.WriteLine("<td align='right' width='150px'>"); sw.WriteLine(string.Format("{0:C2}", livro.valor)); sw.WriteLine("</td>"); sw.WriteLine("<td align='right' width='60px'>"); sw.WriteLine(livro.situacao); sw.WriteLine("</td>"); sw.WriteLine("</tr>"); soma = soma + livro.valor; cont++; } sw.WriteLine(""); sw.WriteLine(""); sw.WriteLine("</table>"); sw.WriteLine("<hr align='left' border:'5px' />"); sw.WriteLine("<h5>"); sw.WriteLine("Total de Registros Impressos: " + cont.ToString()); sw.WriteLine("</br>"); sw.WriteLine("Valor total dos Livros: " + string.Format("{0:C2}", soma)); sw.WriteLine("</h5>"); sw.WriteLine("</body>"); sw.WriteLine("</html>"); } System.Diagnostics.Process.Start(arquivo); var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter(); htmlToPdf.CustomWkHtmlArgs = "--dpi 300"; htmlToPdf.GeneratePdfFromFile(arquivo, null, arquivoPDF); }