Exemplo n.º 1
0
        public Word ExportFromTemplate <T>(string templateUrl, T wordData) where T : class, new()
        {
            XWPFDocument word = NPOIHelper.GetXWPFDocument(templateUrl);

            ReplacePlaceholders(word, wordData);

            var result = new Word()
            {
                Type      = SolutionEnum.NPOI,
                WordBytes = word.ToBytes()
            };

            return(result);
        }
        /// <summary>
        /// 替换占位符
        /// </summary>
        /// <param name="word"></param>
        private void ReplacePlaceholders <T>(XWPFDocument word, T wordData)
            where T : class, new()
        {
            if (word == null)
            {
                throw new ArgumentNullException("word");
            }

            Dictionary <string, string> stringReplacements = WordHelper.GetReplacements(wordData);

            Dictionary <string, IEnumerable <Picture> > pictureReplacements = WordHelper.GetPictureReplacements(wordData);

            NPOIHelper.ReplacePlaceholdersInWord(word, stringReplacements, pictureReplacements);
        }
Exemplo n.º 3
0
        public IEnumerable <Table> GetTables(string fileUrl)
        {
            var result = new List <Table>();

            var doc = NPOIHelper.GetXWPFDocument(fileUrl);

            var xwpfParagraphsEnumerator = doc.GetParagraphsEnumerator();

            while (xwpfParagraphsEnumerator.MoveNext())
            {
                var xwpfParagraph = xwpfParagraphsEnumerator.Current;
            }

            return(result);
        }
Exemplo n.º 4
0
        public Word CreateFromMasterTable <T>(string templateUrl, IEnumerable <T> datas) where T : class, new()
        {
            var template = NPOIHelper.GetXWPFDocument(templateUrl);

            var result = new Word()
            {
                Type = SolutionEnum.NPOI
            };

            var tables = template.GetTablesEnumerator();

            if (tables == null)
            {
                result.WordBytes = template.ToBytes();
                return(result);
            }

            var masterTables = new List <XWPFTable>();

            while (tables.MoveNext())
            {
                masterTables.Add(tables.Current);
            }

            var doc = new XWPFDocument();

            foreach (var data in datas)
            {
                foreach (var masterTable in masterTables)
                {
                    var cloneTable = doc.CreateTable();
                    NPOIHelper.CopyTable(masterTable, cloneTable);
                }

                ReplacePlaceholders(doc, data);
            }

            result.WordBytes = doc.ToBytes();

            return(result);
        }