Bagaimana cara menghasilkan sisi server pdf menggunakan Crystal Reports dengan C# / ASP.NET?

Saya perlu membuat beberapa laporan dalam sistem yang saya kembangkan...

Saat ini saya memiliki sampel menggunakan itext sharp... namun, karena kesederhanaannya, saya tidak dapat membuat laporan yang terlihat bagus...

Jadi saya diminta untuk beralih ke Crystal Reports, namun saya tidak tahu harus mulai dari mana atau bagaimana...

Dari tutorial yang saya lihat, sepertinya pengguna dapat membuat laporan sendiri menggunakan alat CR, tetapi yang saya inginkan adalah laporan tersebut dihasilkan secara otomatis mengambil beberapa data yang disediakan pengguna...

Berikut ini contoh apa yang saya lakukan dengan iText sharp

public class GeneracionPDF
{
    //I need a list for products and a list for services
    List<Producto> listaProductos = new List<Producto>();
    List<Servicio> listaServicios = new List<Servicio>();

    private Producto objProducto = new Producto();
    private Servicio objServicio = new Servicio();
    private CarritoVenta objCV = new CarritoVenta();

    public void GenerarPdf(List<Articulo> lstArticulos, string strNombreReporte, string strEmpresa, string strSlogan, string strNombreVendedor, string strNombreCliente, string strRuta)
    {
        Organizar(lstArticulos);

        #region Inicio
        Document documento = new Document(PageSize.LETTER, 50, 50, 50, 50);
        PdfWriter writerPdf = PdfWriter.GetInstance(documento, new FileStream(strRuta + strNombreReporte, FileMode.Create));
        documento.Open();
        #endregion

        #region Titulo
        Font font1 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 18, Font.BOLD, BaseColor.BLUE);
        Phrase phrEmpresa = new Phrase(strEmpresa + "\n", font1);

