<?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>Usingnamespace - Blog &#187; Excel</title>
	<atom:link href="http://www.usingnamespace.org/category/excel/feed" rel="self" type="application/rss+xml" />
	<link>http://www.usingnamespace.org</link>
	<description>C#, XUL, Javascript, C++ ...</description>
	<lastBuildDate>Tue, 17 Jul 2012 14:53:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>[C#] Open and Export data to an Excel file</title>
		<link>http://www.usingnamespace.org/copen-and-export-data-to-an-excel-file.html</link>
		<comments>http://www.usingnamespace.org/copen-and-export-data-to-an-excel-file.html#comments</comments>
		<pubDate>Sat, 06 Feb 2010 20:08:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Excel]]></category>

		<guid isPermaLink="false">http://www.usingnamespace.org/wp-blog/?p=10</guid>
		<description><![CDATA[If you ever wondered how to open an excel file and write data into it from your .NET project, here is the solution :

using Excel = Microsoft.Office.Interop.Excel;
&#160;
Excel.Application xlApp = new Excel.ApplicationClass&#40;&#41;;
&#160;
xlApp.Visible = true; //If true, the Excel window will be visible during the process, if not, it is done in background
Excel.Workbook xlWBook = xlApp.Workbooks.Open&#40;filename, 0, [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever wondered how to open an excel file and write data into it from your .NET project, here is the solution :</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">Excel </span><span style="color: #008000;">=</span> Microsoft.<span style="color: #0000FF;">Office</span>.<span style="color: #0000FF;">Interop</span>.<span style="color: #0000FF;">Excel</span><span style="color: #008000;">;</span>
&nbsp;
Excel.<span style="color: #0000FF;">Application</span> xlApp <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Excel.<span style="color: #0000FF;">ApplicationClass</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
xlApp.<span style="color: #0000FF;">Visible</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//If true, the Excel window will be visible during the process, if not, it is done in background</span>
Excel.<span style="color: #0000FF;">Workbook</span> xlWBook <span style="color: #008000;">=</span> xlApp.<span style="color: #0000FF;">Workbooks</span>.<span style="color: #0000FF;">Open</span><span style="color: #000000;">&#40;</span>filename, <span style="color: #FF0000;">0</span>, <span style="color: #0600FF;">false</span>, <span style="color: #FF0000;">5</span>, <span style="color: #000000;">System.<span style="color: #0000FF;">Reflection</span></span>.<span style="color: #0000FF;">Missing</span>.<span style="color: #0000FF;">Value</span>, <span style="color: #000000;">System.<span style="color: #0000FF;">Reflection</span></span>.<span style="color: #0000FF;">Missing</span>.<span style="color: #0000FF;">Value</span>, <span style="color: #0600FF;">false</span>, <span style="color: #000000;">System.<span style="color: #0000FF;">Reflection</span></span>.<span style="color: #0000FF;">Missing</span>.<span style="color: #0000FF;">Value</span>, <span style="color: #000000;">System.<span style="color: #0000FF;">Reflection</span></span>.<span style="color: #0000FF;">Missing</span>.<span style="color: #0000FF;">Value</span>, <span style="color: #0600FF;">true</span>, <span style="color: #0600FF;">false</span>, <span style="color: #000000;">System.<span style="color: #0000FF;">Reflection</span></span>.<span style="color: #0000FF;">Missing</span>.<span style="color: #0000FF;">Value</span>, <span style="color: #0600FF;">false</span>, <span style="color: #0600FF;">false</span>, <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><span style="color: #008080; font-style: italic;">//Opens the Excel sheets</span>
Excel.<span style="color: #0000FF;">Worksheet</span> xlSheet <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>Excel.<span style="color: #0000FF;">Worksheet</span><span style="color: #000000;">&#41;</span>xlWBook.<span style="color: #0000FF;">Worksheets</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//Get the first Sheet of the Excel file</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//Write Something at Cell A1 :</span>
<span style="color: #FF0000;">string</span> value <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Test&quot;</span><span style="color: #008000;">;</span>
Excel.<span style="color: #0000FF;">Range</span> excelCell2 <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>Excel.<span style="color: #0000FF;">Range</span><span style="color: #000000;">&#41;</span>xlSheet.<span style="color: #0000FF;">get_Range</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;A1:A1&quot;</span>, Type.<span style="color: #0000FF;">Missing</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//Selects a Cell A1</span>
excelCell2.<span style="color: #0000FF;">Cells</span>.<span style="color: #0000FF;">Value2</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//Duplicate a line</span>
&nbsp;
<span style="color: #FF0000;">int</span> line <span style="color: #008000;">=</span> <span style="color: #FF0000;">5</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//Will copy line 5 and paste the copy above</span>
Excel.<span style="color: #0000FF;">Range</span> R1 <span style="color: #008000;">=</span> xlSheet.<span style="color: #0000FF;">get_Range</span><span style="color: #000000;">&#40;</span>xlSheet.<span style="color: #0000FF;">Cells</span><span style="color: #000000;">&#91;</span>line, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span>, xlSheet.<span style="color: #0000FF;">Cells</span><span style="color: #000000;">&#91;</span>line, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">EntireRow</span><span style="color: #008000;">;</span>
xlSheet.<span style="color: #0000FF;">get_Range</span><span style="color: #000000;">&#40;</span>xlSheet.<span style="color: #0000FF;">Cells</span><span style="color: #000000;">&#91;</span>line, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span>, xlSheet.<span style="color: #0000FF;">Cells</span><span style="color: #000000;">&#91;</span>line, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">EntireRow</span>.<span style="color: #0000FF;">Copy</span><span style="color: #000000;">&#40;</span>Type.<span style="color: #0000FF;">Missing</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
R1.<span style="color: #0000FF;">EntireRow</span>.<span style="color: #0000FF;">Insert</span><span style="color: #000000;">&#40;</span>Excel.<span style="color: #0000FF;">XlInsertShiftDirection</span>.<span style="color: #0000FF;">xlShiftDown</span>, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//Here we copy the cells' formats, style and formulas.</span>
R1.<span style="color: #0000FF;">EntireRow</span>.<span style="color: #0000FF;">PasteSpecial</span><span style="color: #000000;">&#40;</span>Excel.<span style="color: #0000FF;">XlPasteType</span>.<span style="color: #0000FF;">xlPasteFormulasAndNumberFormats</span>, Excel.<span style="color: #0000FF;">XlPasteSpecialOperation</span>.<span style="color: #0000FF;">xlPasteSpecialOperationNone</span>, <span style="color: #0600FF;">false</span>, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>More info about the XlPasteType and XlPasteSpecialOperation :</p>
<p><a href="http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.namedrange.pastespecial%28VS.80%29.aspx">http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.namedrange.pastespecial%28VS.80%29.aspx</a></p>
<p>Also helpful :</p>
<p><a href="http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.applicationclass%28office.11%29.aspx">http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.applicationclass%28office.11%29.aspx<br />
</a></p>
<p>Troubleshooting:</p>
<p>If like me you have an Operating System and an Excel which uses different money and number formats (for me French OS and US Excel), you will encounter the following exception while opening Workbook :</p>
<p>System.Runtime.InteropServices.COMException (0&#215;80028018): Ancien format ou bibliothèque de types non valide. (Exception de HRESULT : 0&#215;80028018 (TYPE_E_INVDATAREAD))</p>
<p>à Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)</p>
<p>You can fix this bug by setting the current thread culture to &#8220;en &#8211; US&#8221; :</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">System.<span style="color: #0000FF;">Threading</span></span>.<span style="color: #0000FF;">Thread</span>.<span style="color: #0000FF;">CurrentThread</span>.<span style="color: #0000FF;">CurrentCulture</span> <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Globalization</span></span>.<span style="color: #0000FF;">CultureInfo</span>.<span style="color: #0000FF;">CreateSpecificCulture</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;en-US&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Be careful though, if you do so it changes to whole behaviour of you program concerning number and money formats. For instance the float.Parse() function will behave differently if the new Culture has a different number formatting than yours !
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.usingnamespace.org/copen-and-export-data-to-an-excel-file.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
