{"id":2676,"date":"2019-02-27T11:26:08","date_gmt":"2019-02-27T02:26:08","guid":{"rendered":"http:\/\/okamurax.com\/?p=2676"},"modified":"2024-05-07T16:11:56","modified_gmt":"2024-05-07T07:11:56","slug":"c-%e3%82%a8%e3%82%af%e3%82%bb%e3%83%ab%e3%81%ab%e6%9b%b8%e3%81%8d%e8%be%bc%e3%81%bf%e5%9f%ba%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=2676","title":{"rendered":"C# Excel\u64cd\u4f5c\u2462"},"content":{"rendered":"<pre class=\"lang:c# decode:true \">using System;\r\nusing System.Windows.Forms;\r\n\/\/ \u53c2\u7167\u306e\u8ffd\u52a0&gt;COM&gt;Microsoft Excel 16.0 Object Library\r\nnamespace ProductionManagement\r\n{\r\n    class Excel\r\n    {\r\n        public void CreateBook()\r\n        {\r\n            if (Type.GetTypeFromProgID(\"Excel.Application\") == null) return;\r\n\r\n            SaveFileDialog saveFileDialog = new SaveFileDialog();\r\n            saveFileDialog.FileName = \"title\";\r\n            saveFileDialog.Filter = \"xlsx | *.xlsx\";\r\n            if (saveFileDialog.ShowDialog() != DialogResult.OK) return;\r\n\r\n            var excel = new Microsoft.Office.Interop.Excel.Application();\r\n            excel.DisplayAlerts = false;\r\n            var books = excel.Workbooks;\r\n            var book = books.Add();\r\n            var sheets = book.Sheets;\r\n            var sheet = sheets[1];\r\n            var cells = sheet.cells;\r\n\r\n            WriteCell(cells, \"Hello World\", 2, 2);\r\n            book.SaveCopyAs(saveFileDialog.FileName);\r\n\r\n            ComRelease(cells);\r\n            ComRelease(sheet);\r\n            ComRelease(sheets);\r\n            book.Close();\r\n            ComRelease(book);\r\n            ComRelease(books);\r\n            excel.DisplayAlerts = true;\r\n            excel.Quit();\r\n            ComRelease(excel);\r\n\r\n            MessageBox.Show(\"\u7d42\u4e86\u3057\u307e\u3057\u305f\u3002\");\r\n        }\r\n\r\n        private void WriteCell(dynamic cells, string value, int r, int c)\r\n        {\r\n            var cell = cells[r, c];\r\n            cell.value = value;\r\n            ComRelease(cell);\r\n        }\r\n\r\n        private void ComRelease(dynamic target)\r\n        {\r\n            System.Runtime.InteropServices.Marshal.ReleaseComObject((object)target);\r\n            target = null;\r\n        }\r\n\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>using System; using System.Windows.Forms; \/\/ \u53c2\u7167\u306e\u8ffd\u52a0&gt;COM&gt;Microsoft Excel 16.0 Object Library namespace Pro &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=2676\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;C# Excel\u64cd\u4f5c\u2462&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9],"class_list":["post-2676","post","type-post","status-publish","format-standard","hentry","category-1","tag-c-net"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2676","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2676"}],"version-history":[{"count":4,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2676\/revisions"}],"predecessor-version":[{"id":6369,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2676\/revisions\/6369"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}