{"id":2249,"date":"2018-03-19T02:33:02","date_gmt":"2018-03-18T17:33:02","guid":{"rendered":"http:\/\/okamurax.com\/?p=2249"},"modified":"2018-03-19T02:33:02","modified_gmt":"2018-03-18T17:33:02","slug":"c-%e3%82%a8%e3%82%af%e3%82%bb%e3%83%ab%e3%82%92%e8%b5%b7%e5%8b%95%e3%81%97%e3%81%a6%e5%80%a4%e3%82%92%e6%9b%b8%e3%81%8d%e8%be%bc%e3%82%80","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=2249","title":{"rendered":"C# Excel\u64cd\u4f5c\u2461"},"content":{"rendered":"<p>\u3053\u306e\u66f8\u304d\u65b9\u3060\u3068\u843d\u3061\u305f\u6642\u306b\u30d7\u30ed\u30bb\u30b9\u304c\u6b8b\u308b\u3002<\/p>\n<pre class=\"lang:c# decode:true\">using System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\nusing System.Windows.Forms;\r\n\r\nusing Microsoft.Office.Interop.Excel;\r\nusing System.Runtime.InteropServices;\r\n\r\nnamespace WindowsFormsApp1\r\n{\r\n    class create_excel\r\n    {\r\n        public void create_order(DataGridView dgv, setting setting)\r\n        {\r\n            var ex = new Microsoft.Office.Interop.Excel.Application();\r\n            ex.Visible = true;\r\n            ex.DisplayAlerts = false;\r\n\r\n            var wbs = ex.Workbooks;\r\n            var wb = wbs.Open(System.Windows.Forms.Application.StartupPath + @\"\\po.xlsx\");\r\n            var wss = wb.Sheets;\r\n            var ws = wss[\"po\"];\r\n\r\n            var _wbs = ex.Workbooks;\r\n            var _wb = _wbs.Add();\r\n            var _wss = _wb.Sheets;\r\n            var _ws = _wss[1];\r\n\r\n            ws.copy(_ws);\r\n            wb.Close();\r\n\r\n            var order = _wss[\"po\"];\r\n\r\n            var selected_rows_index = dgv.SelectedCells.Cast&lt;DataGridViewCell&gt;().Select(x =&gt; x.RowIndex).Distinct();\r\n\r\n            int counter = 18;\r\n            foreach (int selected_row_index in selected_rows_index)\r\n            {\r\n                if (counter == 29)\r\n                {\r\n                    MessageBox.Show(\"\u9078\u629e\u884c\u6570\u304c\u591a\u3044\u305f\u3081\u4e00\u90e8\u8868\u793a\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\");\r\n                    break;\r\n                }\r\n\r\n                if (dgv.Rows[selected_row_index].Cells[setting.id_header].Value != null)\r\n                {\r\n                    var rc = order.Cells[4, 1];\r\n                    if (dgv.Rows[selected_row_index].Cells[\"\u4ed5\u5165\u5148\"].Value != null) rc.value = dgv.Rows[selected_row_index].Cells[\"\u4ed5\u5165\u5148\"].Value.ToString() + \"\u3000\u5fa1\u4e2d\";\r\n                    System.Runtime.InteropServices.Marshal.ReleaseComObject(rc);\r\n                    rc = null;\r\n\r\n                    rc = order.Cells[counter, 1];\r\n                    if(dgv.Rows[selected_row_index].Cells[\"\u54c1\u540d\"].Value != null) rc.value = dgv.Rows[selected_row_index].Cells[\"\u54c1\u540d\"].Value.ToString();\r\n                    System.Runtime.InteropServices.Marshal.ReleaseComObject(rc);\r\n                    rc = null;\r\n\r\n                    rc = order.Cells[counter, 2];\r\n                    if(dgv.Rows[selected_row_index].Cells[\"\u767a\u6ce8\u6570\u91cf\"].Value != null) rc.value = dgv.Rows[selected_row_index].Cells[\"\u767a\u6ce8\u6570\u91cf\"].Value.ToString();\r\n                    System.Runtime.InteropServices.Marshal.ReleaseComObject(rc);\r\n                    rc = null;\r\n\r\n                    rc = order.Cells[counter, 3];\r\n                    if(dgv.Rows[selected_row_index].Cells[\"\u7d0d\u671f\"].Value != null) rc.value = dgv.Rows[selected_row_index].Cells[\"\u7d0d\u671f\"].Value.ToString();\r\n                    System.Runtime.InteropServices.Marshal.ReleaseComObject(rc);\r\n                    rc = null;\r\n\r\n                    rc = order.Cells[counter, 4];\r\n                    if(dgv.Rows[selected_row_index].Cells[\"\u578b\u5f0f\"].Value != null) rc.value = dgv.Rows[selected_row_index].Cells[\"\u578b\u5f0f\"].Value.ToString();\r\n                    System.Runtime.InteropServices.Marshal.ReleaseComObject(rc);\r\n                    rc = null;\r\n\r\n                    counter++;\r\n                }\r\n            }\r\n\r\n            Marshal.ReleaseComObject(order);\r\n            order = null;\r\n\r\n            Marshal.ReleaseComObject(_ws);\r\n            _ws = null;\r\n            Marshal.ReleaseComObject(_wss);\r\n            _wss = null;\r\n            Marshal.ReleaseComObject(_wb);\r\n            _wb = null;\r\n            Marshal.ReleaseComObject(_wbs);\r\n            _wbs = null;\r\n\r\n            Marshal.ReleaseComObject(ws);\r\n            ws = null;\r\n            Marshal.ReleaseComObject(wss);\r\n            wss = null;\r\n            Marshal.ReleaseComObject(wb);\r\n            wb = null;\r\n            Marshal.ReleaseComObject(wbs);\r\n            wbs = null;\r\n\r\n            Marshal.ReleaseComObject(ex);\r\n            ex = null;\r\n        }\r\n\r\n        public void dgv_copy(DataGridView dgv)\r\n        {\r\n            var ex = new Microsoft.Office.Interop.Excel.Application();\r\n            ex.Visible = true;\r\n            ex.DisplayAlerts = false;\r\n\r\n            var wbs = ex.Workbooks;\r\n            var wb = wbs.Add();\r\n            var wss = wb.Sheets;\r\n            var ws = wss[1];\r\n            \r\n            for (int r = 0; r &lt; dgv.Rows.Count; r++)\r\n            {\r\n                for (int c = 0; c &lt; dgv.Columns.Count; c++)\r\n                {\r\n                    var rc = ws.Cells[r + 1, c + 1];\r\n                    if (dgv.Rows[r].Cells[c].Value != null) rc.Value = dgv.Rows[r].Cells[c].Value.ToString();\r\n                    System.Runtime.InteropServices.Marshal.ReleaseComObject(rc);\r\n                    rc = null;\r\n                }\r\n            }\r\n\r\n            Marshal.ReleaseComObject(ws);\r\n            ws = null;\r\n            Marshal.ReleaseComObject(wss);\r\n            wss = null;\r\n            Marshal.ReleaseComObject(wb);\r\n            wb = null;\r\n            Marshal.ReleaseComObject(wbs);\r\n            wbs = null;\r\n\r\n            Marshal.ReleaseComObject(ex);\r\n            ex = null;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u306e\u66f8\u304d\u65b9\u3060\u3068\u843d\u3061\u305f\u6642\u306b\u30d7\u30ed\u30bb\u30b9\u304c\u6b8b\u308b\u3002 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=2249\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;C# Excel\u64cd\u4f5c\u2461&#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-2249","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\/2249","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=2249"}],"version-history":[{"count":3,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2249\/revisions"}],"predecessor-version":[{"id":5090,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2249\/revisions\/5090"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}