<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>C#でExcelを読み込んで操作する方法を公開｜C# Excel操作テクニック.NET &#187; COM解放のみ</title>
	<atom:link href="http://excelcsharp.lance40.com/tag/com%e8%a7%a3%e6%94%be%e3%81%ae%e3%81%bf/feed" rel="self" type="application/rss+xml" />
	<link>http://excelcsharp.lance40.com</link>
	<description>メソッドに貼り付けるだけで機能するソースコードを多数用意しています。</description>
	<lastBuildDate>Sun, 08 Nov 2015 04:41:39 +0000</lastBuildDate>
	<language>ja</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.7.41</generator>
	<item>
		<title>Excelを開いたまま処理を終了させる</title>
		<link>http://excelcsharp.lance40.com/post-2.html</link>
		<comments>http://excelcsharp.lance40.com/post-2.html#comments</comments>
		<pubDate>Sat, 07 Nov 2015 16:56:19 +0000</pubDate>
		<dc:creator><![CDATA[Janga]]></dc:creator>
				<category><![CDATA[ブック操作]]></category>
		<category><![CDATA[COM解放のみ]]></category>
		<category><![CDATA[Excel開いたまま]]></category>

		<guid isPermaLink="false">http://excelcsharp.lance40.com/?p=222</guid>
		<description><![CDATA[Excelが開いた状態でメソッドやアプリを終了しても、プロセスが残らないコードです。 以下の処理を実行すると、Excelの新規Bookが1つ起動します。 処理自体は最後まで流れますが、表示したExcelは開いたままになっ [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Excelが開いた状態でメソッドやアプリを終了しても、プロセスが残らないコードです。</p>
<p>以下の処理を実行すると、Excelの新規Bookが1つ起動します。</p>
<p>処理自体は最後まで流れますが、表示したExcelは開いたままになっており、マウス操作でExcelを閉じてもタスクマネージャにプロセスは残りません。</p>
<p>ポイントは、処理の最後でExcelの終了処理（BOOKやApplicationクラスの終了処理）を行わず、COMオブジェクトの解放のみを行う点です。</p>
<pre class="brush: csharp; title: ; notranslate">

            // Excel操作用オブジェクト
            Microsoft.Office.Interop.Excel.Application xlApp = null;
            Microsoft.Office.Interop.Excel.Workbooks xlBooks = null;
            Microsoft.Office.Interop.Excel.Workbook xlBook = null;
            Microsoft.Office.Interop.Excel.Sheets xlSheets = null;
            Microsoft.Office.Interop.Excel.Worksheet xlSheet = null;

            // Excelアプリケーション生成
            xlApp = new Microsoft.Office.Interop.Excel.Application();

            // 新規のExcelブックを開く
            // Addメソッド
            xlBooks = xlApp.Workbooks;
            xlBook = xlBooks.Add();

            // シートを選択する
            xlSheets = xlBook.Worksheets;
            xlSheet = xlSheets[1] as Microsoft.Office.Interop.Excel.Worksheet; // 1シート目を操作対象に設定する

            // 表示
            xlApp.Visible = true;

            // ■■■以下、COMオブジェクトの解放■■■

            // Sheet解放
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets);

            // ◆プロセスが残らないようにメソッドを抜けるには、
            // 　★の部分をコメントアウトする

            // Book解放
            //xlBook.Close(); //★
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks);

            // Excelアプリケーションを解放
            //xlApp.Quit(); //★
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);


</pre>
]]></content:encoded>
			<wfw:commentRss>http://excelcsharp.lance40.com/post-2.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
