จะสร้างฝั่งเซิร์ฟเวอร์ pdf โดยใช้ Crystal Reports ด้วย C # / ASP.NET ได้อย่างไร

ฉันต้องสร้างรายงานบางอย่างในระบบที่ฉันกำลังพัฒนา...

ตอนนี้ฉันมีตัวอย่างที่ใช้ itext Sharp... อย่างไรก็ตาม เนื่องจากมันเรียบง่าย ฉันจึงไม่สามารถสร้างรายงานที่ดูดีได้...

ดังนั้นฉันจึงถูกขอให้เปลี่ยนไปใช้ Crystal Reports แต่ฉันไม่รู้ว่าจะเริ่มต้นที่ไหนหรืออย่างไร...

จากบทช่วยสอนที่ฉันเห็น ดูเหมือนว่าผู้ใช้สามารถสร้างรายงานของตนเองโดยใช้เครื่องมือ CR ได้ แต่สิ่งที่ฉันต้องการก็คือรายงานจะสร้างโดยอัตโนมัติโดยนำข้อมูลบางส่วนที่ผู้ใช้ให้มา...

นี่คือตัวอย่างสิ่งที่ฉันทำกับ 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);
            }
        }
    }
}

เช่นเดียวกับที่ฉันสร้าง pdf บนฝั่งเซิร์ฟเวอร์เหมือนในตัวอย่างก่อนหน้านี้ ฉันก็อยากทำเช่นเดียวกัน แต่ด้วย Crystal Reports...

มีวิธีที่ฉันสามารถทำสิ่งที่คล้ายกันได้หรือไม่? มีบทเรียนอะไรบ้าง?? โอ้และ BTW... ฉันจะแทรกรูปภาพลงใน pdf ได้อย่างไร

ขอบคุณ


person Victor    schedule 27.08.2012    source แหล่งที่มา


คำตอบ (1)


นี่คือบทช่วยสอนเพื่อสร้างคอลัมน์รายงาน Crystal ของคุณแบบไดนามิก

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

แต่จะดีกว่า:

  1. หากคุณใช้ Crystal Report ให้สร้างเทมเพลตที่ผู้ใช้ของคุณจะเติมเนื่องจากการเรนเดอร์ที่สวยงามสามารถทำได้ด้วยตัวช่วยสร้างรายงาน Crystal เท่านั้น (และอย่าปล่อยให้อิสระในการทำรายงานมากเกินไป)
  2. ต้องการใช้ SSRS (Sql server Reporting Service) จะดีกว่าสำหรับประสิทธิภาพและการจัดการรายงาน (คุณมีเว็บเซิร์ฟเวอร์เป็นค่าเริ่มต้นและคุณสามารถใช้บริการเว็บเพื่อจัดการรายงานเทมเพลตได้)
person Hassan Boutougha    schedule 27.08.2012