Report from a form with no database

Asked

Viewed 195 times

0

My question is, can I generate a report (I don’t know if I can call it that):


Report: Date of payment
Client: xxxxxx xxx xxx xxxx
Payment date: xx/xx/xxxx


Signing


It would be a very simple layout basically what I did above without using database anyway, would fill the values and print what is filled and end.

The layout of the program is this:

inserir a descrição da imagem aqui

How can I do that? And if it is possible, because I researched some and did not understand very well, since my Visualstudio does not have the CrystalReports

UPDATE

Class Print.Cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Impressao
{
    public static class Funcoes
    {
        /// <summary>
        /// Gera a impressão de um texto
        /// </summary>
        /// <param name="_textoImpressao">string para impressão</param>
        /// <param name="_font">Fonte da impressão</param>
        /// <param name="_impressora">Nome da impressora, informar null ou "" para padrão do windows</param>
        public static void ImprimirString(string _textoImpressao, Font _font, string _impressora)
        {
            string[] linhas = _textoImpressao.Split('\n');
            Queue<string> filaLinhas = new Queue<string>();
            foreach (string l in linhas)
            {
                filaLinhas.Enqueue(l);
            }

            PrintDocument p = new PrintDocument();
            p.PrintPage += delegate (object sender1, PrintPageEventArgs ev)
            {
                Font printFont = _font;
                float linesPerPage = 0;
                float yPos = 0;
                int count = 0;
                float leftMargin = ev.MarginBounds.Left;
                float topMargin = ev.MarginBounds.Top;
                string line = null;

                //Calcular o número de linhas por página
                linesPerPage = ev.MarginBounds.Height /
                   printFont.GetHeight(ev.Graphics);

                // Imprime cada linha do texto
                while (count < linesPerPage && filaLinhas.Count > 0)
                {
                    line = filaLinhas.Dequeue();
                    yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                    ev.Graphics.DrawString(line, printFont, Brushes.Black, 0, yPos, new StringFormat());
                    count++;
                }

                // Se existir mais linhas, gera outra página.
                if (line != null && filaLinhas.Count > 0)
                    ev.HasMorePages = true;
                else
                    ev.HasMorePages = false;
            };

            PrintDialog diag = new PrintDialog();
            diag.Document = p;
            diag.PrinterSettings.PrinterName = _impressora;
            if (diag.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                p.Print();
            }
        }
    }
}

Print Generate button: (I try to call the class but don’t think)

 private void button1_Click(object sender, EventArgs e)
        {
            Impressao b = new Impressao();
            DateTime dataPagamento = DateTime.Now;
            string clienteNome = "Fulano da Silva Sauro";
            string texto = "Relatório de Pagamento" + "\n";
            texto += "Cliente: " + clienteNome + "\n";
            texto += "Data Pagamento: " + dataPagamento.ToString("dd/MM/yyyy HH:mm");

            FuncoesImprimir.ImprimirString(texto, new Font("Consolas", 12f, FontStyle.Regular), null);
        }

inserir a descrição da imagem aqui

  • How will you print ? "normal" printer, matricial, dual (Thermal) ?

  • In a normal printer, sheet A4, what I need is to open the report with the data filled already put to print and end

  • 1

    your command should look something like Impressao.Funcoes.ImprimirString(..);

  • See how it looks, it doesn’t seem to find

  • Ata understood, now stopped.. I was trying to instantiate the class, but doing as I said gave,

1 answer

4


Thinking of a simple, text-only printing and that no additional software like Crystal Reports is needed, you can use this function that generates the printing by C#itself. Simply inform the text that will be printed, the source, and the printer that will be used for printing (optional):

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SeuNameSpace
{
    public static class Funcoes
    {
        /// <summary>
        /// Gera a impressão de um texto
        /// </summary>
        /// <param name="_textoImpressao">string para impressão</param>
        /// <param name="_font">Fonte da impressão</param>
        /// <param name="_impressora">Nome da impressora, informar null ou "" para padrão do windows</param>
        public static void ImprimirString(string _textoImpressao, Font _font, string _impressora)
        {
            string[] linhas = _textoImpressao.Split('\n');
            Queue<string> filaLinhas = new Queue<string>();
            foreach (string l in linhas)
            {
                filaLinhas.Enqueue(l);
            }

            PrintDocument p = new PrintDocument();
            p.PrintPage += delegate(object sender1, PrintPageEventArgs ev)
            {
                Font printFont = _font;
                float linesPerPage = 0;
                float yPos = 0;
                int count = 0;
                float leftMargin = ev.MarginBounds.Left;
                float topMargin = ev.MarginBounds.Top;
                string line = null;

                //Calcular o número de linhas por página
                linesPerPage = ev.MarginBounds.Height /
                   printFont.GetHeight(ev.Graphics);

                // Imprime cada linha do texto
                while (count < linesPerPage && filaLinhas.Count > 0)
                {
                    line = filaLinhas.Dequeue();
                    yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                    ev.Graphics.DrawString(line, printFont, Brushes.Black, 0, yPos, new StringFormat());
                    count++;
                }

                // Se existir mais linhas, gera outra página.
                if (line != null && filaLinhas.Count > 0)
                    ev.HasMorePages = true;
                else
                    ev.HasMorePages = false;
            };

            PrintDialog diag = new PrintDialog();
            diag.Document = p;
            diag.PrinterSettings.PrinterName = _impressora;
            if (diag.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                p.Print();
            }
        }
    }
}

To use the code:

        DateTime dataPagamento = DateTime.Now;
        string clienteNome = "Fulano da Silva Sauro";
        string texto = "Relatório de Pagamento" + "\n";
        texto += "Cliente: " + clienteNome + "\n";
        texto += "Data Pagamento: " + dataPagamento.ToString("dd/MM/yyyy HH:mm");

        FuncoesImprimir.ImprimirString(texto, new Font("Consolas", 12f, FontStyle.Regular), null);
  • How do I use this code? Do I need to create inside a . Cs? Put in the generate button?

  • creates a CS and pastes all that code inside it, just remember to change the namespace to your

  • Why can’t I call the class I created in the form? Ai error in line d FuncoesImprimir

  • 1

    barter namespace Impressao for namespace ProjetoFinal and is a static class, does not need the instance b = new Impressao(); just call exactly as I put up here

  • 1

    It worked out bro, it was what I really needed, now I’ll adapt to what I need :) thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.