Exemple #1
0
        public Task <bool> FillAsync(ITemplate t, IDictionary <string, dynamic> stuffing, Stream output)
        {
            using (Stream docstream = new MemoryStream())
            {
                byte[] templateBytes = t.GetTemplateBytes();
                docstream.Write(templateBytes, 0, templateBytes.Length);
                docstream.Flush();
                docstream.Position = 0;
                using (WordprocessingDocument doc = WordprocessingDocument.Open(docstream, true))
                {
                    // main document part
                    if (doc.MainDocumentPart != null)
                    {
                        doPart(doc.MainDocumentPart, stuffing);
                    }

                    var headers = doc.MainDocumentPart.HeaderParts.ToList();
                    headers.ForEach(p =>
                    {
                        doPart(p, stuffing);
                    });
                    var footers = doc.MainDocumentPart.FooterParts.ToList();
                    footers.ForEach(p =>
                    {
                        doPart(p, stuffing);
                    });
                }
                docstream.Position = 0;
                XmlTemplateTool.CopyStream(docstream, output);
            }
            return(Task.FromResult <bool>(true));
        }
Exemple #2
0
 private void doPart(OpenXmlPart part, IDictionary <string, dynamic> stuffing)
 {
     try
     {
         XDocument xdoc1 = part.GetXDocument();
         foreach (KeyValuePair <string, dynamic> pair in stuffing)
         {
             XmlTemplateTool.ReplaceKey(xdoc1.Root, pair.Key, pair.Value);
         }
         part.PutXDocument(xdoc1);
     }
     catch (Exception ex) { Console.WriteLine("XmlFiller.Fill", "bad excel part", ex); }
 }
Exemple #3
0
 private void doPart(OpenXmlPart part, IDictionary <string, dynamic> stuffing)
 {
     try
     {
         XDocument xdoc1 = part.GetXDocument();
         foreach (KeyValuePair <string, dynamic> pair in stuffing)
         {
             XmlTemplateTool.ReplaceKey(xdoc1.Root, pair.Key, pair.Value);
         }
         part.PutXDocument(xdoc1);
     }
     catch (Exception ex)
     {
         //System.Diagnostics.Trace.TraceError("XmlFiller.Fill bad excel part {0}", ex);
     }
 }
Exemple #4
0
        public Task <bool> FillAsync(ITemplate t, IDictionary <string, dynamic> stuffing, Stream output)
        {
            using (Stream docstream = new MemoryStream())
            {
                byte[] templateBytes = t.GetTemplateBytes();
                docstream.Write(templateBytes, 0, templateBytes.Length);
                docstream.Flush();
                docstream.Position = 0;
                using (SpreadsheetDocument doc = SpreadsheetDocument.Open(docstream, true))
                {
                    // shared string part
                    if (doc.WorkbookPart.SharedStringTablePart != null)
                    {
                        doPart(doc.WorkbookPart.SharedStringTablePart, stuffing);
                    }
                    // some other part
                    if (doc.WorkbookPart.WorkbookStylesPart != null)
                    {
                        doPart(doc.WorkbookPart.WorkbookStylesPart, stuffing);
                    }
                    // some other part
                    if (doc.WorkbookPart.CalculationChainPart != null)
                    {
                        doPart(doc.WorkbookPart.CalculationChainPart, stuffing);
                    }

                    var worksheets = doc.WorkbookPart.WorksheetParts.ToList();
                    worksheets.ForEach(w =>
                    {
                        doPart(w, stuffing);
                    });
                }
                docstream.Position = 0;
                XmlTemplateTool.CopyStream(docstream, output);
            }
            return(Task.FromResult <bool>(true));
        }
Exemple #5
0
        public async Task <bool> FillAsync(ITemplate t, IDictionary <string, dynamic> stuffing, Stream output)
        {
            // TODO: make this more asyncy
            XmlWriter writer = XmlWriter.Create(output, new XmlWriterSettings()
            {
                Encoding = UTF8Encoding.UTF8, Indent = true, Async = true
            });

            // should only be one section
            foreach (var section in t.SectionNames)
            {
                XDocument doc;
                try { doc = XDocument.Parse(t.GetSectionText(section)); }
                catch (Exception) { continue; }
                foreach (KeyValuePair <string, dynamic> pair in stuffing)
                {
                    XmlTemplateTool.ReplaceKey(doc.Root, pair.Key, pair.Value);
                }
                doc.WriteTo(writer);
            }
            await writer.FlushAsync();

            return(true);
        }