<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Learning with blogs</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/lazydeveloper/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/lazydeveloper/feed.xml</link>
    <description />
    <lastBuildDate>Wed, 17 Apr 2019 03:43:02 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Using ADO.NET to work with Excel files</title>
      <link>https://docs.microsoft.com/archive/blogs/lazydeveloper/using-ado-net-to-work-with-excel-files</link>
      <pubDate>Mon, 15 Mar 2010 10:04:00 GMT</pubDate>
      <dc:creator><![CDATA[lazydeveloper]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/lazydeveloper/2010/03/15/using-ado-net-to-work-with-excel-files/</guid>
      <description><![CDATA[This articles gives a quick overview of how we can Create Excel files/Modify existing excel...]]></description>
      <content:encoded><![CDATA[<P>This articles gives a quick overview of how we can Create Excel files/Modify existing excel files/Insert data to an excel file using OleDB providers. The code&nbsp; describes use of Jet oleDB provider , hence it assumes that application is running on x86 platform or atleast built on x86 platform(See build configuration of your project).&nbsp; I will describe later in this article on How can we make it work for 64 bit platform without modifying build configurations.</P>
<P>First of all we need to create a connection to an Excel sheet. the below string is a sample connection string (i have assumed that D:\\Book1.xls&nbsp; is the excel file location.)</P>
<P><EM>private const string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Book1.xls;Extended Properties=\"Excel 8.0;HDR=YES;\"";</EM></P>
<P>the value HDR=YES describes that excel contains the Header Rows and Insert/Updates will be done on the basis of Header column names. In case header row doesn’t exists , we need to provide <EM>HDR=NO </EM>in connection string.</P>
<P>Now comes the main part of performing various excel operations.</P>
<P>1-<STRONG>Reading an Existing Excel sheet:</STRONG> Below is a working piece of code , you will need to execute to Read data from excel.</P>
<BLOCKQUOTE>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <EM>DataSet dsData = new DataSet();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string command = "Select * from [Sheet1$]";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OleDbConnection connection = new OleDbConnection(connectionString);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OleDbDataAdapter adaptor = new OleDbDataAdapter(command, connection);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; adaptor.Fill(dsData);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connection.Close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return dsData;</EM></P></BLOCKQUOTE>
<P>Note:We need to tell command string , as to which worksheet we want to pull data from ,like Sheet1 in the code above . My excel file contains a worksheet named Sheet1 .</P>
<P>2-<STRONG>Populating Data in an Excel file: </STRONG>Sample code for inserting data in excel file. in Below code i am inserting data in to Sheet1 worksheet. and i have a Header row with column names col1 , col2 , col3.</P>
<BLOCKQUOTE>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <EM>string commandTxt = "Insert Into [Sheet1$](col1,col2,col3) Values(2,4,5)";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OleDbConnection connection = new OleDbConnection(connectionString);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connection.Open();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OleDbCommand command = new OleDbCommand(commandTxt ,connection);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; command.ExecuteNonQuery(); </EM></P></BLOCKQUOTE>
<BLOCKQUOTE>
<P><EM>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connection.Close();</EM></P></BLOCKQUOTE>
<P>3-<STRONG>Creating a New Sheet in excel</STRONG>: Sample code for Creating a new sheet named Sheet6 and inserting data in to it. 
<BLOCKQUOTE>
<P>string commandTxt = "CREATE TABLE Sheet6(Id char(255), Name char(255), BirthDate date)";<BR>OleDbConnection connection = new OleDbConnection(connectionString);<BR>connection.Open();<BR>OleDbCommand command = new OleDbCommand(commandTxt ,connection);<BR>command.ExecuteNonQuery();<BR>command.CommandText = "INSERT INTO Sheet6 (Id, Name, BirthDate) values ('AAA', 'Andrew', '12/4/1955')";<BR>command.ExecuteNonQuery();<BR>connection.Close();</P></BLOCKQUOTE>
<P>4-Creating a Header Row: Based on my experience on working with excel files , i have found that it is always easy and productive to work with excel files when already have a template available. andin such scenarios above piece of code should do the trick. But in real life , not always can we guarranty that we will have a fix template available , which can be used to populate the data.&nbsp; In such scenarios ,we can first create a template with header row and then populate it with DataSet , as explained above. 
<P>As per Microsoft’s documentation , the below command line should work fine,if we want to insert a header row. 
<P>string commandTxt = "Insert Into [Sheet2$](F1,F2,F3) Values('NewCol','NewCol2','NewCol3')"; 
<P>but i wasn’t lucky enough to get this working. (i tried both HDR=YES and HDR=NO , in connection string , still no luck) 
<P>Here is what i used to insert Header Row in excel. 
<P>Now coming to point of working with xlsx files and on 64 bit platform , Microsoft has release a new driver (ACE 12.0)&nbsp; for these scenarios. So essentially&nbsp; by just modifying the connection string , we should be good to work in all scenarios. 
<P>that’s it for now, ciao 
<P>Updates: Added code for creating a header row in excel.</P>
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:0767317B-992E-4b12-91E0-4F059A8CECA8:fda9718c-5454-46d9-949f-76b21ab52c6a class=wlWriterEditableSmartContent contentEditable=false>Technorati Tags: <A href="https://technorati.com/tags/C%23" rel=tag><FONT color=#0066a7>C#</FONT></A>,<A href="https://technorati.com/tags/ADO.NET" rel=tag><FONT color=#0066a7>ADO.NET</FONT></A>,<A href="https://technorati.com/tags/oledb" rel=tag><FONT color=#0066a7>oledb</FONT></A>,<A href="https://technorati.com/tags/Excel" rel=tag><FONT color=#0066a7>Excel</FONT></A>,<A href="https://technorati.com/tags/Jet+friver" rel=tag><FONT color=#0066a7>Jet friver</FONT></A>,<A href="https://technorati.com/tags/ACE+12.0" rel=tag><FONT color=#0066a7>ACE 12.0</FONT></A>,<A href="https://technorati.com/tags/oleDbConnection" rel=tag><FONT color=#0066a7>oleDbConnection</FONT></A></DIV>]]></content:encoded>
    </item>
    <item>
      <title>What's New with C# 4.0??</title>
      <link>https://docs.microsoft.com/archive/blogs/lazydeveloper/whats-new-with-c-4-0</link>
      <pubDate>Mon, 15 Mar 2010 10:02:00 GMT</pubDate>
      <dc:creator><![CDATA[lazydeveloper]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/lazydeveloper/2010/03/15/whats-new-with-c-4-0/</guid>
      <description><![CDATA[If you have worked with C# vastly, you know it very well that it is a static typed language which...]]></description>
      <content:encoded><![CDATA[<P>If you have worked with C# vastly, you know it very well that it is a static typed language which means that before you can compile the code you have to have Type defined for variable( at least in most of scenarios). Contrary to this in real situation we see that ,inherent nature of objects is ,being Dynamic. This point is the main theme of C# 4.0. There are many very good improvements in C# 4.0 in terms of features/interoperability with COM etc. In this post i will just List out the feature level improvements and small details of them. (with a promise that i will have all of them explained with working code in upcoming posts)</P>
<P>1- <STRONG>Dynamic Look Up</STRONG>: Dynamic lookup allows you to write method, operator and indexer calls, property and field accesses, and even object invocations which bypass the C# static type checking and instead gets resolved at runtime.</P>
<BLOCKQUOTE>
<P>C# introduces a new static type “<EM>dynamic</EM>” , using which you can perform the operation which gets resolved only at run-time. C# compiler allows you to call any method with any name and any signature with such dynamic types. Only during run time , the object will be examined to see what that call actually means.</P></BLOCKQUOTE>
<P>2- <STRONG>Named and Optional parameters: in C#</STRONG> , we tend to create overload of a method&nbsp; to support calling with distinct number of parameters. there are situation where we are forced to pass parameters ,even though those might not be actually used in that call(or we will need those many overloads of that method). the problem gets worse when we use office PIA’s where there are number of parameters, which we know are optional. But we still have to create dummy parameters and pass it to method calls. C# 4.0 simplifies this approach with the introduction of “Optional Parameters”. So during the method declaration, we can now specify the default value and this value will be used , in case not value is passed by caller.</P>
<P>ex.&nbsp; <EM>public bool GetCountOfMethods(int i , string a=”test”);</EM></P>
<P>We can call this method in two ways</P>
<P><EM>GetCountOfMethods(5);</EM></P>
<P>and <EM>GetCountOfMethods(5 , “Don’t test”);</EM></P>
<P>In case of first call ,&nbsp; the default value i.e.”test” will be used for argument a.</P>
<P>The other important feature if Named Arguments. So now it is not compulsory to pass the arguments in the order they are in method declaration. Instead you can pass the arguments by parameters names. for ex. above method can be called like</P>
<P><EM>GetCountOfMethods(i:6 , a:”Foo”)</EM>&nbsp; </P>
<P>and Order of parameters also doesn’t matter if names are provided for method arguments.</P>
<P>3-<STRONG>COM specific improvements</STRONG>: The above two feature helps a lot when working with COM API’s in C# 4.0 . but there are couple of COM specific improvements also which simplifies the work of developer even furthur.</P>
<BLOCKQUOTE>
<P>a- Dynamic Import: Many COM methods accept and returns variants types and are represented in PIA’s as object types. So even though a developer knows the static type of a returned object , he is required to perform a explicit cast operation on the returned value to make use of it.</P>
<P>To facilitate the better development experience , it is not possible to use dynamic types instead of variants in COM PIA’s. and then a static type value can be directly assigned to it , with explicit cast operation.</P>
<P>This means that you can easily access members directly off a returned object, or you can assign it to a strongly typed local variable without having to cast. To illustrate, you can now say 
<P><EM>excel.Cells[1, 1].Value = "Hello";</EM> 
<P>instead of 
<P><EM>((Excel.Range)excel.Cells[1, 1]).Value2 = "Hello";</EM> 
<P>and 
<P><EM>Excel.Range range = excel.Cells[1, 1];</EM> 
<P>instead of 
<P><EM>Excel.Range range = (Excel.Range)excel.Cells[1, 1];</EM></P></BLOCKQUOTE>
<BLOCKQUOTE>
<P>b. Omitting ref : Due to difference in programming model , COM PIA’s has lot of reference parameters (which is entirely different from C# passing by reference). There for in C# 4.0&nbsp; , all such parameters can now be passed by value . C# compiler will take care of&nbsp; generating temporary variables to hold the passed-in values and discarding the returned values. It simplifies the programming syntax greatly.</P></BLOCKQUOTE>
<P mce_keep="true">&nbsp;</P>
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:0767317B-992E-4b12-91E0-4F059A8CECA8:288d8e09-a414-4465-95d1-1974ad5a89b4 class=wlWriterEditableSmartContent contentEditable=false>Technorati Tags: <A href="https://technorati.com/tags/C%23+4.0" rel=tag mce_href="https://technorati.com/tags/C%23+4.0"><FONT color=#0066a7>C# 4.0</FONT></A>,<A href="https://technorati.com/tags/C%23" rel=tag mce_href="https://technorati.com/tags/C%23"><FONT color=#0066a7>C#</FONT></A>,<A href="https://technorati.com/tags/dynamic+in+C%23" rel=tag mce_href="https://technorati.com/tags/dynamic+in+C%23"><FONT color=#0066a7>dynamic in C#</FONT></A>,<A href="https://technorati.com/tags/VS+2010" rel=tag mce_href="https://technorati.com/tags/VS+2010"><FONT color=#0066a7>VS 2010</FONT></A></DIV>]]></content:encoded>
    </item>
  </channel>
</rss>