Ejemplo n.º 1
0
        public FIS_Export(SharedClasses.DB.DB_Connector connection)
        {
            #region Components
            InitializeComponent();

            cbAddress.Items.AddRange(Properties.Settings.Default.FIS_Addresses.Cast <string>().ToArray());
            cbAddress.SelectedIndex = 0;

            tbXSD_Path.Text = Properties.Settings.Default.FIS_XSD_Path;
            #endregion

            _DB_Connection = connection;
        }
Ejemplo n.º 2
0
            public static void CreateFromTemplate(SharedClasses.DB.DB_Connector connection, Dictionary <string, Font> fonts, XElement excelTemplateElement, uint[] ids, string resultFile)
            {
                List <string>                         colNames;
                Dictionary <byte, ushort>             colWidths;
                List <System.Tuple <string, string> > colFonts;

                GetTableFormatting(excelTemplateElement, out colNames, out colWidths, out colFonts);

                List <object[]> rows;

                if (excelTemplateElement.Element("Placeholder") != null)
                {
                    foreach (XElement column in excelTemplateElement.Element("Structure").Elements())
                    {
                        if (column.Element("Placeholder") != null)
                        {
                            throw new System.Exception("Нельзя задавать плейсхолдеры для отдельных столбцов, если задан табличный плейсхолдер. Значение: " + column.Element("Placeholder").Value);
                        }
                    }

                    rows = connection.CallProcedure(_PH_Table[excelTemplateElement.Element("Placeholder").Value], new Dictionary <string, object> {
                        { "id", ids[0] }
                    });
                }
                else
                {
                    List <string> placeholders     = new List <string>();
                    string        placeholderGroup = null;

                    foreach (XElement column in excelTemplateElement.Element("Structure").Elements())
                    {
                        string placeholder = column.Element("Placeholder").Value;
                        if (placeholderGroup == null)
                        {
                            placeholderGroup = GetPlaceholderGroup(placeholder);
                        }
                        else
                        if (placeholderGroup != GetPlaceholderGroup(placeholder))
                        {
                            throw new System.Exception("В одном шаблоне не могут использоваться плейсхолдеры из разных групп. Конфликтная группа: " + GetPlaceholderGroup(placeholder));
                        }

                        placeholders.Add(placeholder);
                    }

                    rows = new List <object[]>();
                    foreach (uint id in ids)
                    {
                        rows.Add(new object[placeholders.Count]);
                        for (byte i = 0; i < placeholders.Count; ++i)
                        {
                            rows[rows.Count - 1][i] = SelectByPlaceholder(connection, id, placeholders[i]);
                        }
                    }
                }

                if (bool.Parse(excelTemplateElement.Element("Numeration").Value))
                {
                    for (byte i = 0; i < rows.Count; ++i)
                    {
                        object[] buf = new object[rows[i].Length + 1];
                        buf[0] = i + 1;
                        for (byte j = 0; j < rows[i].Length; ++j)
                        {
                            buf[j + 1] = rows[i][j];
                        }
                    }
                }

                Create(colNames, colWidths, fonts, colFonts, rows, resultFile);
            }
Ejemplo n.º 3
0
            private static DocX Create(Dictionary <string, Font> fonts, XElement wordTemplateElement, SharedClasses.DB.DB_Connector connection, uint?id, string[] singleParams, IEnumerable <string[]>[] tableParams, string resultFile)
            {
                DocX doc = DocX.Create(resultFile + ".docx");

                AddCoreProperties(doc);

                string placeholderGroup = null;

                if (wordTemplateElement.Element("Properties") != null)
                {
                    ApplyProperties(doc, wordTemplateElement.Element("Properties"), fonts, connection, id, ref placeholderGroup, singleParams);
                }

                foreach (XElement element in wordTemplateElement.Element("Structure").Elements())
                {
                    if (element.Element("Paragraph") != null)
                    {
                        MakeParagraph(element.Element("Paragraph"), doc.InsertParagraph(), fonts, connection, id, ref placeholderGroup, singleParams);
                    }
                    else if (element.Element("Table") != null)
                    {
                        AddTable(
                            doc,
                            element.Element("Table"),
                            fonts,
                            connection != null ? connection.CallProcedure(_PH_Table[element.Element("Table").Element("Placeholder").Value], new Dictionary <string, object> {
                            { "id", id }
                        })
                            .ConvertAll(row => System.Array.ConvertAll(row, c => c.ToString()))
                                : tableParams[int.Parse(element.Element("Table").Element("Placeholder").Value)]
                            );
                    }
                    else
                    {
                        XElement tableEl = element.Element("FixedTable");

                        MakeFixedTable(InsertFixedTable(doc, tableEl), tableEl, fonts, connection, id, ref placeholderGroup, singleParams);
                    }
                }

                return(doc);
            }
