Example #1
0
        static public void SaveSmallAudit(string path, string pathSaveFile, IEnumerable <ListDisplay.ListItem> data,
                                          DateTime?dateFrom, DateTime?dateTo, string sumPaid)
        {
            Document document = new Document();

            document.Info.Title = "Mały audyt";

            Section section = document.AddSection();

            section.PageSetup.LeftMargin = "1cm";
            Paragraph paragraph;
            Table     table;
            Column    column;
            Row       row;

            paragraph = section.AddParagraph("Wyciąg z audytu wygenerowany " + DateTime.Now);
            paragraph.Format.Font.Size = 6;

            if (dateFrom == null)
            {
                paragraph = section.AddParagraph("Data od: " + DateTime.Parse(data.First().ItemDate).ToShortDateString());
                paragraph.Format.Font.Size = 6;
            }
            else
            {
                paragraph = section.AddParagraph("Data od: " + dateFrom.Value.ToShortDateString());
                paragraph.Format.Font.Size = 6;
            }

            if (dateTo == null)
            {
                paragraph = section.AddParagraph("Data do: " + DateTime.Parse(data.Last().ItemDate).ToShortDateString());
                paragraph.Format.Font.Size = 6;
            }
            else
            {
                paragraph = section.AddParagraph("Data do: " + dateTo.Value.ToShortDateString());
                paragraph.Format.Font.Size = 6;
            }

            paragraph = section.AddParagraph("\n\n");

            //Table section
            table                     = section.AddTable();
            table.Style               = "Table";
            table.Borders.Color       = Color.Parse("Black");
            table.Borders.Width       = 0.25;
            table.Borders.Left.Width  = 0.5;
            table.Borders.Right.Width = 0.5;
            table.Format.Alignment    = ParagraphAlignment.Center;
            table.Rows.LeftIndent     = 0;

            column = table.AddColumn("2,5cm");
            column.Format.Alignment = ParagraphAlignment.Center;
            column.Format.Font.Size = 5;
            column = table.AddColumn("2,5cm");
            column.Format.Alignment = ParagraphAlignment.Center;
            column.Format.Font.Size = 5;
            column = table.AddColumn("2,5cm");
            column.Format.Alignment = ParagraphAlignment.Center;
            column.Format.Font.Size = 5;
            column = table.AddColumn("2cm");
            column.Format.Alignment = ParagraphAlignment.Center;
            column.Format.Font.Size = 5;

            row = table.AddRow();
            row.Format.Font.Bold = true;
            row.Format.Font.Size = 6;
            row.Cells[0].AddParagraph("Kod pracownika");
            row.Cells[1].AddParagraph("ID");
            row.Cells[2].AddParagraph("Televend ID");
            row.Cells[3].AddParagraph("Suma zakupów");

            List <string> IDs = DatabaseHandling.GetCardIDs(path);

            int i;
            IEnumerable <ListDisplay.ListItem> currentIDItem;

            for (i = 0; i < IDs.Count(); i++)
            {
                currentIDItem = (data as IEnumerable <ListDisplay.ListItem>).Where(item => item.ItemID == IDs[i]);
                if (currentIDItem.FirstOrDefault().ItemID == null)
                {
                    continue;
                }
                row = table.AddRow();
                row.Cells[0].AddParagraph(currentIDItem.FirstOrDefault().ItemWorkerID);
                row.Cells[2].AddParagraph(IDs[i]);
                row.Cells[1].AddParagraph(currentIDItem.FirstOrDefault().ItemStickerID);
                row.Cells[3].AddParagraph(currentIDItem.Sum(item => Convert.ToDecimal(item.ItemPurchase)).ToString());
            }

            row = table.AddRow();
            row.Format.Font.Bold = true;
            row.Format.Font.Size = 7;
            row.Cells[0].AddParagraph("Suma");
            row.Cells[3].AddParagraph(sumPaid);

            //Final PDF generation
            PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);

            renderer.Document = document;
            renderer.RenderDocument();

            if (!pathSaveFile.EndsWith(".pdf"))
            {
                pathSaveFile += ".pdf";
            }

            renderer.PdfDocument.Save(pathSaveFile);
            Process.Start(pathSaveFile);
        }
Example #2
0
        static public void SaveSmallAudit(string path, string pathSaveFile, IEnumerable <ListDisplay.ListItem> data,
                                          DateTime?dateFrom, DateTime?dateTo, string sumPaid)
        {
            XSSFWorkbook xlWorkBook = new XSSFWorkbook();
            ISheet       sheet = xlWorkBook.CreateSheet("1");
            IRow         firstRow, row;

            firstRow = sheet.CreateRow(0);
            row      = sheet.CreateRow(1);
            row.CreateCell(0).SetCellValue("Data od");
            row.CreateCell(2).SetCellValue("Data do");

            if (dateTo == null)
            {
                row.CreateCell(3).SetCellValue(DateTime.Parse(data.Last().ItemDate).ToShortDateString());
            }
            else
            {
                row.CreateCell(3).SetCellValue(dateTo.Value.ToShortDateString());
            }

            if (dateFrom == null)
            {
                row.CreateCell(1).SetCellValue(DateTime.Parse(data.First().ItemDate).ToShortDateString());
            }
            else
            {
                row.CreateCell(1).SetCellValue(dateFrom.Value.ToShortDateString());
            }

            row = sheet.CreateRow(2);
            row.CreateCell(0).SetCellValue("Kod pracownika");
            row.CreateCell(1).SetCellValue("ID");
            row.CreateCell(2).SetCellValue("Televend ID");
            row.CreateCell(3).SetCellValue("Suma zakupów");

            List <string> IDs = DatabaseHandling.GetCardIDs(path);

            int i;
            int rowCounter = 3;
            IEnumerable <ListDisplay.ListItem> currentIDItem;

            for (i = 0; i < IDs.Count(); i++)
            {
                currentIDItem = (data as IEnumerable <ListDisplay.ListItem>).Where(item => item.ItemID == IDs[i]);
                if (currentIDItem.FirstOrDefault().ItemID == null)
                {
                    continue;
                }

                string currentSum = currentIDItem.Sum(item => Convert.ToDecimal(item.ItemPurchase)).ToString();

                if (currentSum == "0")
                {
                    continue;
                }

                row = sheet.CreateRow(rowCounter);
                row.CreateCell(0).SetCellValue(currentIDItem.FirstOrDefault().ItemWorkerID);
                row.CreateCell(2).SetCellValue(IDs[i]);
                row.CreateCell(1).SetCellValue(currentIDItem.FirstOrDefault().ItemStickerID);
                row.CreateCell(3).SetCellValue(currentSum);
                rowCounter++;
            }
            row = sheet.CreateRow(i + 3);
            row.CreateCell(0).SetCellValue("Suma");
            row.CreateCell(3).SetCellValue(sumPaid);

            for (int j = 0; j < 4; j++)
            {
                sheet.AutoSizeColumn(j);
            }

            //Add after auto sizing to avoid enormous first column
            firstRow.CreateCell(0).SetCellValue("Wyciąg z audytu wygenerowany " + DateTime.Now);

            if (!pathSaveFile.EndsWith(".xlsx"))
            {
                pathSaveFile += ".xlsx";
            }

            using (var fs = new FileStream(pathSaveFile, FileMode.Create, FileAccess.Write))
            {
                xlWorkBook.Write(fs);
            }
        }