AXForum  
Вернуться   AXForum > Microsoft Dynamics AX > DAX Blogs
DAX
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск Все разделы прочитаны

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 12.10.2018, 22:13   #1  
Blog bot is offline
Blog bot
Участник
 
25,459 / 846 (79) +++++++
Регистрация: 28.10.2006
goshoom: Word document from code
Источник: http://dev.goshoom.net/en/2018/10/wo...ent-from-code/
==============

Sometimes you may want to generate a Word document from code in D365FO, which gives you much more control over the result than if you simply printed a report to Word.

Here is a very brief example of how you can do it.

Start with creating an X++ project. Then add a C# class library to the same project. Right-click the C# project, use Manage NuGet Packages… and install DocumentFormat.OpenXml package.

Then add the following class:

using System.IO;using DocumentFormat.OpenXml;using DocumentFormat.OpenXml.Packaging;using DocumentFormat.OpenXml.Wordprocessing; namespace WordLib{ public class WordDoc { public Stream Create() { MemoryStream ms = new MemoryStream(); using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(ms, WordprocessingDocumentType.Document, true)) { MainDocumentPart mainPart = wordDocument.AddMainDocumentPart(); string text = "Do androids dream of electric sheep?"; Body body = new Body(new Paragraph(new Run(new Text(text)))); mainPart.Document = new Document(body); } return ms; } }}






This code will create a very simple Word document, containing only a single line of text, and returns it as a memory stream. You would likely need something more complicated, but that’s out of scope of this blog post. You can get more information from Open XML SDK documentation.

Build the C# class library and we’re done with it; now we need to call it from X++.

Go to the X++ project, right-click References, chose Add Reference… and add a project reference to the C# library.

Then add a runnable class with the following code, which merely calls the library and returns the stream as a file to user:

class WordDocGeneratorSample{ public static void main(Args _args) { using (System.IO.Stream wordStream = new WordLib.WordDoc().Create()) { File::SendFileToUser(wordStream, 'file.docx'); } }}






Set the class as the startup object, run the project and your browser should offer you the Word document for download.



Источник: http://dev.goshoom.net/en/2018/10/wo...ent-from-code/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
atinkerersnotebook: Creating Document Attachment Templates Using Word Blog bot DAX Blogs 0 17.07.2014 14:11
goshoom: Code in forms Blog bot DAX Blogs 0 22.04.2013 11:11
ax_gfm_framework_team: Step 5: Add code to distribute and journalize source document amounts Blog bot DAX Blogs 0 08.05.2012 04:13
dax-lessons: Document Handling in AX - setup and Example Blog bot DAX Blogs 0 27.08.2007 23:00
Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 00:52.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.