Exemple #1
0
        public void GenratepptxUsingExistingPpt(string sText)
        {
            try
            {
                IPresentation pptxDoc     = Presentation.Open("C:\\Project-Result\\PPTx-Project\\PPTCreationApp\\DataFile\\DemoPPT.pptx");
                ILayoutSlide  layoutSlide = pptxDoc.Masters[1].LayoutSlides.Add(SlideLayoutType.Blank, "CustomLayout");
                IShape        shape       = layoutSlide.Shapes.AddShape(AutoShapeType.Diamond, 30, 20, 400, 300);
                layoutSlide.Background.Fill.SolidFill.Color = ColorObject.FromArgb(78, 89, 90);
                ISlide slide = pptxDoc.Slides.Add(layoutSlide);
                ITable table = slide.Shapes.AddTable(1, 3, 100, 120, 600, 50);

                Syncfusion.Drawing.Image image = Syncfusion.Drawing.Image.FromFile("C:\\Project-Result\\PPTx-Project\\PPTCreationApp\\DataFile\\Ninja.jpg");

                ICell cell1 = table.Rows[0].Cells[0] as ICell;
                cell1.Fill.FillType = FillType.Picture;
                cell1.Fill.PictureFill.ImageBytes = image.ImageData;

                ICell cell2 = table.Rows[0].Cells[1] as ICell;
                cell2.Fill.FillType = FillType.Solid;
                cell2.Fill.SolidFill.Color.SystemColor = Color.BlueViolet;
                cell2.TextBody.Text = sText.ToString();

                ICell  cell3     = table.Rows[0].Cells[2] as ICell;
                Stream txtStream = File.Open("C:\\Project-Result\\PPTx-Project\\PPTCreationApp\\DataFile\\Ornament-CodeFlow.pdf", FileMode.Open);
                //Stream imageStream = File.Open("D:\\PPTxExamplesConsoleApps\\PPTConsole\\PPTCreationApp\\Images\\TextFile.png", FileMode.Open);
                byte[] file        = File.ReadAllBytes("C:\\Project-Result\\PPTx-Project\\PPTCreationApp\\DataFile\\logopdf.png");
                Stream imageStream = new MemoryStream(file);
                cell3.Fill.FillType = FillType.Picture;
                cell3.Fill.PictureFill.ImageBytes = file;



                IOleObject oleObject = slide.Shapes.AddOleObject(imageStream, "Excel.Sheet.12", txtStream);


                //Set size and position of the OLE object
                oleObject.Left   = 80;
                oleObject.Top    = 30;
                oleObject.Width  = 40;
                oleObject.Height = 30;



                int rowIndex = 0, colIndex;

                //Iterate row-wise cells and add text to it
                foreach (IRow rows in table.Rows)
                {
                    colIndex = 0;

                    foreach (ICell cell in rows.Cells)
                    {
                        //cell.TextBody.AddParagraph("(" + rowIndex.ToString() + " , " + colIndex.ToString() + ")");

                        //colIndex++;
                    }

                    rowIndex++;
                }
                pptxDoc.Save("C:\\Project-Result\\PPTx-Project\\PPTCreationApp\\DataFile\\DemoPPT.pptx");
                pptxDoc.Close();
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public async Task <IActionResult> mamxl(int id)
        {
            var user = await _userManager.FindByNameAsync(User.Identity.Name);

            var items = _context.MamreqDetails.Include(mamStock => mamStock.R).Where(m => m.Rid.Equals(id) && (m.R.UserName.Equals(User.Identity.Name) || m.R.Tenant.Equals(user.TenantId))).Include(m => m.SId).AsNoTracking()
                        .Select(m => new mamxlVm()
            {
                item          = m.SId.Item,
                buffer        = m.SId.Buffer,
                formname      = m.FormName,
                benefactories = m.NoOfBenificiaries.GetValueOrDefault(),
                zarib         = m.SId.Zarib,
                adj           = m.Adjustment.GetValueOrDefault(),
                balance       = m.CurrentBalance.GetValueOrDefault(),
                comment       = m.AdjustmentComment,
            }).ToList();
            var report = _context.Mamreq.Where(m => m.Rid.Equals(id) && (m.UserName.Equals(User.Identity.Name) || m.Tenant.Equals(user.TenantId))).SingleOrDefault();


            if (!items.Any())
            {
                return(BadRequest());
            }

            ExcelEngine excelEngine = new ExcelEngine();

            IApplication application = excelEngine.Excel;

            application.DefaultVersion = ExcelVersion.Excel2013;

            IWorkbook workbook = application.Workbooks.Create(1);

            IWorksheet sheet = workbook.Worksheets[0];

            sheet.IsGridLinesVisible = false;
            FileStream file = new FileStream(@"Data/pic.png", FileMode.Open, FileAccess.Read);

            byte[] byteArray = new byte[file.Length];
            file.Read(byteArray, 0, (int)file.Length);
            file.Dispose();
            if (byteArray != null)
            {
                MemoryStream s = new MemoryStream(byteArray);

                Syncfusion.Drawing.Image img   = Syncfusion.Drawing.Image.FromStream(s);
                IPictureShape            shape = sheet.Pictures.AddPicture(2, 11, img);
            }

            sheet["A3:B3"].Merge();
            sheet["C3:D3"].Merge();
            sheet["A3"].Text       = "Province";
            sheet.Range["C3"].Text = report.ProvCode;
            sheet.Range["C3"].CellStyle.Font.Italic = true;



            sheet["A4:B4"].Merge();
            sheet["C4:D4"].Merge();
            sheet["A4"].Text       = "Implementing Agency";
            sheet.Range["C4"].Text = report.ImpCode;
            sheet.Range["C4"].CellStyle.Font.Italic = true;

            sheet["A5:B5"].Merge();
            sheet["C5:D5"].Merge();
            sheet["A5"].Text        = "Request Year";
            sheet.Range["C5"].Value = report.ReqYear.ToString();
            sheet.Range["C5"].CellStyle.Font.Italic = true;

            sheet["A6:B6"].Merge();
            sheet["C6:D6"].Merge();
            sheet["A6"].Text        = "Request Month";
            sheet.Range["C6"].Value = report.ReqMonth.ToString();
            sheet.Range["C6"].CellStyle.Font.Italic = true;

            sheet.Range["A3:D6"].BorderAround();
            sheet.Range["A3:D6"].BorderInside(ExcelLineStyle.Thin, ExcelKnownColors.Black);


            sheet["T4:U4"].Merge();
            sheet["V4:W4"].Merge();
            sheet["T4"].Text        = "No of months";
            sheet.Range["V4"].Value = report.Month.ToString();
            sheet.Range["V4"].CellStyle.Font.Italic = true;

            sheet["T5:U5"].Merge();
            sheet["V5:W5"].Merge();
            sheet["T5"].Text       = "Requested by";
            sheet.Range["V5"].Text = report.ReqBy;
            sheet.Range["V5"].CellStyle.Font.Italic = true;

            sheet["T6:U6"].Merge();
            sheet["V6:W6"].Merge();
            sheet["T6"].Text       = "Request Date";
            sheet.Range["V6"].Text = report.UpdateDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);;
            sheet.Range["V6"].CellStyle.Font.Italic = true;

            sheet.Range["T4:W6"].BorderAround();
            sheet.Range["T4:W6"].BorderInside(ExcelLineStyle.Thin, ExcelKnownColors.Black);



            sheet["J5:L5"].Merge();
            sheet["J5"].Text = "          Ministry of Public Health";
            sheet["I6:M6"].Merge();
            sheet["I6"].Text = "        General Directorate of Preventive Medicine";
            sheet["j7:L7"].Merge();
            sheet["j7"].Text = "        Public Nutrition Directorate ";

            sheet["j8:L8"].Merge();
            sheet["j8"].Text = "      MAM Monthly Request Form";



            sheet["A10:C10"].Merge();
            sheet["A10"].Text = "Number of Health Facilities Covered ";
            sheet["A11:B11"].Merge();
            sheet["A12:B12"].Merge();
            sheet["A11"].Text = "PH";
            sheet["A11"].CellStyle.ColorIndex = ExcelKnownColors.Aqua;
            sheet["A12"].Value = report.Ph.ToString();

            sheet["C11:D11"].Merge();
            sheet["C12:D12"].Merge();
            sheet["C11"].Text = "DH";
            sheet["C11"].CellStyle.ColorIndex = ExcelKnownColors.Aqua;
            sheet["C12"].Value = report.Dh.ToString();

            sheet["E11:F11"].Merge();
            sheet["E12:F12"].Merge();
            sheet["E11"].Text = "CHC";
            sheet["E11"].CellStyle.ColorIndex = ExcelKnownColors.Aqua;
            sheet["E12"].Value = report.Chc.ToString();

            sheet["G11:H11"].Merge();
            sheet["G12:H12"].Merge();
            sheet["G11"].Text = "BHC";
            sheet["G11"].CellStyle.ColorIndex = ExcelKnownColors.Aqua;
            sheet["G12"].Value = report.Bhc.ToString();

            sheet["I11:J11"].Merge();
            sheet["I12:J12"].Merge();
            sheet["I11"].Text = "SHC";
            sheet["I11"].CellStyle.ColorIndex = ExcelKnownColors.Aqua;
            sheet["I12"].Value = report.Shc.ToString();

            sheet["K11:L11"].Merge();
            sheet["K12:L12"].Merge();
            sheet["K11"].Text = "MHT";
            sheet["K11"].CellStyle.ColorIndex = ExcelKnownColors.Aqua;
            sheet["K12"].Value = report.Mht.ToString();

            sheet.Range["A11:L12"].BorderAround();


            sheet["A16:F16"].Merge();
            sheet["A16"].Text = "Requested supply for this period";

            sheet["A17:C17"].Merge();
            sheet["A17:C17"].CellStyle.ColorIndex = ExcelKnownColors.Aqua;
            sheet["A17"].Text = "Requested Item";

            sheet["D17:E17"].Merge();
            sheet["D17:E17"].CellStyle.ColorIndex = ExcelKnownColors.Aqua;
            sheet["D17"].Text = "Program";

            sheet["F17:H17"].Merge();
            sheet["F17:H17"].CellStyle.ColorIndex = ExcelKnownColors.Aqua;
            sheet["F17"].Text = "Balance from Last Release";

            sheet["I17:K17"].Merge();
            sheet["I17:K17"].CellStyle.ColorIndex = ExcelKnownColors.Aqua;
            sheet["I17"].Text = "# of beneficiaries";

            sheet["L17:N17"].Merge();
            sheet["L17:N17"].CellStyle.ColorIndex = ExcelKnownColors.Aqua;
            sheet["L17"].Text = "Needed Quantity for Next 1 Month";


            sheet["O17:P17"].Merge();
            sheet["O17:P17"].CellStyle.ColorIndex = ExcelKnownColors.Aqua;
            sheet["O17"].Text = "Buffer (%)";


            sheet["Q17:R17"].Merge();
            sheet["Q17:R17"].CellStyle.ColorIndex = ExcelKnownColors.Aqua;
            sheet["Q17"].Text = "Adjustment";

            sheet["S17"].CellStyle.ColorIndex = ExcelKnownColors.Aqua;
            sheet["S17"].Text = "Total Qty Needed";

            sheet["T17:U17"].CellStyle.ColorIndex = ExcelKnownColors.Aqua;
            sheet["T17"].Text = "Comment/Remarks";


            int i = 1;

            foreach (var item in items)
            {
                string c = (17 + i).ToString();
                sheet[$"A{c}:C{c}"].Merge();
                sheet[$"A{c}"].Text = item.item;

                sheet[$"D{c}:E{c}"].Merge();
                sheet[$"D{c}"].Text = item.formname;

                sheet[$"F{c}:H{c}"].Merge();
                sheet[$"F{c}"].Value = item.balance.ToString();

                sheet[$"I{c}:K{c}"].Merge();
                sheet[$"I{c}"].Value = item.benefactories.ToString();
                float needed = item.benefactories * item.zarib;

                sheet[$"L{c}:N{c}"].Merge();
                sheet[$"L{c}"].Value = (needed).ToString();

                sheet[$"O{c}:P{c}"].Merge();
                sheet[$"O{c}"].Value = "0";

                sheet[$"Q{c}:R{c}"].Merge();
                sheet[$"Q{c}"].Value = item.adj.ToString();

                sheet[$"S{c}"].Value = (needed + item.balance + item.adj).ToString();

                sheet[$"T{c}:U{c}"].Merge();
                sheet[$"T{c}"].Text = item.comment;


                i++;
            }

            sheet.SetRowHeight(17, 35);
            sheet.Range["A17:U17"].CellStyle.VerticalAlignment = ExcelVAlign.VAlignCenter;


            string rows = (items.Count() + 17).ToString();

            sheet.Range[$"A17:U{rows}"].BorderAround();
            sheet.Range[$"A17:U{rows}"].BorderInside(ExcelLineStyle.Thin, ExcelKnownColors.Black);


            string       ContentType = "Application/msexcel";
            string       filename    = "mamreq.xlsx";
            MemoryStream ms          = new MemoryStream();

            workbook.SaveAs(ms);
            ms.Position = 0;
            workbook.Close();
            excelEngine.Dispose();
            return(File(ms, ContentType, filename));
        }
        private void ExportTemplate(int fieldId, string filePath)
        {
            try
            {
                IPresentation pptxDoc = Presentation.Open(@"Templates\Report.pptx");

                Syncfusion.Drawing.Image image = null;

                List <FieldResultDetail> fieldResultDetail = fieldResultDetails.Where(p => p.RestaurantFieldId == fieldId &&
                                                                                      !string.IsNullOrWhiteSpace(p.AttachPath)).ToList();
                //Adds a blank slide to Presentation
                int    idxImg    = 0;
                var    field     = fieldResults.FirstOrDefault(p => p.RestaurantFieldId == fieldId);
                string shiftWork = "C";
                if (field.StartTime.Hour < 12)
                {
                    shiftWork = "S";
                }
                for (int idx = 1; idx < pptxDoc.Slides.Count; idx++)
                {
                    ISlide itmSlide = pptxDoc.Slides[idx];
                    if (idx < pptxDoc.Slides.Count - 1)
                    {
                        //Adds normal shape to slide
                        foreach (IShape itmShap in itmSlide.Shapes.OrderBy(p => p.ShapeName))
                        {
                            if (itmShap.ShapeName.Contains("header"))
                            {
                                //xu ly replace
                                ITextBody textBody = itmShap.TextBody;
                                textBody.Text = textBody.Text.Replace("Localtion", field.LocationName);
                                textBody.Text = textBody.Text.Replace("ActionDate", field.StartTime.ToString("yyyy-MM-dd"));
                                if (field.StartTime.Hour < 13)
                                {
                                    textBody.Text = textBody.Text.Replace("Shift", "Ca Sáng");
                                }
                                else
                                {
                                    textBody.Text = textBody.Text.Replace("Shift", "Ca Chiều");
                                }

                                textBody.Text = textBody.Text.Replace("WorkingTime", $"{field.StartTime.ToString("HH:mm")} - {field.StartTime.ToString("HH:mm")}");
                            }
                            else
                            {
                                if (idxImg <= fieldResultDetail.Count - 1 && !string.IsNullOrEmpty(fieldResultDetail[idxImg].AttachPath))
                                {
                                    using (Stream stream = new FileStream(fieldResultDetail[idxImg].AttachPath, FileMode.Open))
                                    {
                                        image = Syncfusion.Drawing.Image.FromStream(stream);
                                    }
                                    IFill lineFormat = itmShap.Fill;
                                    lineFormat.FillType = FillType.Picture;
                                    lineFormat.PictureFill.ImageBytes = image.ImageData;
                                }
                                else
                                {
                                    break;
                                }
                                idxImg++;
                            }
                        }
                    }
                    else
                    {
                        //lay duong dan folder.
                        //search image
                        string   folder      = fieldResultDetail[0].AttachPath.Substring(0, fieldResultDetail[0].AttachPath.LastIndexOf(@"\"));
                        string[] fileResults = Directory.GetFiles(folder, $"ketqua_{shiftWork}*");
                        idxImg = 0;
                        foreach (IShape itmShap in itmSlide.Shapes.OrderBy(p => p.ShapeName))
                        {
                            if (itmShap.ShapeName.Contains("header"))
                            {
                                ITextBody textBody = itmShap.TextBody;
                                textBody.Text = textBody.Text.Replace("Localtion", field.LocationName);
                                textBody.Text = textBody.Text.Replace("ActionDate", field.StartTime.ToString("yyyy-MM-dd"));
                                if (field.StartTime.Hour < 13)
                                {
                                    textBody.Text = textBody.Text.Replace("Shift", "Ca Sáng");
                                }
                                else
                                {
                                    textBody.Text = textBody.Text.Replace("Shift", "Ca Chiều");
                                }
                                textBody.Text = textBody.Text.Replace("WorkingTime", $"{field.StartTime.ToString("HH:mm")} - {field.StartTime.ToString("HH:mm")}");

                                //xu ly replace
                            }
                            else
                            {
                                using (Stream stream = new FileStream(fileResults[idxImg], FileMode.Open))
                                {
                                    image = Syncfusion.Drawing.Image.FromStream(stream);
                                }
                                IFill lineFormat = itmShap.Fill;
                                lineFormat.FillType = FillType.Picture;
                                lineFormat.PictureFill.ImageBytes = image.ImageData;
                                idxImg++;
                            }
                        }
                    }
                }

                pptxDoc.Save(filePath);
                pptxDoc.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }