<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DevtheWeb.NET &#187; OpenXML</title>
	<atom:link href="http://www.devtheweb.net/blog/category/openxml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.devtheweb.net/blog</link>
	<description></description>
	<lastBuildDate>Tue, 06 Dec 2011 19:25:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Export data to docx file using Open XML SDK</title>
		<link>http://www.devtheweb.net/blog/2010/02/08/export-data-to-docx-file-using-open-xml-sdk/</link>
		<comments>http://www.devtheweb.net/blog/2010/02/08/export-data-to-docx-file-using-open-xml-sdk/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 10:46:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[OpenXML]]></category>

		<guid isPermaLink="false">http://www.devtheweb.net/blog/?p=913</guid>
		<description><![CDATA[Here&#8217;s simple example how can we export some table data to docx file using Open XML SDK. If you are not familiar with Open XML SDK, you can check out first Introduction to Open XML SDK. In this example: 1) Create some string array that represent table data 2) Create a new Docx file and [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s simple example how can we export some table data to docx file using Open XML SDK. If you are not familiar with Open XML SDK, you can check out first <a href="http://www.devtheweb.net/blog/2010/01/11/introduction-to-open-xml-sdk-2-0/" target="_blank">Introduction to Open XML SDK</a>.</p>
<p>In this example:<br />
1) Create some string array that represent table data<br />
2) Create a new Docx file and init it<br />
3) Create styles for the table and fill it with data<br />
4) Fill the table with data<br />
5) Add the table to the document and save the changes</p>
<p>//t some data to export in table<br />
string[][] data = {new string[]{&#8220;column 1&#8243;, &#8220;column 2&#8243;, &#8220;column 3&#8243;, &#8220;column 4&#8243;, &#8220;column 5&#8243;},<br />
new string[]{&#8220;A&#8221;, &#8220;B&#8221;, &#8220;C&#8221;, &#8220;D&#8221;, &#8220;E&#8221;},<br />
new string[]{&#8220;1&#8243;, &#8220;2&#8243;, &#8220;3&#8243;, &#8220;4&#8243;, &#8220;5&#8243;},<br />
new string[]{&#8220;Dev&#8221;, &#8220;The&#8221;, &#8220;Web&#8221;, &#8220;dot&#8221;, &#8220;NET&#8221;}<br />
};</p>
<p>/*t create and init a new docx file and<br />
a WordprocessingDocument object to represent it t*/<br />
WordprocessingDocument doc = WordprocessingDocument.Create(&#8220;E:\dox-export.docx&#8221;,<br />
WordprocessingDocumentType.Document);</p>
<p>MainDocumentPart mainDocPart = doc.AddMainDocumentPart();<br />
mainDocPart.Document = new Document();<br />
Body body = new Body();<br />
mainDocPart.Document.Append(body);</p>
<p>/*t init the cell border styles t*/<br />
TableCellProperties cellProperties = new TableCellProperties(<br />
new TableCellBorders<br />
{<br />
TopBorder = new TopBorder<br />
{<br />
Val = new EnumValue&lt;BorderValues&gt;(BorderValues.BasicBlackDots),<br />
Size = new UInt32Value((uint)8),<br />
Color = new StringValue(&#8220;000000&#8243;)<br />
},<br />
RightBorder = new RightBorder<br />
{<br />
Val = new EnumValue&lt;BorderValues&gt;(BorderValues.BasicBlackDots),<br />
Size = new UInt32Value((uint)8),<br />
Color = new StringValue(&#8220;000000&#8243;)<br />
},<br />
BottomBorder = new BottomBorder<br />
{<br />
Val = new EnumValue&lt;BorderValues&gt;(BorderValues.BasicBlackDots),<br />
Size = new UInt32Value((uint)8),<br />
Color = new StringValue(&#8220;000000&#8243;)<br />
},<br />
LeftBorder = new LeftBorder<br />
{<br />
Val = new EnumValue&lt;BorderValues&gt;(BorderValues.BasicBlackDots),<br />
Size = new UInt32Value((uint)8),<br />
Color = new StringValue(&#8220;000000&#8243;)<br />
},<br />
});</p>
<p>/*t fill the string[][] data into<br />
DocumentFormat.OpenXml.Wordprocessing.Table object t*/</p>
<p>Table table = new Table();<br />
for (int i = 0; i &lt; data.Length; ++i)<br />
{<br />
TableRow row = new TableRow();</p>
<p>for (int j = 0; j &lt; data[i].Length; ++j)<br />
{<br />
TableCell cell = new TableCell(cellProperties.Clone() as TableCellProperties);</p>
<p>cell.Append(new Paragraph(<br />
new Run(<br />
new Text(data[i][j])<br />
)));</p>
<p>row.Append(cell);<br />
}</p>
<p>table.Append(row);<br />
}</p>
<p>/*t add some info about the dox export t*/<br />
body.Append(new Paragraph(<br />
new Run(<br />
new Text(&#8220;Table exported by DevTheWeb.NET&#8221;)<br />
)));</p>
<p>body.Append(table);<br />
<!--adsense--><br />
/*t don&#8217;t forget to save your changes t*/<br />
doc.MainDocumentPart.Document.Save();<br />
doc.Dispose();</p>
<p>That&#8217;s all, if we open the dox file, we can see the exported table:<br />
<a href="http://www.devtheweb.net/blog/wp-content/uploads/2010/02/export-data-to-docx.png" rel="lightbox[913]"><img class="alignnone size-full wp-image-915" title="export-data-to-docx" src="http://www.devtheweb.net/blog/wp-content/uploads/2010/02/export-data-to-docx.png" alt="" width="400" height="402" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.devtheweb.net/blog/2010/02/08/export-data-to-docx-file-using-open-xml-sdk/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Playing with OpenXML: Let&#8217;s Convert docx to Simple html5</title>
		<link>http://www.devtheweb.net/blog/2010/01/25/playing-with-openxml-lets-convert-docx-to-simple-html5/</link>
		<comments>http://www.devtheweb.net/blog/2010/01/25/playing-with-openxml-lets-convert-docx-to-simple-html5/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 08:43:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[OpenXML]]></category>

		<guid isPermaLink="false">http://www.devtheweb.net/blog/?p=728</guid>
		<description><![CDATA[I decided to play little with the application from the Introduction to Open XML SDK 2.0 Post and I decided to read text in more details, ex. to iterate through all document paragraphs and for each paragraph to iterate through each of its Runs (Run is a region of text with a common set of [...]]]></description>
			<content:encoded><![CDATA[<p>I decided to play little with the application from the <a href="http://www.devtheweb.net/blog/2010/01/11/introduction-to-open-xml-sdk-2-0/" target="_blank">Introduction to Open XML SDK 2.0 Post</a> and I decided to read text in more details, ex. to iterate through all document paragraphs and for each paragraph to iterate through each of its Runs (Run is a region of text with a common set of properties, within paragraph text is grouped into one or more runs).</p>
<p>Here&#8217;s a simple example how after load docx file, we can iterate through all of its paragraphs, for each paragraph to iterate through all of its Runs and read some Run&#8217;s attributes to convert them to html:</p>
<p>//t load the .docx document<br />
doc = WordprocessingDocument.Open(_filePathTB.Text, false);</p>
<p>//t iterate through paragraphs<br />
paragraphs = doc.MainDocumentPart.Document.Body.Elements&lt;Paragraph&gt;();</p>
<p>foreach (Paragraph para in paragraphs)<br />
{<br />
foreach (Run r in para.Elements&lt;Run&gt;())<br />
{<br />
//t  r is our current Run<br />
}<br />
}</p>
<p>Using the code above we can write a simple docx to html5 convertor</p>
<p>//t load the .docx document<br />
doc = WordprocessingDocument.Open(_filePathTB.Text, false);</p>
<p>text.AppendLine(&#8220;&lt;!DOCTYPE html&gt;&#8221;);<br />
text.AppendLine(&#8220;&lt;html&gt;&#8221;);<br />
text.AppendLine(&#8220;  &lt;head&gt;&#8221;);<br />
text.AppendLine(&#8220;    &lt;title&gt;&#8221; + _filePathTB.Text + &#8221; HTML5 Export&lt;/title&gt;&#8221;);<br />
text.AppendLine(&#8220;  &lt;/head&gt;&#8221;);<br />
text.AppendLine(&#8220;  &lt;body&gt;&#8221;);</p>
<p>//t iterate through paragraphs<br />
paragraphs = doc.MainDocumentPart.Document.Body.Elements&lt;Paragraph&gt;();</p>
<p>foreach (Paragraph para in paragraphs)<br />
{<br />
//t we export paragraphs as &lt;p&gt; tags<br />
text.Append(&#8220;&lt;p&gt;&#8221;);<br />
foreach (Run r in para.Elements&lt;Run&gt;())<br />
{<br />
if (r.RunProperties != null)<br />
{<br />
//t ADD OPENNING TAGS HERE, if any<br />
if (r.RunProperties.Bold != null)<br />
text.Append(&#8220;&lt;b&gt;&#8221;);</p>
<p>if (r.RunProperties.Italic != null)<br />
text.Append(&#8220;&lt;i&gt;&#8221;);</p>
<p>if (r.RunProperties.Underline != null)<br />
text.Append(&#8220;&lt;u&gt;&#8221;);</p>
<p>if (r.RunProperties.Color != null &amp;&amp; r.RunProperties.Color.Val != null)<br />
text.Append(&#8220;&lt;span style=&#8221;color:#&#8221; + r.RunProperties.Color.Val + &#8220;&#8221;&gt;&#8221;);</p>
<p>//t ADD TEXT HERE<br />
text.Append(r.InnerText);</p>
<p>//t ADD CLOSING TAGS HERE<br />
//t IN BACK ORDER OF OPENNING TAGS<br />
if (r.RunProperties.Color != null &amp;&amp; r.RunProperties.Color.Val != null)<br />
text.Append(&#8220;&lt;/span&gt;&#8221;);</p>
<p>if (r.RunProperties.Underline != null)<br />
text.Append(&#8220;&lt;/u&gt;&#8221;);</p>
<p>if (r.RunProperties.Italic != null)<br />
text.Append(&#8220;&lt;/i&gt;&#8221;);</p>
<p>if (r.RunProperties.Bold != null)<br />
text.Append(&#8220;&lt;/b&gt;&#8221;);<br />
}<br />
else<br />
{<br />
text.Append(r.InnerText);<br />
}</p>
<p>}<br />
text.Append(&#8220;&lt;/p&gt;&#8221;);<br />
}</p>
<p>text.AppendLine(&#8220;  &lt;/body&gt;&#8221;);<br />
text.AppendLine(&#8220;&lt;/html&gt;&#8221;);</p>
<p>_showTextTB.Text = text.ToString();</p>
<p>The StringBuilder variable text contains all the docx text converted to html5, it supports only bold, italic, underline and color properties. I said, it&#8217;s simple <img src='http://www.devtheweb.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Here are screenshots of the app in action:</p>
<p>We have a docx with simple text<br />
<a href="http://www.devtheweb.net/blog/wp-content/uploads/2010/01/docx_with_simple_text.png" rel="lightbox[728]"><img class="alignnone size-full wp-image-731" title="docx_with_simple_text" src="http://www.devtheweb.net/blog/wp-content/uploads/2010/01/docx_with_simple_text.png" alt="" width="400" height="299" /></a></p>
<p><a href="http://www.devtheweb.net/blog/wp-content/uploads/2010/01/convert_docx_to_html5.png" rel="lightbox[728]"><img class="alignnone size-full wp-image-732" title="convert_docx_to_html5" src="http://www.devtheweb.net/blog/wp-content/uploads/2010/01/convert_docx_to_html5.png" alt="" width="400" height="268" /></a></p>
<p>Now, we copy-paste the result in html file and the result is:<br />
<a href="http://www.devtheweb.net/blog/wp-content/uploads/2010/01/docx_to_html5_result.png" rel="lightbox[728]"><img class="alignnone size-full wp-image-734" title="docx_to_html5_result" src="http://www.devtheweb.net/blog/wp-content/uploads/2010/01/docx_to_html5_result.png" alt="" width="400" height="241" /></a></p>
<p>You can download the simple app <a href="http://www.devtheweb.net/downloads/TestOpenXmlSdkDocxToHtml5.rar" target="_blank">HERE</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devtheweb.net/blog/2010/01/25/playing-with-openxml-lets-convert-docx-to-simple-html5/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Introduction to Open XML SDK 2.0</title>
		<link>http://www.devtheweb.net/blog/2010/01/11/introduction-to-open-xml-sdk-2-0/</link>
		<comments>http://www.devtheweb.net/blog/2010/01/11/introduction-to-open-xml-sdk-2-0/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 08:22:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[OpenXML]]></category>

		<guid isPermaLink="false">http://www.devtheweb.net/blog/?p=493</guid>
		<description><![CDATA[Currently, I work with Open XML SDK 2.0 and in this post, I&#8217;ll try to explain simple introduction to it: What is Open XML and Open XML SDK? Open XML is an open standard that defines a set of XML schemas for representing spreadsheets, presentations, charts and word processing documents. Microsoft Office 2007 applications (Word, [...]]]></description>
			<content:encoded><![CDATA[<p>Currently, I work with Open XML SDK 2.0 and in this post, I&#8217;ll try to explain simple introduction to it:</p>
<p><strong>What is Open XML and Open XML SDK?</strong></p>
<p>Open XML is an open standard that defines a set of XML schemas for representing spreadsheets, presentations, charts and word processing documents. Microsoft Office 2007 applications (Word, Excel and PowerPoint) use the Open XML as the default file format. The Open XML file formats are based on ZIP and XML.</p>
<div><!--adsense--></div>
<p>The Open XML SDK 2.0 provides classes to manipulate Open XML documents. The Open XML SDK 2.0 uses the LINQ (Language-Integrated Query) technology to provide strongly typed object access to the XML content inside the parts of the Open XML documents.</p>
<p><strong>Download and Install<br />
</strong><br />
You can download Open XML SDK 2.0 <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&amp;displaylang=en" target="_blank">HERE</a>.</p>
<p>System Requirements:</p>
<ul>
<li>OS: Windows 7; Windows Server 2003 SP 2; Windows Server 2008 R2; Windows Server 2008 SP 2; Windows Vista SP 2; Windows XP SP 3</li>
<li>.NET Framework version 3.5 SP1</li>
<li>At least 300 MB available disk space</li>
</ul>
<p><strong>Using the Open XML API 2.0 in Visual Studio 2008 Project</strong></p>
<p>To use the API in your Visual Studio project, all you need to do is to add Reference to DocumentFormat.OpenXml:</p>
<p>In Solution Explorer, right-click References and select Add Reference.</p>
<p>In the dialog box, select .NET tab and select DocumentFormat.OpenXml option and then click OK.</p>
<p><a href="http://www.devtheweb.net/blog/wp-content/uploads/2010/01/add-openxml-reference.png" rel="lightbox[493]"><img class="alignnone size-full wp-image-495" title="add-openxml-reference" src="http://www.devtheweb.net/blog/wp-content/uploads/2010/01/add-openxml-reference.png" alt="" width="416" height="352" /></a></p>
<p><strong>Read Docx&#8217;s Text</strong></p>
<p>It&#8217;s very easy to read the text of Word 2007 Document file, here&#8217;s an example:</p>
<p>using System.IO;<br />
using DocumentFormat.OpenXml;<br />
using DocumentFormat.OpenXml.Packaging;</p>
<p>//t local variables<br />
WordprocessingDocument doc;<br />
string result = &#8220;&#8221;;</p>
<p>try<br />
{<br />
//t load the .docx document<br />
doc = WordprocessingDocument.Open(stringForFilePath, false);</p>
<p>result = (doc.MainDocumentPart.Document).Body.InnerText;<br />
}<br />
catch (Exception ex)<br />
{<br />
//t handle the exception<br />
}</p>
<p>return result;</p>
<p><strong>Conclusion</strong><br />
At first look it seems to be easy to work with the Open XML 2.0 SDK, it&#8217;s easy to install use in Visual Studio 2008. I created a simple program that loads text from docx file:<br />
<a href="http://www.devtheweb.net/blog/wp-content/uploads/2010/01/openxml-sdk-introduction.png" rel="lightbox[493]"><img class="alignnone size-full wp-image-537" title="openxml-sdk-introduction" src="http://www.devtheweb.net/blog/wp-content/uploads/2010/01/openxml-sdk-introduction.png" alt="" width="400" height="270" /></a></p>
<p>You can download it <a href="http://www.devtheweb.net/downloads/TestOpenXmlSdk.rar">HERE</a>.</p>
<form id="formPoll" method="post" action="http://www.devtheweb.net/polls/poll-do-you-find-microsoft-openxml-sdk-useful.aspx?embed=true#vote">
<p><strong>Do you find Microsoft OpenXML SDK useful?</strong></p>
<div>&nbsp;</div>
<div>
<input value="option1" id="option1" type="radio" name="poll" /><label for="option1">Yes, it&#8217;s great</label></div>
<div>
<input value="option2" id="option2" type="radio" name="poll" /><label for="option2">Maybe</label></div>
<div>
<input value="option3" id="option3" type="radio" name="poll" /><label for="option3">No, total waste of time</label></div>
<div>
<input value="option4" id="option4" type="radio" name="poll" /><label for="option4">I don&#8217;t know</label></div>
<div>&nbsp;</div>
<div>
<input type="submit" value="VOTE" onclick="this.form.target='_blank';return true;" /></div>
<div>&nbsp;</div>
<p><a href="http://www.devtheweb.net/polls/poll-do-you-find-microsoft-openxml-sdk-useful.aspx#results" target="_blank">View Results</a></p>
</form>
]]></content:encoded>
			<wfw:commentRss>http://www.devtheweb.net/blog/2010/01/11/introduction-to-open-xml-sdk-2-0/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

