﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / SQL Server 2008 - General  / XML with word document style / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sun, 19 May 2013 19:20:16 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: XML with word document style</title><link>http://www.sqlservercentral.com/Forums/Topic1409515-391-1.aspx</link><description>[quote][b]elham_azizi_62 (1/26/2013)[/b][hr]Dear Tavis.my goal is to create word mail merge with T-sql in a store procedure use of creating xml from query result.is there Easier way to do this if you know?[/quote]Make life easy on yourself.  Export the data to a file and use Word Mail Merge from there.  You'll have nearly instant success.</description><pubDate>Sat, 26 Jan 2013 17:43:12 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: XML with word document style</title><link>http://www.sqlservercentral.com/Forums/Topic1409515-391-1.aspx</link><description>[quote][b]elham_azizi_62 (1/26/2013)[/b][hr]Dear Tavis.We need to have one dataset with more than 1 datatable  in word mail merge .is it possible this?[/quote]You could start by creating a mock-up (example) Word document with all the elements you need: title, heading, paragraph of text, all the tables each with a row of column headings and two rows from the dataset.Then you can save this document as XML: Save As &amp;gt; Other Formats &amp;gt; Word XML &amp;#100;ocument.This will give you the target document to aim for, although it will be complex and have more structure and content than you actually need to generate from your query. You can then think about the best way to create this document, directly (as in the code above), through a simpler format like HTML that Word can open and convert, or through an intermediary format that can be converted into Word by other means, or using some other existing tool or process.I am assuming that you have looked at other options like [url=http://msdn.microsoft.com/en-us/library/dd283105(v=sql.105).aspx]Reporting Services export to Word[/url] or whatever, and these are not suitable for you.If you attach a sample Word document showing your tables (column headings and two rows of dummy data each would be fine), we can take a look at it and give more suggestions.</description><pubDate>Sat, 26 Jan 2013 09:32:33 GMT</pubDate><dc:creator>Tavis Reddick</dc:creator></item><item><title>RE: XML with word document style</title><link>http://www.sqlservercentral.com/Forums/Topic1409515-391-1.aspx</link><description>Dear Tavis.We need to have one dataset with more than 1 datatable  in word mail merge .is it possible this?</description><pubDate>Sat, 26 Jan 2013 06:44:50 GMT</pubDate><dc:creator>elham_azizi_62</dc:creator></item><item><title>RE: XML with word document style</title><link>http://www.sqlservercentral.com/Forums/Topic1409515-391-1.aspx</link><description>Dear Tavis.my goal is to create word mail merge with T-sql in a store procedure use of creating xml from query result.is there Easier way to do this if you know?</description><pubDate>Sat, 26 Jan 2013 02:54:41 GMT</pubDate><dc:creator>elham_azizi_62</dc:creator></item><item><title>RE: XML with word document style</title><link>http://www.sqlservercentral.com/Forums/Topic1409515-391-1.aspx</link><description>thank you so much for your reply.you suggest me that create html from query result and It is easier than you think.please guide me how to do it?</description><pubDate>Fri, 25 Jan 2013 23:23:35 GMT</pubDate><dc:creator>elham_azizi_62</dc:creator></item><item><title>RE: XML with word document style</title><link>http://www.sqlservercentral.com/Forums/Topic1409515-391-1.aspx</link><description>You might want to look at the [url=http://www.ecma-international.org/publications/standards/Ecma-376.htm]Office Open XML File Formats[/url] used by Word 2007 onwards, but they are complex.You can read introductory articles such as [url=http://msdn.microsoft.com/en-us/library/office/bb266220(v=office.12).aspx]Walkthrough: Word 2007 XML Format[/url] and [url=http://msdn.microsoft.com/en-us/library/office/gg278313(v=office.14).aspx]Working With Tables (WordprocessingML)[/url].I still do not recommend generating WordprocessingML directly from SQL, but you can do it. The following code generates a Word XML document containing a table (with one column coloured) driven by two joined tables:[code="sql"]-- SQL Server 2008 R2 demonstration code to extract data into WordProcessingML for Microsoft Word 2007+-- Hold standard parts of your Word document package in XML variablesDECLARE @Package1 XML;SET @Package1 = '	&amp;lt;pkg:part xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage" pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="512"&amp;gt;		&amp;lt;pkg:xmlData&amp;gt;			&amp;lt;Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"&amp;gt;				&amp;lt;Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/&amp;#100;ocument.xml"/&amp;gt;			&amp;lt;/Relationships&amp;gt;		&amp;lt;/pkg:xmlData&amp;gt;	&amp;lt;/pkg:part&amp;gt;';-- An XML variable to hold the WordProcessingML document before it is inserted into the Package.DECLARE @Document XML;-- Declare XML namespaces for Microsoft Word 2007+ WordprocessingMLWITH XMLNAMESPACES (	'http://schemas.openxmlformats.org/markup-compatibility/2006' AS ve,	'urn:schemas-microsoft-com:office:office' AS o,	'http://schemas.openxmlformats.org/officeDocument/2006/relationships' AS r,	'http://schemas.openxmlformats.org/officeDocument/2006/math' AS m,	'urn:schemas-microsoft-com:vml' AS v,	'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing' AS wp,	'urn:schemas-microsoft-com:office:word' AS w10,	'http://schemas.openxmlformats.org/wordprocessingml/2006/main' AS w,	'http://schemas.openxmlformats.org/wordprocessingml/2006/main' AS w2, -- second reference to namespace allows duplication of elements with same name in query	'http://schemas.microsoft.com/office/word/2006/wordml' AS wne),-- Courses belong to a cluster. Represents a data table.Course AS (	SELECT *	FROM (	VALUES ('HND Art and Design', 1)		,('Woodcraft Workshop', 1)		,('HNC Fitness Health and Exercise ', 2)		,('HND Coaching and Development of Sport', 2)		,('NC Intermediate 2 Administration', 3)		,('HNC Accounting', 3)		,('BA Business Administration', 3)	) AS Course(Title, Cluster)),-- Clusters group many courses together, each has a different colour. Represents a data table.Cluster AS (	SELECT *	FROM (	VALUES (1, 'Creative Industries', '#91278F')	, (2, 'Sport and Fitness', '#A0CBED')	, (3, 'Business and Management', '#00467F')	) AS Cluster(Identifier, Title, Colour))SELECT @Document = (	SELECT (		SELECT Cluster.Title AS 'w:tc/w:p/w:r/w:t',			Cluster.Colour AS 'w:tc/w:tcPr/w:shd/@w:fill',			Course.Title AS 'w2:tc/w2:p/w2:r/w2:t' -- see note above in XMLNAMESPACES		FROM Course			INNER JOIN				Cluster					ON Course.Cluster = Cluster.Identifier		FOR XML PATH('w:tr'), TYPE, ROOT('w:tbl')	) FOR XML PATH('w:body'), ROOT('w:document'));-- Declare XML namespaces for Microsoft Word 2007+ PackagingWITH XMLNAMESPACES (	'http://schemas.microsoft.com/office/2006/xmlPackage' AS pkg)SELECT @Package1,	'/word/&amp;#100;ocument.xml' AS 'pkg:part/@pkg:name',	'application/vnd.openxmlformats-office&amp;#100;ocument.wordprocessingml.&amp;#100;ocument.main+xml' AS 'pkg:part/@pkg:contentType',	@Document AS 'pkg:part/pkg:xmlData' -- insert the XML document you created above.FOR XML PATH(''), ROOT('pkg:package')/*You can save output as &amp;#100;ocument.xml and open it in Word.To make Word the default application to open it, you can also manually insert the following as the first two lines of the XML document:&amp;lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&amp;gt;&amp;lt;?mso-application progid="Word.Document"?&amp;gt;*/[/code]It would be easier to generate HTML (which has less complex hierarchies of elements) and open it in Word.My personal preference is to generate a (preferably standardized) data-oriented XML format from SQL and transform it (using XSLT) into one or more document-oriented formats (HTML, Word, XSL-FO-into-PDF).</description><pubDate>Wed, 23 Jan 2013 09:41:02 GMT</pubDate><dc:creator>Tavis Reddick</dc:creator></item><item><title>RE: XML with word document style</title><link>http://www.sqlservercentral.com/Forums/Topic1409515-391-1.aspx</link><description>thanks for your reply Tevis.please give me a link for more information about Title,Body,Font,Table,etc in xml from sql server.Meanwhile please give me a sample xml with word document style from "select * from table"'s result for example.thanks</description><pubDate>Wed, 23 Jan 2013 02:40:55 GMT</pubDate><dc:creator>elham_azizi_62</dc:creator></item><item><title>RE: XML with word document style</title><link>http://www.sqlservercentral.com/Forums/Topic1409515-391-1.aspx</link><description>If you mean, can you generate a word document from a SQL query, then I guess the short answer is "yes".Try:[code="sql"]WITH XMLNAMESPACES ('http://schemas.microsoft.com/office/word/2003/wordml' AS W,	'urn:schemas-microsoft-com:office:office' AS o)SELECT 'Hello World!' AS 'o:DocumentProperties/o:Title',	'Hello World!' AS 'W:body/W:p/W:r/W:t'FOR XML PATH(''), ROOT('W:wordDocument')[/code]which gives you a very simple Word 2003 document in XML:[code="xml"]&amp;lt;W:wordDocument xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:W="http://schemas.microsoft.com/office/word/2003/wordml"&amp;gt;  &amp;lt;o:DocumentProperties&amp;gt;    &amp;lt;o:Title&amp;gt;Hello World!&amp;lt;/o:Title&amp;gt;  &amp;lt;/o:DocumentProperties&amp;gt;  &amp;lt;W:body&amp;gt;    &amp;lt;W:p&amp;gt;      &amp;lt;W:r&amp;gt;        &amp;lt;W:t&amp;gt;Hello World!&amp;lt;/W:t&amp;gt;      &amp;lt;/W:r&amp;gt;    &amp;lt;/W:p&amp;gt;  &amp;lt;/W:body&amp;gt;&amp;lt;/W:wordDocument&amp;gt;[/code] which you can save as helloworld.xml and open in Word. If you add an XML declaration as the first line to the above output:[code="xml"]&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;[/code]you can save as helloworld.doc and Word opens it more naturally.You can add various Word styles, properties and objects by referring to Microsoft's schema guidance.However, it might be more useful and flexible to save as an intermediary data format first, then convert/transform it to Word.</description><pubDate>Tue, 22 Jan 2013 08:34:17 GMT</pubDate><dc:creator>Tavis Reddick</dc:creator></item><item><title>RE: XML with word document style</title><link>http://www.sqlservercentral.com/Forums/Topic1409515-391-1.aspx</link><description>What do you mean with "word document style"?</description><pubDate>Mon, 21 Jan 2013 05:56:43 GMT</pubDate><dc:creator>Koen Verbeeck</dc:creator></item><item><title>XML with word document style</title><link>http://www.sqlservercentral.com/Forums/Topic1409515-391-1.aspx</link><description>hello all.is it possible create one xml file with word document style from query result?</description><pubDate>Mon, 21 Jan 2013 05:48:47 GMT</pubDate><dc:creator>elham_azizi_62</dc:creator></item></channel></rss>