{"id":3758,"date":"2016-07-10T14:15:06","date_gmt":"2016-07-10T14:15:06","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=3758"},"modified":"2016-07-10T14:15:06","modified_gmt":"2016-07-10T14:15:06","slug":"automating-excel-some-basics","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/automating-excel-some-basics\/","title":{"rendered":"Automating Excel (some basics)"},"content":{"rendered":"<p>Here&#8217;s some basic for automating Excel from C#.<\/p>\n<p><strong>Make sure you dereference your Excel COM objects<\/strong><\/p>\n<p><em>Actually I&#8217;m going to start with a word of caution. When interacting with Excel you need to ensure that you dereference any Excel objects after use or you&#8217;ll find Excel remains in memory when you probably thought it had been closed.<\/em><\/p>\n<p>To correctly deal with Excel&#8217;s COM objects the best thing to do is store each object in a variable and when you&#8217;ve finished with it, make sure you set that variable to null. Accessing some Excel objects using simply dot notation such as <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\napplication.Workbooks&#x5B;0].Sheets&#x5B;1];\r\n<\/pre>\n<p>will result in COM objects being created but without your application having a reference to them they&#8217;ll remain referenced long after you expect. <\/p>\n<p>Instead do things like<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar workbooks = application.Workbooks&#x5B;0];\r\nvar workSheet = workbooks.Sheets&#x5B;1];\r\n<\/pre>\n<p>If in doubt, check via Task Manager to see if your instance of Excel has been closed.<\/p>\n<p><strong>Starting Excel<\/strong><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar application = new Excel.Application();\r\nvar workbook = application.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);\r\nExcel.Worksheet worksheet = workbook.Sheets&#x5B;1];\r\n\r\napplication.Visible = true;\r\n<\/pre>\n<p><strong>Setting Cell data<\/strong><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nworksheet.Cells&#x5B;row, column++] = \r\n    cell.Value != null ? \r\n       cell.Value.ToString() : \r\n       String.Empty;\r\n<\/pre>\n<p><strong>Grouping a range<\/strong><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nExcel.Range range = worksheet.Rows&#x5B;String.Format(&quot;{0}:{1}&quot;, row, row + children)];\r\nrange.OutlineLevel = indent;\r\nrange.Group(Missing.Value, Missing.Value, Missing.Value, Missing.Value);\r\n<\/pre>\n<p><strong>Change the background colour<\/strong><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nworksheet.Rows&#x5B;row].Interior.Color = Excel.XlRgbColor.rgbRed;\r\n<\/pre>\n<p><strong>Change the background colour from a Color object<\/strong><\/p>\n<p>We can use the built-in colour conversion code, which from WPF would mean converting to a System.Drawing.Color, as per this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tSystem.Drawing.Color clr = System.Drawing.Color.FromArgb(solid.Color.A, solid.Color.R, solid.Color.G, solid.Color.B);\r\n<\/pre>\n<p>Now we can use this as follows<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nworksheet.Rows&#x5B;row].Interior.Color = ColorTranslator.ToOle(clr);\r\n<\/pre>\n<p>or we can do this ourselves using<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nint clr = solid.Color.R | solid.Color.G &lt;&lt; 8 | solid.Color.B &lt;&lt; 16;\t\t\t\t\t\t\t\t\tworksheet.Rows&#x5B;row].Interior.Color = clr;\r\n<\/pre>\n<p><strong>Changing the foreground colour<\/strong><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nint clr = solid.Color.R | solid.Color.G &lt;&lt; 8 | solid.Color.B &lt;&lt; 16;\t\t\t\t\t\t\t\t\tworksheet.Rows&#x5B;row].Font.Color = clr;\r\n<\/pre>\n<p><strong>References<\/strong><\/p>\n<p>https:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.office.interop.excel.aspx<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s some basic for automating Excel from C#. Make sure you dereference your Excel COM objects Actually I&#8217;m going to start with a word of caution. When interacting with Excel you need to ensure that you dereference any Excel objects after use or you&#8217;ll find Excel remains in memory when you probably thought it had [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,54],"tags":[],"class_list":["post-3758","post","type-post","status-publish","format-standard","hentry","category-c","category-excel"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3758","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=3758"}],"version-history":[{"count":11,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3758\/revisions"}],"predecessor-version":[{"id":4138,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3758\/revisions\/4138"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=3758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=3758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=3758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}