<?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>Dot Net Thoughts &#187; Excel</title>
	<atom:link href="http://www.dotnetthoughts.net/tag/excel/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>thoughts about .Net, WPF, Sharepoint, Javascript and more.</description>
	<lastBuildDate>Wed, 28 Jul 2010 09:59:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Import Data from Excel using C# &#8211; Part 2</title>
		<link>http://www.dotnetthoughts.net/2010/02/16/import-data-from-excel-using-c-part-2/</link>
		<comments>http://www.dotnetthoughts.net/2010/02/16/import-data-from-excel-using-c-part-2/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 18:56:16 +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[Windows Forms]]></category>
		<category><![CDATA[C#.Net]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[Import XSLX]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=770</guid>
		<description><![CDATA[In my web application, we used to import data from MS Excel file. Few days before one of the client representative posted an issue that he can’t import data from XLSX (MS Excel 2007 File Format) files. Previously we were using OLE DB provider to import data from Excel file (Checkout my previous post regarding [...]]]></description>
			<content:encoded><![CDATA[<p>In my web application, we used to import data from MS Excel file. Few days before one of the client representative posted an issue that he can’t import data from XLSX (MS Excel 2007 File Format) files. Previously we were using OLE DB provider to import data from Excel file (Checkout my previous post regarding How to Import / Export from C# :<a href="http://www.dotnetthoughts.net/2009/09/23/import-export-data-in-ms-excel-using-c/"> IMPORT / EXPORT DATA IN MS EXCEL USING C#</a> ), but Microsoft worked on the Office 2007 file formats, and we are not able to connect to Excel using Microsoft.Jet.OLEDB. Yesterday I got a chance to work on this module, and thought of implementing XLSX support. After few searches I found a new provider from Microsoft to connect to Excel 2007 files, called <strong>Microsoft.ACE.OLEDB.12.0</strong>.</p>
<pre class="brush: csharp;">
/// &lt;summary&gt;
/// Imports Data from Microsoft Excel File.
/// &lt;/summary&gt;
/// &lt;param name=&quot;FileName&quot;&gt;Filename from which data need to import&lt;/param&gt;
/// &lt;returns&gt;List of DataTables, based on the number of sheets&lt;/returns&gt;
private List&lt;DataTable&gt; ImportExcel(string FileName)
{
    List&lt;DataTable&gt; _dataTables = new List&lt;DataTable&gt;();
    string _ConnectionString = string.Empty;
    string _Extension = Path.GetExtension(FileName);
    //Checking for the extentions, if XLS connect using Jet OleDB
    if (_Extension.Equals(&quot;.xls&quot;, StringComparison.CurrentCultureIgnoreCase))
    {
        _ConnectionString =
            &quot;Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0};Extended Properties=Excel 8.0&quot;;
    }
    //Use ACE OleDb
    else if (_Extension.Equals(&quot;.xlsx&quot;, StringComparison.CurrentCultureIgnoreCase))
    {
        _ConnectionString =
            &quot;Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0&quot;;
    }

    DataTable dataTable = null;

    using (OleDbConnection oleDbConnection =
        new OleDbConnection(string.Format(_ConnectionString, FileName)))
    {
        oleDbConnection.Open();
        //Getting the meta data information.
        //This DataTable will return the details of Sheets in the Excel File.
        DataTable dbSchema = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables_Info, null);
        foreach (DataRow item in dbSchema.Rows)
        {
            //reading data from excel to Data Table
            using (OleDbCommand oleDbCommand = new OleDbCommand())
            {
                oleDbCommand.Connection = oleDbConnection;
                oleDbCommand.CommandText = string.Format(&quot;SELECT * FROM [{0}]&quot;,
                    item[&quot;TABLE_NAME&quot;].ToString());
                using (OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter())
                {
                    oleDbDataAdapter.SelectCommand = oleDbCommand;
                    dataTable = new DataTable(item[&quot;TABLE_NAME&quot;].ToString());
                    oleDbDataAdapter.Fill(dataTable);
                    _dataTables.Add(dataTable);
                }
            }
        }
    }
    return _dataTables;
}
</pre>
<p>The connection string also supports HDR attribute, which decides, whether the Excel file first row is header or not. It can be either Yes or No. It can be used with both Excel 2003 and Excel 2007 files.</p>
<p>Update: You can download the Microsoft.ACE.OLEDB.12.0 provider from Microsoft : <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=7554F536-8C28-4598-9B72-EF94E038C891&#038;displaylang=en" target="_blank">2007 Office System Driver: Data Connectivity Components</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2010/02/16/import-data-from-excel-using-c-part-2/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<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 is using OleDb provider. You may need to add one more attribute to connection string [...]]]></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;">
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;">
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;">
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>
]]></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>