        Font font2 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12, Font.ITALIC, BaseColor.DARK_GRAY);
        Phrase phrSlogan = new Phrase(strSlogan, font2);

        Font font16I = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 16, Font.ITALIC, BaseColor.BLACK);
        Phrase phrDescrip = new Phrase(("Cliente: " + strNombreCliente), font16I);

        PdfContentByte cb = writerPdf.DirectContent;
        documento.Add(phrEmpresa);
        documento.Add(phrSlogan);
        documento.Add(phrDescrip);
        BaseFont bfTimes = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, false);
        Font fontHelvetica16red = new Font(bfTimes, 12, Font.ITALIC, BaseColor.RED);
        #endregion

        #region Seccion Productos
        if (listaProductos.Count > 0)
        {
            documento.Add(new Paragraph("\n Productos \n", fontHelvetica16red));
            PdfPTable tableProductos = null;

            tableProductos = new PdfPTable(6);
            float[] colWidths = { 100, 200, 400, 150, 150, 150 };
            tableProductos.SetWidths(colWidths);
            Font LetraTituloTabla = FontFactory.GetFont(FontFactory.HELVETICA, 10, Font.NORMAL);
            Font LetraCeldaTabla = FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL);

            PdfPCell celdaIdProducto = new PdfPCell(new Phrase("ID", LetraTituloTabla));
            PdfPCell celdaNombreProducto = new PdfPCell(new Phrase("Nombre", LetraTituloTabla));
            PdfPCell celdaDescripcionProducto = new PdfPCell(new Phrase("DescripciĆ³n", LetraTituloTabla));
            PdfPCell celdaPrecioProducto = new PdfPCell(new Phrase("Precio", LetraTituloTabla));
            PdfPCell celdaCantidadProducto = new PdfPCell(new Phrase("Cantidad", LetraTituloTabla));
            PdfPCell celdaTotalProducto = new PdfPCell(new Phrase("Total", LetraTituloTabla));

            celdaIdProducto.HorizontalAlignment = 1;
            celdaNombreProducto.HorizontalAlignment = 1;
            celdaDescripcionProducto.HorizontalAlignment = 1;
            celdaPrecioProducto.HorizontalAlignment = 1;
            celdaCantidadProducto.HorizontalAlignment = 1;
            celdaTotalProducto.HorizontalAlignment = 1;

            tableProductos.AddCell(celdaIdProducto);
            tableProductos.AddCell(celdaNombreProducto);
            tableProductos.AddCell(celdaDescripcionProducto);
            tableProductos.AddCell(celdaPrecioProducto);
            tableProductos.AddCell(celdaCantidadProducto);
            tableProductos.AddCell(celdaTotalProducto);

            foreach (Producto prod in listaProductos)
            {
                celdaIdProducto = new PdfPCell(new Phrase(prod.IdProducto.ToString(), LetraCeldaTabla));
                celdaNombreProducto = new PdfPCell(new Phrase(prod.Nombre, LetraCeldaTabla));
                celdaDescripcionProducto = new PdfPCell(new Phrase(prod.Descripcion, LetraCeldaTabla));
                celdaPrecioProducto = new PdfPCell(new Phrase("$ " + prod.PrecioVenta, LetraCeldaTabla));
                celdaCantidadProducto = new PdfPCell(new Phrase(prod.Cantidad.ToString(), LetraCeldaTabla));
                celdaTotalProducto = new PdfPCell(new Phrase("$ " + prod.TotalCompra, LetraCeldaTabla));

                celdaIdProducto.HorizontalAlignment = 1;
                celdaNombreProducto.HorizontalAlignment = 1;
                celdaDescripcionProducto.HorizontalAlignment = 1;
                celdaPrecioProducto.HorizontalAlignment = 1;
                celdaCantidadProducto.HorizontalAlignment = 1;
                celdaTotalProducto.HorizontalAlignment = 1;

                tableProductos.AddCell(celdaIdProducto);
                tableProductos.AddCell(celdaNombreProducto);
                tableProductos.AddCell(celdaDescripcionProducto);
                tableProductos.AddCell(celdaPrecioProducto);
                tableProductos.AddCell(celdaCantidadProducto);
                tableProductos.AddCell(celdaTotalProducto);
            }

            documento.Add(tableProductos);
        }
        #endregion

        #region Seccion Servicios
        if (listaServicios.Count > 0)
        {
            documento.Add(new Paragraph("\n Servicios \n", fontHelvetica16red));
            PdfPTable tableServicios = null;

            tableServicios = new PdfPTable(6);
            float[] colWidths = { 100, 200, 400, 150, 150, 150 };
            tableServicios.SetWidths(colWidths);
            Font LetraTituloTabla = FontFactory.GetFont(FontFactory.HELVETICA, 10, Font.NORMAL);
            Font LetraCeldaTabla = FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL);

            PdfPCell celdaIdServicio = new PdfPCell(new Phrase("ID", LetraTituloTabla));
            PdfPCell celdaNombreServicio = new PdfPCell(new Phrase("Nombre", LetraTituloTabla));
            PdfPCell celdaDescripcionServicio = new PdfPCell(new Phrase("DescripciĆ³n", LetraTituloTabla));
            PdfPCell celdaPrecioServicio = new PdfPCell(new Phrase("Precio", LetraTituloTabla));
            PdfPCell celdaCantidadServicio = new PdfPCell(new Phrase("Cantidad", LetraTituloTabla));
            PdfPCell celdaTotalServicio = new PdfPCell(new Phrase("Total", LetraTituloTabla));

            celdaIdServicio.HorizontalAlignment = 1;
            celdaNombreServicio.HorizontalAlignment = 1;
            celdaDescripcionServicio.HorizontalAlignment = 1;
            celdaPrecioServicio.HorizontalAlignment = 1;
            celdaCantidadServicio.HorizontalAlignment = 1;
            celdaTotalServicio.HorizontalAlignment = 1;

            tableServicios.AddCell(celdaIdServicio);
            tableServicios.AddCell(celdaNombreServicio);
            tableServicios.AddCell(celdaDescripcionServicio);
            tableServicios.AddCell(celdaPrecioServicio);
            tableServicios.AddCell(celdaCantidadServicio);
            tableServicios.AddCell(celdaTotalServicio);

            foreach (Servicio ser in listaServicios)
            {
                celdaIdServicio = new PdfPCell(new Phrase(ser.IdServicio.ToString(), LetraCeldaTabla));
                celdaNombreServicio = new PdfPCell(new Phrase(ser.Nombre, LetraCeldaTabla));
                celdaDescripcionServicio = new PdfPCell(new Phrase(ser.Descripcion, LetraCeldaTabla));
                celdaPrecioServicio = new PdfPCell(new Phrase("$ " + ser.Precio, LetraCeldaTabla));
                celdaCantidadServicio = new PdfPCell(new Phrase(ser.Cantidad.ToString(), LetraCeldaTabla));
                celdaTotalServicio = new PdfPCell(new Phrase("$ " + ser.Total, LetraCeldaTabla));

                celdaIdServicio.HorizontalAlignment = 1;
                celdaNombreServicio.HorizontalAlignment = 1;
                celdaDescripcionServicio.HorizontalAlignment = 1;
                celdaPrecioServicio.HorizontalAlignment = 1;
                celdaCantidadServicio.HorizontalAlignment = 1;
                celdaTotalServicio.HorizontalAlignment = 1;

                tableServicios.AddCell(celdaIdServicio);
                tableServicios.AddCell(celdaNombreServicio);
                tableServicios.AddCell(celdaDescripcionServicio);
                tableServicios.AddCell(celdaPrecioServicio);
                tableServicios.AddCell(celdaCantidadServicio);
                tableServicios.AddCell(celdaTotalServicio);
            }

            documento.Add(tableServicios);
        }
        #endregion

        #region Finalizacion
        //This method returns a decimal value, so no worries here
        decimal decSubtotal = objCV.Subtotal(lstArticulos);

        documento.Add(new Paragraph("\n Subtotal: $" + decSubtotal, fontHelvetica16red));
        documento.Add(new Paragraph("IVA: $" + (decSubtotal * 0.11m), fontHelvetica16red));
        documento.Add(new Paragraph("Total: $" + (decSubtotal * 1.11m), fontHelvetica16red));
        documento.Add(new Paragraph("\nNota: Si no requiere factura, omita el IVA", fontHelvetica16red));
        documento.Close();
        #endregion
    }

    //Method to separate products and services in different lists, since lstArticulos contains both products and services
    private void Organizar(List<Articulo> lstArticulos)
    {
        listaProductos.Clear();
        listaServicios.Clear();

        foreach (Articulo art in lstArticulos)
        {
            if (art.Tipo == "Producto")
            {
                objProducto = new Producto(int.Parse(art.IdArticulo.Substring(8)), art.Nombre, art.Descripcion, art.Imagen, art.Precio, art.Cantidad);
                listaProductos.Add(objProducto);
            }

            else if (art.Tipo == "Servicio")
            {
                objServicio = new Servicio(int.Parse(art.IdArticulo.Substring(8)), art.Nombre, art.Descripcion, art.Imagen, art.Precio, art.Cantidad);
                listaServicios.Add(objServicio);
            }
        }
    }
}

Jadi sama seperti saya membuat pdf di sisi server seperti pada contoh sebelumnya, saya ingin melakukan hal yang sama tetapi dengan Crystal Reports...

Apakah ada cara saya bisa melakukan hal serupa?? ada tutorial?? oh dan BTW...bagaimana cara menyisipkan gambar di pdf?

Terima kasih


person Victor    schedule 27.08.2012    source sumber


Jawaban (1)


di sini tutorial untuk membuat kolom laporan kristal Anda secara dinamis

http://csharp.net-informations.com/crystal-reports/csharp-dynamic-crystal-reports.htm

Tapi akan lebih baik:

  1. jika Anda menggunakan crystal report buatlah template yang akan diisi oleh pengguna Anda karena rendering yang indah hanya mungkin dilakukan dengan crystal report wizard (dan jangan biarkan terlalu banyak kebebasan dalam membuat laporan)
  2. Lebih suka menggunakan SSRS (Layanan Pelaporan server Sql) lebih baik untuk kinerja dan pengelolaan laporan (Anda memiliki server web secara default dan Anda dapat menggunakan layanan web untuk mengelola templat laporan)
person Hassan Boutougha    schedule 27.08.2012