Ejemplo n.º 4
0
            private static void MakeFixedTable(Table table, XElement tableEl, Dictionary <string, Font> fonts, SharedClasses.DB.DB_Connector connection, uint?id, ref string placeholderGroup, string[] singleParams)
            {
                MakeBorders(table, tableEl.Element("Borders"));

                byte index = 0;

                foreach (XElement row in tableEl.Element("Rows").Elements())
                {
                    if (row.Element("Merges") != null)
                    {
                        foreach (XElement merge in row.Element("Merges").Elements())
                        {
                            table.Rows[index].MergeCells(
                                int.Parse(merge.Element("StartColumn").Value),
                                int.Parse(merge.Element("EndColumn").Value)
                                );
                        }
                    }

                    foreach (Cell cell in table.Rows[index].Cells)
                    {
                        while (cell.RemoveParagraphAt(1))
                        {
                            ;
                        }
                    }

                    foreach (XElement cellEl in row.Element("Cells").Elements())
                    {
                        Cell cell = table.Rows[index].Cells[int.Parse(cellEl.Attribute("Column").Value)];

                        if (cellEl.Element("VerticalAlignment") != null)
                        {
                            cell.VerticalAlignment = _VerticalAlignments[cellEl.Element("VerticalAlignment").Value];
                        }

                        MakeBorders(cell, cellEl.Element("Borders"));

                        if (cellEl.Element("Paragraphs") != null)
                        {
                            cell.RemoveParagraphAt(0);
                            foreach (XElement paragraph in cellEl.Element("Paragraphs").Elements())
                            {
                                MakeParagraph(paragraph, cell.InsertParagraph(), fonts, connection, id, ref placeholderGroup, singleParams);
                            }
                        }
                    }

                    if (row.Element("Height") != null)
                    {
                        table.Rows[index].Height = ushort.Parse(row.Element("Height").Value);
                    }

                    index++;
                }

                if (tableEl.Element("AutoFit") != null)
                {
                    table.AutoFit = _AutoFits[tableEl.Element("AutoFit").Value];
                }
            }
Ejemplo n.º 5
0
 public static DocX CreateFromTemplate(SharedClasses.DB.DB_Connector connection, Dictionary <string, Font> fonts, XElement wordTemplateElement, uint id, string resultFile)
 {
     return(Create(fonts, wordTemplateElement, connection, id, null, null, resultFile));
 }
Ejemplo n.º 6
0
            private static void MakeParagraph(XElement parElem, Paragraph paragraph, Dictionary <string, Font> fonts, SharedClasses.DB.DB_Connector connection, uint?id, ref string placeholderGroup, string[] singleParams)
            {
                if (parElem.Element("Alighment") != null)
                {
                    paragraph.Alignment = _Alignments[parElem.Element("Alighment").Value];
                }

                string parFontID = parElem.Element("FontID")?.Value;

                if (parElem.Element("Parts") != null)
                {
                    foreach (XElement part in parElem.Element("Parts").Elements())
                    {
                        XElement text = part.Element("Text");

                        if (text.Element("String") != null)
                        {
                            paragraph.Append(text.Element("String").Value);
                        }
                        else if (connection != null)
                        {
                            string placeholder = text.Element("Placeholder").Value;

                            if (_PH_SingleSpecial.ContainsKey(placeholder))
                            {
                                paragraph.Append(_PH_SingleSpecial[placeholder]());
                            }
                            else
                            {
                                if (placeholderGroup == null)
                                {
                                    placeholderGroup = GetPlaceholderGroup(placeholder);
                                }
                                else
                                if (placeholderGroup != GetPlaceholderGroup(placeholder))
                                {
                                    throw new System.Exception("В одном шаблоне не могут использоваться плейсхолдеры из разных групп. Конфликтная группа: " + GetPlaceholderGroup(placeholder));
                                }

                                paragraph.Append(SelectByPlaceholder(connection, id.Value, placeholder));
                            }
                        }
                        else
                        {
                            paragraph.Append(singleParams[int.Parse(text.Element("Placeholder").Value)]);
                        }

                        SetFont(paragraph, fonts, part.Element("FontID")?.Value ?? parFontID);
                    }
                }
            }
