示例#1
0
        private int PerformTemplate(TemplateNode template, XmlDocument xml_doc, ExcelDocument xlsx_doc, int offset)
        {
            xlsx_doc.RestartRows();
            xlsx_doc.AddRow();

            List <string> columns = new List <string>();

            var nodes = xml_doc.SelectNodes(template.Path);

            for (int i = 0; i < nodes.Count; i++)
            {
                var node = nodes[i];

                var values = template.Apply(node);

                if (values == null)
                {
                    continue;
                }

                List <string> row = new List <string>(columns.Count);
                foreach (var value in values)
                {
                    int index = columns.IndexOf(value.Key);
                    if (index < 0)
                    {
                        index = columns.Count;
                        xlsx_doc.AddColumn(offset + columns.Count);
                        columns.Add(value.Key);
                    }
                    while (row.Count <= index)
                    {
                        row.Add("");
                    }
                    row[index] = value.Value;
                }
                while (row.Count < columns.Count)
                {
                    row.Add("");
                }
                xlsx_doc.AddRow(offset, row);

                int progress = (i + 1) * 100 / nodes.Count;
                if (Progress != progress)
                {
                    Progress = progress;
                    ProgressChanged?.Invoke(this, EventArgs.Empty);
                }
            }

            for (int i = 0; i < columns.Count; i++)
            {
                xlsx_doc[offset + i, 0] = columns[i];
            }

            return(columns.Count);
        }
示例#2
0
        public bool Convert()
        {
            Progress = 0;
            var xml_doc = new XmlDocument();

            xml_doc.Load(XMLPath);

            using (var xlsx_doc = new ExcelDocument())
            {
                int offset = 0;
                for (int i = 0; i < Templates.Count; i++)
                {
                    Message = "Template: " + (i + 1) + " / " + Templates.Count;

                    var template = Templates[i];

                    offset += PerformTemplate(template, xml_doc, xlsx_doc, offset);
                }

                return(xlsx_doc.Save(XLSXPath));
            }
        }
示例#3
0
        public void Download()
        {
            using (var Document = new ExcelDocument(XLSXPath))
            {
                Document.ColumnRow        = CRR;
                Document.ImageColumnWidth = ICWR;

                Downloading = "";
                Progress    = 0;

                int total_rows    = Document.RowCount;
                int total_columns = Document.ColumnCount;

                List <int> column_indexes = new List <int>();
                for (int i = 0; i < total_columns; i++)
                {
                    if (Columns.Contains(Document[i, 0]))
                    {
                        column_indexes.Add(i);
                    }
                }

                int total_images  = total_rows * column_indexes.Count;
                int current_count = 0;

                for (int y = 1; y < total_rows; y++)
                {
                    foreach (int x in column_indexes)
                    {
                        string url = Document[x, y];
                        if (url.Length > 0)
                        {
                            url         = Prefix + url;
                            Downloading = url;
                            ProgressChanged?.Invoke(this, EventArgs.Empty);

                            string image_path = Path.GetFullPath("image_buffer" + Path.GetExtension(url));
                            if (SaveImage(url, image_path))
                            {
                                Document.AddImage(x, y, image_path);
                                Document[x, y] = "";
                            }
                        }

                        current_count++;
                        int progress = current_count * 100 / total_images;
                        if (progress > Progress)
                        {
                            Progress = progress;
                            ProgressChanged?.Invoke(this, EventArgs.Empty);
                        }

                        var event_args = new CancelEventArgs();
                        Cancel?.Invoke(this, event_args);
                        if (event_args.Cancel)
                        {
                            Document.Save();
                            return;
                        }
                    }
                }

                Document.Save();
            }
        }