{"id":144,"date":"2016-09-29T18:28:19","date_gmt":"2016-09-29T09:28:19","guid":{"rendered":"http:\/\/okamurax.com\/?p=144"},"modified":"2016-12-23T09:52:07","modified_gmt":"2016-12-23T00:52:07","slug":"c-%e3%82%a8%e3%82%af%e3%82%bb%e3%83%ab%e3%82%92%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=144","title":{"rendered":"C# Excel\u64cd\u4f5c\u2460"},"content":{"rendered":"<p>&nbsp;<\/p>\n<pre class=\"lang:c# decode:true \">using System;\r\nusing System.Collections.Generic;\r\nusing System.ComponentModel;\r\nusing System.Data;\r\nusing System.Drawing;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\nusing System.Windows.Forms;\r\n\r\nusing System.Drawing.Printing;\r\n\r\nusing Excel = Microsoft.Office.Interop.Excel;\r\n\/\/ using Microsoft.Office.Interop; \u3053\u306e\u66f8\u304d\u65b9\u3067\u3082\u3044\u3051\u308b\u304b\u3068\u601d\u3063\u305f\u3051\u3069\u30c0\u30e1\u3060\u3063\u305f\u3002\r\n\r\nnamespace Dia\r\n{\r\n    public partial class Form1 : Form\r\n    {\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n\r\n            Button b = new Button();\r\n            b.Text = \"Click Me\";\r\n            b.Location = new Point(2, 2);\r\n            b.Size = new Size(100,20);\r\n            Controls.Add(b);\r\n\r\n            b.Click += new EventHandler((object sender, EventArgs e) =&gt; {\r\n\r\n                PrintDocument pd = new PrintDocument();\r\n\r\n                pd.PrintPage += new PrintPageEventHandler((object psender, PrintPageEventArgs pe)=&gt; {\r\n\r\n                    MessageBox.Show(pe.PageBounds.ToString());\r\n\r\n                });\r\n\r\n                pd.Print();\r\n\r\n            });\r\n           \r\n        }\r\n\r\n        private void button1_Click(object sender, EventArgs e)\r\n        {\r\n\r\n            test_script();\r\n\r\n            return;\r\n\r\n            \/\/\u53c2\u7167\u300cMicrosoft Exce xx.x Object Library\u300d\r\n            Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();\r\n            xl.Visible = true;\r\n            System.Threading.Thread.Sleep(1000);\r\n            xl.Quit();\r\n            System.Runtime.InteropServices.Marshal.ReleaseComObject(xl);\r\n\r\n        }\r\n\r\n        private void test_script()\r\n        {\r\n            \/\/ \u5fc5\u8981\u306a\u5909\u6570\u306f try \u306e\u5916\u3067\u5ba3\u8a00\u3059\u308b\r\n            Excel.Application xlApplication = null;\r\n\r\n            \/\/ COM \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u89e3\u653e\u3092\u4fdd\u8a3c\u3059\u308b\u305f\u3081\u306b try \uff5e finally \u3092\u4f7f\u7528\u3059\u308b\r\n            try\r\n            {\r\n                xlApplication = new Excel.Application();\r\n\r\n                \/\/ \u8b66\u544a\u30e1\u30c3\u30bb\u30fc\u30b8\u306a\u3069\u3092\u8868\u793a\u3057\u306a\u3044\u3088\u3046\u306b\u3059\u308b\r\n                xlApplication.DisplayAlerts = false;\r\n\r\n                Excel.Workbooks xlBooks = xlApplication.Workbooks;\r\n\r\n                try\r\n                {\r\n                    Excel.Workbook xlBook = xlBooks.Add(string.Empty);\r\n\r\n                    try\r\n                    {\r\n                        Excel.Sheets xlSheets = xlBook.Worksheets;\r\n\r\n                        try\r\n                        {\r\n                            Excel.Worksheet xlSheet = (Excel.Worksheet)xlSheets[1];\r\n\r\n                            try\r\n                            {\r\n                                Excel.Range xlCells = xlSheet.Cells;\r\n\r\n                                try\r\n                                {\r\n                                    Excel.Range xlRange = (Excel.Range)xlCells[6, 4];\r\n\r\n                                    try\r\n                                    {\r\n                                        \/\/ Microsoft Excel \u3092\u8868\u793a\u3059\u308b\r\n                                        xlApplication.Visible = true;\r\n\r\n                                        \/\/ 1000 \u30df\u30ea\u79d2 (1\u79d2) \u5f85\u6a5f\u3059\u308b\r\n                                        System.Threading.Thread.Sleep(1000);\r\n\r\n                                        \/\/ Row=6, Column=4 \u306e\u4f4d\u7f6e\u306b\u6587\u5b57\u3092\u30bb\u30c3\u30c8\u3059\u308b\r\n                                        xlRange.Value2 = \"\u3042\u3068 1 \u79d2\u3067\u7d42\u4e86\u3057\u307e\u3059\";\r\n\r\n                                        \/\/ 1000 \u30df\u30ea\u79d2 (1\u79d2) \u5f85\u6a5f\u3059\u308b\r\n                                        System.Threading.Thread.Sleep(1000);\r\n                                    }\r\n                                    finally\r\n                                    {\r\n                                        if (xlRange != null)\r\n                                        {\r\n                                            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRange);\r\n                                        }\r\n                                    }\r\n                                }\r\n                                finally\r\n                                {\r\n                                    if (xlCells != null)\r\n                                    {\r\n                                        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlCells);\r\n                                    }\r\n                                }\r\n                            }\r\n                            finally\r\n                            {\r\n                                if (xlSheet != null)\r\n                                {\r\n                                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);\r\n                                }\r\n                            }\r\n                        }\r\n                        finally\r\n                        {\r\n                            if (xlSheets != null)\r\n                            {\r\n                                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets);\r\n                            }\r\n                        }\r\n                    }\r\n                    finally\r\n                    {\r\n                        if (xlBook != null)\r\n                        {\r\n                            try\r\n                            {\r\n                                xlBook.Close();\r\n                            }\r\n                            finally\r\n                            {\r\n                                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook);\r\n                            }\r\n                        }\r\n                    }\r\n                }\r\n                finally\r\n                {\r\n                    if (xlBooks != null)\r\n                    {\r\n                        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks);\r\n                    }\r\n                }\r\n            }\r\n            finally\r\n            {\r\n                if (xlApplication != null)\r\n                {\r\n                    try\r\n                    {\r\n                        xlApplication.Quit();\r\n                    }\r\n                    finally\r\n                    {\r\n                        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApplication);\r\n                    }\r\n                }\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using S &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=144\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;C# Excel\u64cd\u4f5c\u2460&#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-144","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\/144","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=144"}],"version-history":[{"count":4,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/144\/revisions"}],"predecessor-version":[{"id":5091,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/144\/revisions\/5091"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}