суббота, 12 мая 2012 г.

Merge docx files using C# OOXML SDK WordprocessingDocument class

This code create one docx document from template replacing certain fields in template.


 class DocumentInfo
    {
        public string ProductName { get; set; }

        public string AWPCount { get; set; }

        public string RegNumber { get; set; }

        public string OrganizationName { get; set; }

        public string LifeTime { get; set; }
    }


public static byte[] GetDocx(string templatePath, DocumentInfo[] documentInfos)
        {
            if (documentInfos.Length == 0)
            {
                throw new Exception("documentInfos count = 0!");
            }

            if (!File.Exists(templatePath))
            {
                throw new FileNotFoundException("Document template not found", templatePath);
            }

            var template = File.ReadAllBytes(templatePath);

            var mainStream = new MemoryStream();

            mainStream.Write(template, 0, template.Length);
            mainStream.Position = 0;

            byte[] result;
            try
            {
                using (var mainDocument = WordprocessingDocument.Open(mainStream, true))
                {
                    var mainXml = mainDocument.MainDocumentPart.Document.Body.OuterXml;

                    mainXml = mainXml.Replace("productName", documentInfos[0].ProductName);
                    mainXml = mainXml.Replace("AWPCount", documentInfos[0].AWPCount);
                    mainXml = mainXml.Replace("regNumber", documentInfos[0].RegNumber);
                    mainXml = mainXml.Replace("organizationName", documentInfos[0].OrganizationName);
                    mainXml = mainXml.Replace("lifeTime", documentInfos[0].LifeTime);

                    var mainBody = XElement.Parse(mainXml);

                    for (var i = 1; i < documentInfos.Length; i++)
                    {
                        using (var tempDocument = WordprocessingDocument.Open(new MemoryStream(template), false))
                        {
                            var tempXml = tempDocument.MainDocumentPart.Document.Body.OuterXml;

                            tempXml = tempXml.Replace("productName", documentInfos[i].ProductName);
                            tempXml = tempXml.Replace("AWPCount", documentInfos[i].AWPCount);
                            tempXml = tempXml.Replace("regNumber", documentInfos[i].RegNumber);
                            tempXml = tempXml.Replace("organizationName", documentInfos[i].OrganizationName);
                            tempXml = tempXml.Replace("lifeTime", documentInfos[i].LifeTime);

                            var tempBody = XElement.Parse(tempXml);

                            mainBody.Add(tempBody);
                            mainDocument.MainDocumentPart.Document.Body = new Body(mainBody.ToString());
                            mainDocument.MainDocumentPart.Document.Save();
                            mainDocument.Package.Flush();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(string.Format(CultureInfo.CurrentCulture, "Error while merging files.{0", 1), e);
            }
            finally
            {
                result = mainStream.ToArray();
                mainStream.Close();
                mainStream.Dispose();
            }

            return result;
        }

Комментариев нет:

Отправить комментарий