<?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>dotnet thoughts &#187; Import /Export Excel</title>
	<atom:link href="http://www.dotnetthoughts.net/tag/import-export-excel/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>a dotnet developer&#039;s technical blog</description>
	<lastBuildDate>Thu, 02 Feb 2012 03:18:00 +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>Import / export data in MS Excel using C#</title>
		<link>http://www.dotnetthoughts.net/2009/09/23/import-export-data-in-ms-excel-using-c/</link>
		<comments>http://www.dotnetthoughts.net/2009/09/23/import-export-data-in-ms-excel-using-c/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 07:38:02 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C#.Net]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[Import /Export Excel]]></category>
		<category><![CDATA[OleDb]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://anuraj.wordpress.com/?p=401</guid>
		<description><![CDATA[Sometimes we may require to generate Excel file from our reports,read from excel files to import data etc. This can be achieved using Office Interop (Office Automation) assemblies, but Office Automation in Web servers,got some issues;(More details: http://support.microsoft.com/kb/257757). The alternative &#8230; <a href="http://www.dotnetthoughts.net/2009/09/23/import-export-data-in-ms-excel-using-c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Sometimes we may require to generate Excel file from our reports,read from excel files to import data etc. This can be achieved using Office Interop (Office Automation) assemblies, but Office Automation in Web servers,got some issues;(More details: <a target="_blank" href="http://support.microsoft.com/kb/257757">http://support.microsoft.com/kb/257757</a>). The alternative is using OleDb provider. You may need to add one more attribute to connection string to connect to the Excel file. And connection string will be</p>
<pre class="brush: csharp; title: ; notranslate">
string connectionString = &quot;Provider=Microsoft.Jet.OleDb.4.0; Data Source=C:\Sample.xls; Extended Properties=Excel 8.0;&quot;
</pre>
<p>Please note the &#8220;Extended Properties&#8221; attribute. This attribute helps us to query the excel file.</p>
<p>Exporting Data from Data Table to Excel File.</p>
<pre class="brush: csharp; title: ; notranslate">
string connectionString = &quot;Provider=Microsoft.Jet.OleDb.4.0; Data Source=C:\Sample.xls; Extended Properties=Excel 8.0;&quot;
// Establish a connection to the data source.
using(OleDbConnection Connection = new OleDbConnection(connectionString))
{
Connection.Open()
//creating a new Sheet with name sample and three columns with Heading firstname, lastname and email
using(OleDbCommand command = new OleDbCommand())
{
command.Connection = Connection;
command.CommandText = &quot;CREATE TABLE [Sample$](FirstName Char(255), LastName char(255), Email char(255))&quot;;
command.ExecuteNonQuery();
}
//Adding records to the Sample Worksheet
using(OleDbCommand command = new OleDbCommand())
{
command.Connection = Connection;
command.CommandText = &quot;INSERT INTO TABLE [Sample$](FirstName,LastName,Email) VALUES('Anuraj','P','anuraj.p@example.com')&quot;;
command.ExecuteNonQuery();
command.CommandText = &quot;INSERT INTO TABLE [Sample$](FirstName,LastName,Email) VALUES('sreekumar','vn','sreekumar.vn@example.com')&quot;;
command.ExecuteNonQuery();
}
}
</pre>
<p>Import the Data from Excel</p>
<pre class="brush: csharp; title: ; notranslate">
DataTable dt;
string connectionString = &quot;Provider=Microsoft.Jet.OleDb.4.0; Data Source=C:\Sample.xls; Extended Properties=Excel 8.0;&quot;
// Establish a connection to the data source.
using(OleDbConnection Connection = new OleDbConnection(connectionString))
{
Connection.Open()
//reading data from excel to Data Table
using(OleDbCommand command = new OleDbCommand())
{
command.Connection = Connection;
command.CommandText = &quot;SELECT * FROM [Sample]&quot;;
using(OleDbDataAdapter adapter =new OleDbDataAdapter())
{
adapter.SelectCommand = command;
adapter.Fill(dt);
}
}
}
</pre>
<p>You can get more information from Microsoft support site<br />
<a href="http://support.microsoft.com/kb/306023" target="_blank"><br />
http://support.microsoft.com/kb/306023 &#8211; How to transfer data to an Excel workbook by using Visual C# 2005 or Visual C# .NET</a></p>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2010/02/16/import-data-from-excel-using-c-part-2/" title="Permanent link to Import Data from Excel using C# &#8211; Part 2">Import Data from Excel using C# &#8211; Part 2</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/12/31/text-truncated-to-255-characters-when-reading-from-excel-using-oledb/" title="Permanent link to Text truncated to 255 characters when reading from Excel using OLEDB">Text truncated to 255 characters when reading from Excel using OLEDB</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/10/22/implementing-custom-paging-in-datarepeater-using-c-and-sql-server/" title="Permanent link to Implementing Custom Paging in DataRepeater using C# and SQL Server">Implementing Custom Paging in DataRepeater using C# and SQL Server</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/10/07/how-to-store-and-retrieve-files-from-sql-server-database/" title="Permanent link to How to Store and Retrieve files from SQL Server Database">How to Store and Retrieve files from SQL Server Database</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/04/27/using-multiple-active-result-sets-mars/" title="Permanent link to Using Multiple Active Result Sets (MARS)">Using Multiple Active Result Sets (MARS)</a>  </li>
</ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2009/09/23/import-export-data-in-ms-excel-using-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

