{"id":798,"date":"2012-03-02T11:36:13","date_gmt":"2012-03-02T11:36:13","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=798"},"modified":"2022-02-17T06:24:03","modified_gmt":"2022-02-17T06:24:03","slug":"reading-from-excel","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/office\/excel\/reading-from-excel","title":{"rendered":"Reading From Excel"},"content":{"rendered":"<h4>Adding Excel Functionality To Your Project<\/h4>\n<h5>Add Microsoft Excel Reference<\/h5>\n<p>Menu &gt; Project &gt; Properties &gt; Common Properties &gt; Add Reference &gt; .Net &gt; Microsoft.Office.Interop.Excel (choose the latest version)<\/p>\n<h5>Add Namespaces<\/h5>\n<pre><code>\r\n\tusing namespace Microsoft::Office::Core;\r\n\t\/\/using namespace Microsoft::Office::Interop::Excel;\t\t\/\/To avoid pain with errors due to clashes with \"Application\" by using this we use the below instead\r\n\tusing namespace Microsoft::Office::Interop;\r\n<\/code><\/pre>\n<h5>Create Object<\/h5>\n<pre><code>\r\n\tprivate: Excel::Application ^ExcelApp1;\r\n\r\n\tExcelApp1 = gcnew Excel::Application();\r\n<\/code><\/pre>\n<h4>Reading Cells In A Worksheet<\/h4>\n<pre><code>\r\n\ttry\r\n\t{\r\n\t\ttxtOutput-&gt;Text = \"\";\r\n\r\n\r\n\t\t\/\/----- OPEN EXCEL WORKBOOK -----\r\n\t\tExcel::Workbook ^WorkBook1 = ExcelApp1-&gt;Workbooks-&gt;Open(\"C:\\\\_Downloaded\\\\test.xlsx\",\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t 0,\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t true,\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t 5,\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t \"\",\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t \"\",\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t true,\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t Excel::XlPlatform::xlWindows,\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t \"\\t\",\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t false,\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t false,\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t 0,\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t true,\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t 1,\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t 0);\r\n\r\n\r\n\r\n\t\t\/\/----- OPEN THE WORKSHEET -----\r\n\t\t\/* Use this if you need to deal with workbooks with multiple sheets\r\n\t\tint SheetNumber;\r\n\t\tfor (SheetNumber = 1; SheetNumber &lt;= WorkBook1-&gt;Sheets-&gt;Count; SheetNumber++)\r\n\t\t{\r\n\t\t}\r\n\t\t*\/\r\n\t\tExcel::Worksheet ^WorkSheet1 = (Excel::Worksheet^)WorkBook1-&gt;ActiveSheet;\r\n\r\n\t\t\/\/If you don't know which cells your after you can use this:\r\n\t\t\/\/Excel::Range ^ExcelRange1 = Sheet1-&gt;UsedRange;\r\n\t\t\r\n\r\n\t\t\/\/----- READ WORKSHEET -----\r\n\t\tint RowIndex = 1;\r\n\t\tint ColumnIndex = 1;\r\n\t\tString ^CellValue;\r\n\r\n\t\tfor (RowIndex = 1; RowIndex &lt;= 10; RowIndex++)\r\n\t\t{\r\n\t\t\t\/\/----- READ NEXT ROW -----\r\n\t\t\tfor (ColumnIndex = 1; ColumnIndex &lt;= 3; ColumnIndex++)\t\r\n\t\t\t{\r\n\t\t\t\t\/\/----- READ NEXT COLUMN -----\r\n\t\t\t\tif (((Excel::Range^)WorkSheet1-&gt;Cells[(System::Object^)RowIndex, (System::Object^)ColumnIndex])-&gt;Value2 != nullptr)\r\n\t\t\t\t{\r\n\t\t\t\t\t\/\/CELL HAS A VALUE\r\n\t\t\t\t\tCellValue = ((Excel::Range^)WorkSheet1-&gt;Cells[(System::Object^)RowIndex, (System::Object^)ColumnIndex])-&gt;Value2-&gt;ToString();\r\n\t\t\t\t\t\r\n\t\t\t\t\ttxtOutput-&gt;Text += \"'\" + CellValue + \"'\";\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\t\/\/CELL IS BLANK\r\n\t\t\t\t\ttxtOutput-&gt;Text += \"#\";\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\ttxtOutput-&gt;Text += \"\\r\\n\";\r\n\t\t}\r\n\t    \r\n\t\t\/\/----- CLOSE WORKBOOK -----\r\n\t\tWorkBook1-&gt;Close(false, \"C:\\\\_Downloaded\\\\test.xlsx\", nullptr);\r\n\r\n\r\n\t}\r\n\tcatch (Exception ^e)\r\n\t{\r\n\t\tMessageBox::Show(L\"Error:\\n\" + e, L\"Error\", MessageBoxButtons::OK, MessageBoxIcon::Error);\r\n\t}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Adding Excel Functionality To Your Project Add Microsoft Excel Reference Menu &gt; Project &gt; Properties &gt; Common Properties &gt; Add Reference &gt; .Net &gt; Microsoft.Office.Interop.Excel (choose the latest version) Add Namespaces using namespace Microsoft::Office::Core; \/\/using namespace Microsoft::Office::Interop::Excel; \/\/To avoid pain with errors due to clashes with &#8220;Application&#8221; by using this we use the below instead [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[84],"tags":[],"class_list":["post-798","post","type-post","status-publish","format-standard","hentry","category-excel"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/798","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=798"}],"version-history":[{"count":3,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/798\/revisions"}],"predecessor-version":[{"id":801,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/798\/revisions\/801"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}