コード例 #1
0
        public void Test1()
        {
            var          resourcePath = "dmnClient.Test.TestData.dmnTest1.xlsx";
            var          assembly     = Assembly.GetExecutingAssembly();
            ExcelPackage ep;
            var          name = string.Empty;

            using (Stream resourceAsStream = assembly.GetManifestResourceStream(resourcePath))
            {
                ep = new ExcelPackage(resourceAsStream);
            }

            //var file = "dmnTest1.xlsx";
            //var savedFilePath = Path.Combine(Directory.GetCurrentDirectory() + @"..\..\..\..\TestData\", file);
            ////var savedFilePath = @"C:\Users\MatiasGonzalezTognon\Dropbox (Arkitektum AS)\Ark_prosjekter\DIBK\DigiTek\Brannteknisk prosjektering\DMN\Trinn1\ExcelFiles\13_OverflateKledning.xlsx";
            //var name = Path.GetFileNameWithoutExtension(savedFilePath);
            //var fi = new FileInfo(savedFilePath);
            //ExcelPackage ep = new ExcelPackage(new FileInfo(savedFilePath));

            ExcelWorksheet workSheet = ep.Workbook.Worksheets.FirstOrDefault();
            var            table     = ep.Workbook.Worksheets.FirstOrDefault().Tables.FirstOrDefault();

            var columnsDictionary = ExcelServices.GetColumnRagngeInLeters(table, 4, 3);

            columnsDictionary.TryGetValue("outputsIndex", out var outputsIndex);
            columnsDictionary.TryGetValue("inputsIndex", out var inputsIndex);


            var outputsRulesDictionary    = new ExcelServices().GetRulesFromExcel(workSheet, outputsIndex, true);
            var inputsRulesDictionary     = new ExcelServices().GetRulesFromExcel(workSheet, inputsIndex, true);
            var annotationRulesDictionary = new ExcelServices().GetAnnotationsRulesFromExcel(workSheet, inputsIndex, outputsIndex, true);
            var outputsRulesTypes         = new ExcelServices().GetRulesTypes(workSheet, outputsIndex, true);
            var inputsRulesTypes          = new ExcelServices().GetRulesTypes(workSheet, inputsIndex, true);
            var inputsDictionary          = new ExcelServices().GetExcelHeaderName(workSheet, inputsIndex, true);
            var outputsDictionary         = new ExcelServices().GetExcelHeaderName(workSheet, outputsIndex, true);
            var dmnInfo = new ExcelServices().GetDmnInfo(workSheet).FirstOrDefault();
            var dmnName = dmnInfo.Value;
            var dmnId   = dmnInfo.Key;


            var newDmn = new DmnV1Builder()
                         .AddDefinitionsInfo("Excel2Dmn_" + DateTime.Now.ToString("dd-mm-yy"), name)
                         .AddDecision(dmnId, dmnName, "decisionTable")
                         .AddInputsToDecisionTable(inputsDictionary, inputsRulesTypes)
                         .AddOutputsToDecisionTable(outputsDictionary, outputsRulesTypes)
                         .AddDecisionRules(inputsRulesDictionary, outputsRulesDictionary, annotationRulesDictionary)
                         .Build();

            var           dmnFile = string.Concat(@"c:\temp\", name, "_", ".dmn");
            XmlSerializer xs      = new XmlSerializer(typeof(tDefinitions));
            TextWriter    tw      = new StreamWriter(dmnFile);

            xs.Serialize(tw, newDmn);

            File.Exists(dmnFile).Should().BeTrue();
        }
コード例 #2
0
ファイル: DmnController.cs プロジェクト: schifflee/DmnClient
        public ActionResult <string> PostExcelToDmn(string inputs, string outputs, bool haveId)
        {
            var httpRequest              = HttpContext.Request;
            var responsDictionary        = new Dictionary <string, string>();
            HttpResponseMessage response = null;

            if (httpRequest.Form.Files.Count != 1)
            {
                return(BadRequest(new Dictionary <string, string>()
                {
                    { "Error:", "Not file fount" }
                }));
            }
            var            file      = httpRequest.Form.Files[0];
            var            file1     = httpRequest.Form.Files.FirstOrDefault();
            ExcelPackage   ep        = null;
            ExcelWorksheet workSheet = null;
            ExcelTable     table     = null;

            if (file != null)
            {
                try
                {
                    //Open Excel file
                    using (Stream excelFile = file.OpenReadStream())
                    {
                        ep = new ExcelPackage(excelFile);
                    }

                    workSheet = ep.Workbook.Worksheets.FirstOrDefault();
                    table     = workSheet.Tables.FirstOrDefault();
                }
                catch (Exception e)
                {
                    return(BadRequest(new Dictionary <string, string>()
                    {
                        { "Error:", "Can't Open Excel File" }
                    }));
                }

                if (table != null)
                {
                    Dictionary <int, Dictionary <string, object> > outputsRulesFromExcel = null;
                    Dictionary <int, Dictionary <string, object> > inputsRulsFromExcel   = null;
                    Dictionary <int, string> annotationsRulesDictionary = null;

                    Dictionary <int, string> outputsRulesTypes = null;
                    Dictionary <int, string> inputsRulesTypes  = null;
                    Dictionary <string, Dictionary <string, string> > inputsDictionary  = null;
                    Dictionary <string, Dictionary <string, string> > outputsDictionary = null;
                    var dmnName = string.Empty;
                    var dmnId   = string.Empty;

                    var      columnsDictionary = GetTablesIndex(table, inputs, outputs);
                    string[] outputsIndex      = null;
                    string[] inputsIndex       = null;
                    if (columnsDictionary != null)
                    {
                        columnsDictionary.TryGetValue("outputsIndex", out outputsIndex);
                        columnsDictionary.TryGetValue("inputsIndex", out inputsIndex);
                    }

                    if (inputsIndex == null && outputsIndex == null)
                    {
                        return(BadRequest(new Dictionary <string, string>()
                        {
                            { "Error", "Can't get inputs/output rows" }
                        }));
                    }

                    try
                    {
                        outputsRulesFromExcel      = new ExcelServices().GetRulesFromExcel(workSheet, outputsIndex, haveId);
                        inputsRulsFromExcel        = new ExcelServices().GetRulesFromExcel(workSheet, inputsIndex, haveId);
                        annotationsRulesDictionary = new ExcelServices().GetAnnotationsRulesFromExcel(workSheet, inputsIndex, outputsIndex, haveId);
                        outputsRulesTypes          = new ExcelServices().GetRulesTypes(workSheet, outputsIndex, haveId);
                        inputsRulesTypes           = new ExcelServices().GetRulesTypes(workSheet, inputsIndex, haveId);
                        inputsDictionary           = new ExcelServices().GetExcelHeaderName(workSheet, inputsIndex, haveId);
                        outputsDictionary          = new ExcelServices().GetExcelHeaderName(workSheet, outputsIndex, haveId);
                        var dmnInfo = new ExcelServices().GetDmnInfo(workSheet).FirstOrDefault();
                        dmnName = dmnInfo.Value;
                        dmnId   = dmnInfo.Key;
                        if (!outputsRulesFromExcel.Any() || !inputsRulsFromExcel.Any() ||
                            !outputsRulesTypes.Any() || !inputsRulesTypes.Any() || !inputsDictionary.Any() ||
                            !outputsDictionary.Any())
                        {
                            return(BadRequest(new Dictionary <string, string>()
                            {
                                { "Error:", "Wrong information to create DMN from Excel" }
                            }));
                        }
                    }
                    catch (Exception)
                    {
                        return(BadRequest(new Dictionary <string, string>()
                        {
                            { "Error:", "Can't Get Excel info" }
                        }));
                    }

                    var filename = Path.GetFileNameWithoutExtension(file.FileName);
                    var newDmn   = new DmnV1Builder()
                                   .AddDefinitionsInfo("Excel2Dmn_" + DateTime.Now.ToString("dd-mm-yy"), filename)
                                   .AddDecision(dmnId, dmnName, "decisionTable")
                                   .AddInputsToDecisionTable(inputsDictionary, inputsRulesTypes)
                                   .AddOutputsToDecisionTable(outputsDictionary, outputsRulesTypes)
                                   .AddDecisionRules(inputsRulsFromExcel, outputsRulesFromExcel, annotationsRulesDictionary)
                                   .Build();
                    // Save DMN
                    try
                    {
                        var path = Path.Combine(@"C:\", "ExcelToDmn");

                        Directory.CreateDirectory(path);


                        //var dmnFile = string.Concat(path, filename, "_Exc2Dmn", ".dmn");
                        XmlSerializer xs      = new XmlSerializer(typeof(DecisionModelNotation.Shema.tDefinitions));
                        var           combine = Path.Combine(path, string.Concat(filename, ".dmn"));
                        using (TextWriter tw = new StreamWriter(combine))
                        {
                            xs.Serialize(tw, newDmn);
                        }

                        return(Ok(new Dictionary <string, string>()
                        {
                            { filename + ".dmn", "Created" }, { "Path", combine }
                        }));
                    }
                    catch (Exception e)
                    {
                        return(BadRequest(new Dictionary <string, string>()
                        {
                            { filename + ".dmn", "Can't be safe" }
                        }));
                    }
                }
                return(BadRequest(new Dictionary <string, string>()
                {
                    { file.FileName, "Excel file don't have table" }
                }));
            }
            return(Ok(responsDictionary));
        }
コード例 #3
0
        public IActionResult PostExcelToDmn(string inputs, string outputs, bool haveId)
        {
            var httpRequest = HttpContext.Request;

            if (httpRequest.Form.Files.Count != 1)
            {
                return(BadRequest(new Dictionary <string, string>()
                {
                    { "Error:", "Not file fount" }
                }));
            }
            var file = httpRequest.Form.Files[0];

            if (file != null)
            {
                ExcelPackage ep;
                try
                {
                    //Open Excel file
                    using (Stream excelFile = file.OpenReadStream())
                    {
                        ep = new ExcelPackage(excelFile);
                    }
                }
                catch (Exception e)
                {
                    return(BadRequest(new Dictionary <string, string>()
                    {
                        { "Error:", "Can't Open Excel File" }
                    }));
                }

                string dmnName;
                string dmnId;

                Dictionary <int, Dictionary <string, object> > inputsRulesFromExcel;
                Dictionary <int, Dictionary <string, object> > outputsRulesFromExcel;
                Dictionary <int, string> annotationsRulesDictionary;

                Dictionary <int, string> outputsRulesTypes;
                Dictionary <int, string> inputsRulesTypes;
                Dictionary <string, Dictionary <string, string> > inputsDictionary;
                Dictionary <string, Dictionary <string, string> > outputsDictionary;

                using (ExcelWorksheet worksheet = ep.Workbook.Worksheets.FirstOrDefault())
                {
                    if (worksheet == null)
                    {
                        return(BadRequest(new Dictionary <string, string>()
                        {
                            { file.FileName, "Can't find Excelsheet" }
                        }));
                    }

                    var table = worksheet.Tables.FirstOrDefault();

                    if (table == null)
                    {
                        return(BadRequest(new Dictionary <string, string>()
                        {
                            { file.FileName, "Excel file don't have a table" }
                        }));
                    }

                    //Fix cell were to set the information for the DMN table
                    dmnName = (string)(worksheet.Cells["C1"].Value ?? "DMN Table Name");
                    dmnId   = (string)(worksheet.Cells["C2"].Value ?? "dmnId");



                    var      columnsDictionary = GetTablesIndex(table, inputs, outputs);
                    string[] outputsIndex      = null;
                    string[] inputsIndex       = null;
                    if (columnsDictionary != null)
                    {
                        columnsDictionary.TryGetValue("outputsIndex", out outputsIndex);
                        columnsDictionary.TryGetValue("inputsIndex", out inputsIndex);
                    }

                    if (inputsIndex == null && outputsIndex == null)
                    {
                        return(BadRequest(new Dictionary <string, string>()
                        {
                            { "Error", "Can't get inputs/output rows" }
                        }));
                    }

                    try
                    {
                        inputsRulesFromExcel       = ExcelConverter.GetTableCellsAdressAndValue(worksheet, inputsIndex, haveId);
                        outputsRulesFromExcel      = ExcelConverter.GetTableCellsAdressAndValue(worksheet, outputsIndex, haveId);
                        annotationsRulesDictionary = ExcelConverter.GetTableAnnotationsCellsValue(worksheet, inputsIndex, outputsIndex, haveId);

                        inputsRulesTypes  = ExcelConverter.GetTableColumnsType(worksheet, inputsIndex, haveId);
                        outputsRulesTypes = ExcelConverter.GetTableColumnsType(worksheet, outputsIndex, haveId);

                        inputsDictionary  = ExcelConverter.GetTableHeader(worksheet, inputsIndex, haveId);
                        outputsDictionary = ExcelConverter.GetTableHeader(worksheet, outputsIndex, haveId);

                        if (!outputsRulesFromExcel.Any() || !inputsRulesFromExcel.Any() ||
                            !outputsRulesTypes.Any() || !inputsRulesTypes.Any() || !inputsDictionary.Any() ||
                            !outputsDictionary.Any())
                        {
                            return(BadRequest(new Dictionary <string, string>()
                            {
                                { "Error:", "Wrong information to create DMN from Excel" }
                            }));
                        }
                    }
                    catch (Exception e)
                    {
                        return(BadRequest(new Dictionary <string, string>()
                        {
                            { "Error:", "Can't Get Excel info" }
                        }));
                    }
                }



                var filename = Path.GetFileNameWithoutExtension(file.FileName);
                var newDmn   = new DmnV1Builder()
                               .AddDefinitionsInfo("Excel2Dmn_" + DateTime.Now.ToString("dd-mm-yy"), filename)
                               .AddDecision(dmnId, dmnName, "decisionTable")
                               .AddInputsToDecisionTable(inputsDictionary, inputsRulesTypes)
                               .AddOutputsToDecisionTable(outputsDictionary, outputsRulesTypes)
                               .AddDecisionRules(inputsRulesFromExcel, outputsRulesFromExcel, annotationsRulesDictionary)
                               .Build();

                // Create DMN Stream response
                try
                {
                    MemoryStream stream = new MemoryStream();
                    StreamWriter sw     = new StreamWriter(stream, Encoding.UTF8);

                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(DecisionModelNotation.Shema.tDefinitions));
                    xmlSerializer.Serialize(sw, newDmn);

                    stream.Position = 0;
                    return(File(stream, "aplication/dmn", $"{filename}.dmn"));
                }
                catch (Exception e)
                {
                    return(BadRequest(new Dictionary <string, string>()
                    {
                        { filename + ".dmn", "Unable Stream dmn file" }
                    }));
                }
            }
            else
            {
                return(BadRequest(new Dictionary <string, string>()
                {
                    { "Error", "Can't find any file" }
                }));
            }
        }