Ejemplo n.º 7
0
            private static void ApplyProperties(DocX doc, XElement properties, Dictionary <string, Font> fonts, SharedClasses.DB.DB_Connector connection, uint?id, ref string placeholderGroup, string[] singleParams)
            {
                if (properties.Element("Borders") != null)
                {
                    AddBorders(doc);
                }

                if (properties.Element("Format") != null)
                {
                    float width, height;
                    switch (properties.Element("Format").Value)
                    {
                    case "A5":
                        width  = 419.5f;
                        height = 595.2f;
                        break;

                    case "A6":
                        width  = 297.6f;
                        height = 419.5f;
                        break;

                    default:
                        throw new System.Exception("Unreachable reached.");
                    }

                    if (doc.PageLayout.Orientation == Orientation.Portrait)
                    {
                        doc.PageWidth  = width;
                        doc.PageHeight = height;
                    }
                    else
                    {
                        doc.PageHeight = width;
                        doc.PageWidth  = height;
                    }
                }

                if (properties.Element("Album") != null)
                {
                    float width  = doc.PageWidth;
                    float height = doc.PageHeight;
                    doc.PageLayout.Orientation = Orientation.Landscape;
                    doc.PageWidth  = height;
                    doc.PageHeight = width;
                }

                if (properties.Element("Margins") != null)
                {
                    XElement margins = properties.Element("Margins");

                    if (margins.Element("Left") != null)
                    {
                        doc.MarginLeft = float.Parse(margins.Element("Left").Value);
                    }
                    if (margins.Element("Top") != null)
                    {
                        doc.MarginTop = float.Parse(margins.Element("Top").Value);
                    }
                    if (margins.Element("Right") != null)
                    {
                        doc.MarginRight = float.Parse(margins.Element("Right").Value);
                    }
                    if (margins.Element("Bottom") != null)
                    {
                        doc.MarginBottom = float.Parse(margins.Element("Bottom").Value);
                    }
                }

                if (properties.Element("Headers") != null)
                {
                    doc.AddHeaders();
                    doc.Headers.first.RemoveParagraphAt(0);
                    MakeParagraph(properties.Element("Headers"), doc.Headers.first.InsertParagraph(), fonts, connection, id, ref placeholderGroup, singleParams);
                    doc.Headers.odd.RemoveParagraphAt(0);
                    MakeParagraph(properties.Element("Headers"), doc.Headers.odd.InsertParagraph(), fonts, connection, id, ref placeholderGroup, singleParams);
                    doc.Headers.even.RemoveParagraphAt(0);
                    MakeParagraph(properties.Element("Headers"), doc.Headers.even.InsertParagraph(), fonts, connection, id, ref placeholderGroup, singleParams);
                }

                if (properties.Element("PageNumeration") != null)
                {
                    doc.AddFooters();
                    doc.Footers.first.PageNumbers             = true;
                    doc.Footers.odd.PageNumbers               = true;
                    doc.Footers.even.PageNumbers              = true;
                    doc.Footers.first.Paragraphs[0].Alignment = Alignment.right;
                    doc.Footers.odd.Paragraphs[0].Alignment   = Alignment.right;
                    doc.Footers.even.Paragraphs[0].Alignment  = Alignment.right;
                }
            }