1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Text;
using System.Xml;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Wordprocessing;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
byte[] templateBytes = System.IO.File.ReadAllBytes(@"c:\IS\testtemplate2.dotx");
using (MemoryStream templateStream = new MemoryStream())
{
templateStream.Write(templateBytes, 0, (int)templateBytes.Length);
using (WordprocessingDocument outDoc = WordprocessingDocument.Open(templateStream, true))
{
MainDocumentPart mainPart = outDoc.MainDocumentPart;
foreach (SdtElement sdt in mainPart.Document.Descendants<SdtElement>().ToList())
{
SdtAlias alias = sdt.Descendants<SdtAlias>().FirstOrDefault();
if (alias != null)
{
string sdtTitle = alias.Val.Value;
switch (sdtTitle)
{
case "Client:name":
Console.WriteLine("bingo name");
break;
case "Client:Adresse":
Console.WriteLine("bingo adresse");
break;
}
}
}
outDoc.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
}
using (FileStream fileStream = new FileStream(@"c:\IS\result.docx", System.IO.FileMode.Append))
{
templateStream.WriteTo(fileStream);
}
}
}
}
} |
Partager