Exemple #1
0
        public void Test1()
        {
            var          file        = "dmnTest1.dmn";
            string       ifcDataFile = Path.Combine(Directory.GetCurrentDirectory() + @"..\..\..\..\Data\", file);
            tDefinitions dmn;

            using (Stream dmnStream = File.Open(ifcDataFile, FileMode.Open))
            {
                dmn = DmnConverter.DeserializeStreamDmnFile(dmnStream);
            }

            var Items    = dmn.Items;
            var decision = Items.Where(t => t.GetType() == typeof(tDecision));

            var excelPkg = new ExcelPackage();

            foreach (var tdecision in decision)
            {
                tDecisionTable decisionTable = null;
                try
                {
                    var dt = ((tDecision)tdecision).Item;
                    decisionTable = (tDecisionTable)Convert.ChangeType(dt, typeof(tDecisionTable));
                    ExcelWorksheet wsSheet = excelPkg.Workbook.Worksheets.Add(tdecision.id);
                    //Add Table Title
                    ExcelConverter.AddTableTitle(tdecision.name, wsSheet, decisionTable.hitPolicy.ToString(), tdecision.id);
                    // Add "input" and "output" headet to Excel table
                    ExcelConverter.AddTableInputOutputTitle(wsSheet, decisionTable);
                    //Add DMN Table to excel Sheet
                    ExcelConverter.CreateExcelTableFromDecisionTable(decisionTable, wsSheet, tdecision.id);
                }
                catch
                {
                    //
                }
            }

            var filename = Path.GetFileNameWithoutExtension(ifcDataFile);
            var path     = string.Concat(@"c:\temp\");

            Directory.CreateDirectory(path);
            var filePath = Path.Combine(path, string.Concat(filename, "new1", ".xlsx"));

            excelPkg?.SaveAs(new FileInfo(filePath));

            File.Exists(filePath).Should().BeTrue();
        }
Exemple #2
0
        public IActionResult PosDmnToExcel()
        {
            var httpRequest = HttpContext.Request;

            var httpFiles       = httpRequest.Form.Files;
            var errorDictionary = new Dictionary <string, string>();

            if (httpFiles == null && !httpFiles.Any())
            {
                return(NotFound("Can't find any file"));
            }

            if (httpFiles.Count == 1)
            {
                var file = httpFiles.FirstOrDefault();

                tDefinitions dmn = null;

                //Deserialize DMN file
                if (file != null)
                {
                    using (Stream dmnfile = file.OpenReadStream())
                    {
                        dmn = DmnConverter.DeserializeStreamDmnFile(dmnfile);
                    }
                }
                if (dmn == null)
                {
                    if (file != null)
                    {
                        return(BadRequest(new Dictionary <string, string>()
                        {
                            { file.FileName + ".dmn", "Can't Deserialize DMN file" }
                        }));
                    }
                }

                // check if DMN have desicion table
                var items        = dmn.Items;
                var decision     = items.Where(t => t.GetType() == typeof(tDecision));
                var tDrgElements = decision as tDRGElement[] ?? decision.ToArray();
                if (!tDrgElements.Any())
                {
                    if (file != null)
                    {
                        return(BadRequest(new Dictionary <string, string>()
                        {
                            { file.FileName + ".dmn", "Dmn file have non Decision tables" }
                        }));
                    }
                }

                // create Excel Package
                var excelPkg = new ExcelPackage();
                foreach (var tdecision in tDrgElements)
                {
                    try
                    {
                        var            dt            = ((tDecision)tdecision).Item;
                        var            decisionTable = (tDecisionTable)Convert.ChangeType(dt, typeof(tDecisionTable));
                        ExcelWorksheet wsSheet       = excelPkg.Workbook.Worksheets.Add(tdecision.id);
                        //Add Table Title
                        ExcelConverter.AddTableTitle(tdecision.name, wsSheet, decisionTable.hitPolicy.ToString(), tdecision.id);
                        // Add "input" and "output" headet to Excel table
                        ExcelConverter.AddTableInputOutputTitle(wsSheet, decisionTable);
                        //Add DMN Table to excel Sheet
                        ExcelConverter.CreateExcelTableFromDecisionTable(decisionTable, wsSheet, tdecision.id);
                        wsSheet.Protection.IsProtected = true;
                    }
                    catch
                    {
                        if (file != null)
                        {
                            return(BadRequest(new Dictionary <string, string>()
                            {
                                { file.FileName + ".dmn", "Can't be create ExcelPackage" }
                            }));
                        }
                    }
                }

                // Create Excel Stream response
                try
                {
                    if (file != null)
                    {
                        var filename = Path.GetFileNameWithoutExtension(file.FileName);
                        excelPkg.Save();
                        var fileStream = excelPkg.Stream;
                        fileStream.Flush();
                        fileStream.Position = 0;

                        return(File(fileStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", $"{filename}.xlsx"));
                    }
                }
                catch
                {
                    if (file != null)
                    {
                        errorDictionary.Add(file.FileName, "Can't create excel Stream response");
                    }
                }
            }
            else
            {
                return(BadRequest(new Dictionary <string, string>()
                {
                    { "Error", "Can't convert more than one file." }
                }));
            }

            return(Ok(new Dictionary <string, string>()
            {
                { "Error", "No data to process." }
            }));
        }