1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Drawing.Printing; using Excel = Microsoft.Office.Interop.Excel; // using Microsoft.Office.Interop; この書き方でもいけるかと思ったけどダメだった。 namespace Dia { public partial class Form1 : Form { public Form1() { InitializeComponent(); Button b = new Button(); b.Text = "Click Me"; b.Location = new Point(2, 2); b.Size = new Size(100,20); Controls.Add(b); b.Click += new EventHandler((object sender, EventArgs e) => { PrintDocument pd = new PrintDocument(); pd.PrintPage += new PrintPageEventHandler((object psender, PrintPageEventArgs pe)=> { MessageBox.Show(pe.PageBounds.ToString()); }); pd.Print(); }); } private void button1_Click(object sender, EventArgs e) { test_script(); return; //参照「Microsoft Exce xx.x Object Library」 Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application(); xl.Visible = true; System.Threading.Thread.Sleep(1000); xl.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xl); } private void test_script() { // 必要な変数は try の外で宣言する Excel.Application xlApplication = null; // COM オブジェクトの解放を保証するために try ~ finally を使用する try { xlApplication = new Excel.Application(); // 警告メッセージなどを表示しないようにする xlApplication.DisplayAlerts = false; Excel.Workbooks xlBooks = xlApplication.Workbooks; try { Excel.Workbook xlBook = xlBooks.Add(string.Empty); try { Excel.Sheets xlSheets = xlBook.Worksheets; try { Excel.Worksheet xlSheet = (Excel.Worksheet)xlSheets[1]; try { Excel.Range xlCells = xlSheet.Cells; try { Excel.Range xlRange = (Excel.Range)xlCells[6, 4]; try { // Microsoft Excel を表示する xlApplication.Visible = true; // 1000 ミリ秒 (1秒) 待機する System.Threading.Thread.Sleep(1000); // Row=6, Column=4 の位置に文字をセットする xlRange.Value2 = "あと 1 秒で終了します"; // 1000 ミリ秒 (1秒) 待機する System.Threading.Thread.Sleep(1000); } finally { if (xlRange != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRange); } } } finally { if (xlCells != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(xlCells); } } } finally { if (xlSheet != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet); } } } finally { if (xlSheets != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets); } } } finally { if (xlBook != null) { try { xlBook.Close(); } finally { System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook); } } } } finally { if (xlBooks != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks); } } } finally { if (xlApplication != null) { try { xlApplication.Quit(); } finally { System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApplication); } } } } } } |