<?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; シート操作</title>
	<atom:link href="http://excelcsharp.lance40.com/category/sheet/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>複数シートを選択する</title>
		<link>http://excelcsharp.lance40.com/post.html</link>
		<comments>http://excelcsharp.lance40.com/post.html#comments</comments>
		<pubDate>Sun, 23 Feb 2014 12:44:24 +0000</pubDate>
		<dc:creator><![CDATA[Janga]]></dc:creator>
				<category><![CDATA[シート操作]]></category>
		<category><![CDATA[メソッド]]></category>
		<category><![CDATA[Selectメソッド引数]]></category>
		<category><![CDATA[シート選択]]></category>
		<category><![CDATA[複数シート]]></category>

		<guid isPermaLink="false">http://excelcsharp.lance40.com/?p=166</guid>
		<description><![CDATA[複数シートを選択状態にするコードです。 注意点は、_Worksheet.Selectメソッドの引数です。 ループ処理で対象シートを選択していますが、最初のループのみ引数にはTrueを設定し、以降のシート選択時はFalse [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>複数シートを選択状態にするコードです。</p>
<p>注意点は、_Worksheet.Selectメソッドの引数です。</p>
<p>ループ処理で対象シートを選択していますが、最初のループのみ引数にはTrueを設定し、以降のシート選択時はFalseを設定しています。</p>
<p>この引数は、「すでに選択しているシートを選択解除するかどうか」の判定です。</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();

            // 既存のBookを開く
            xlBooks = xlApp.Workbooks;
            xlBook = xlBooks.Open(System.IO.Path.GetFullPath(@&quot;..\..\..\data\work01.xlsx&quot;));

            // シートを取得する
            xlSheets = xlBook.Worksheets;

            // 1シート目を操作対象に設定する
            // 作業用変数
            int i = 0;              // ループカウンタ
            bool bActivate = true;  // シート選択時の引数

            // ◆Sheet1とSheet2を選択状態にする◆
            // (Sheet1がアクティブ状態)
            while (i &lt; 2)
            {
                // 2シート目以降はFalseを設定
                if (i &gt; 0)
                    bActivate = false;

                i = i + 1;

                // ※Worksheets[n]はオブジェクト型を返すため、Worksheet型にキャスト
                xlSheet = xlSheets[i] as Microsoft.Office.Interop.Excel.Worksheet;

                // iシート目を選択状態に設定
                xlSheet.Select(bActivate);

                // シートオブジェクト解放
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);

            }

            // 表示
            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.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sheetの名称を取得・設定する</title>
		<link>http://excelcsharp.lance40.com/sheetname.html</link>
		<comments>http://excelcsharp.lance40.com/sheetname.html#comments</comments>
		<pubDate>Fri, 29 Nov 2013 13:11:51 +0000</pubDate>
		<dc:creator><![CDATA[Janga]]></dc:creator>
				<category><![CDATA[シート操作]]></category>
		<category><![CDATA[プロパティ]]></category>
		<category><![CDATA[シート名取得]]></category>
		<category><![CDATA[シート名変更]]></category>

		<guid isPermaLink="false">http://excelcsharp.lance40.com/?p=164</guid>
		<description><![CDATA[操作中Sheetの名称を取得・設定します。 注意点は、Sheet[n]をWorkSheetオブジェクトに設定する際はキャストが必要だということです。]]></description>
				<content:encoded><![CDATA[<p>操作中Sheetの名称を取得・設定します。</p>
<p>注意点は、Sheet[n]をWorkSheetオブジェクトに設定する際はキャストが必要だということです。</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ブックを開く
            xlBooks = xlApp.Workbooks;
            xlBook = xlBooks.Add();

            // シートを選択する
            xlSheets = xlBook.Worksheets;
            // 1シート目を操作対象に設定する
            // ※Worksheets[n]はオブジェクト型を返すため、Worksheet型にキャスト
            xlSheet = xlSheets[1] as Microsoft.Office.Interop.Excel.Worksheet;

            // 表示
            xlApp.Visible = true;

            // Sheet名表示
            MessageBox.Show(xlSheet.Name);

            // ◆シート1の名称を変更する◆
            // Nameプロパティ
            xlSheet.Name = &quot;Sheet999&quot;;

            // Sheet名表示
            MessageBox.Show(xlSheet.Name);


            // ■■■以下、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/sheetname.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ExcelのSheet数を取得する</title>
		<link>http://excelcsharp.lance40.com/sheetcnt.html</link>
		<comments>http://excelcsharp.lance40.com/sheetcnt.html#comments</comments>
		<pubDate>Fri, 29 Nov 2013 12:23:14 +0000</pubDate>
		<dc:creator><![CDATA[Janga]]></dc:creator>
				<category><![CDATA[シート操作]]></category>
		<category><![CDATA[プロパティ]]></category>
		<category><![CDATA[Countプロパティ]]></category>
		<category><![CDATA[シート数取得]]></category>

		<guid isPermaLink="false">http://excelcsharp.lance40.com/?p=147</guid>
		<description><![CDATA[操作対象Excelのシート数を取得します。]]></description>
				<content:encoded><![CDATA[<p>操作対象Excelのシート数を取得します。</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;

            // Excelアプリケーション生成
            xlApp = new Microsoft.Office.Interop.Excel.Application();

            // 既存のBookを開く
            xlBooks = xlApp.Workbooks;
            xlBook = xlBooks.Open(System.IO.Path.GetFullPath(@&quot;..\..\..\data\work01.xlsx&quot;));

            // シートを取得する
            xlSheets = xlBook.Worksheets;

            // 表示
            xlApp.Visible = true;

            // ◆シート数取得◆
            // Countプロパティ
            System.Windows.Forms.MessageBox.Show(xlSheets.Count.ToString());

            // ■■■以下、COMオブジェクトの解放■■■

            // Sheet解放
            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/sheetcnt.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>既存のExcelファイルを開く</title>
		<link>http://excelcsharp.lance40.com/bookopen.html</link>
		<comments>http://excelcsharp.lance40.com/bookopen.html#comments</comments>
		<pubDate>Wed, 20 Nov 2013 14:26:10 +0000</pubDate>
		<dc:creator><![CDATA[Janga]]></dc:creator>
				<category><![CDATA[シート操作]]></category>
		<category><![CDATA[ブック操作]]></category>
		<category><![CDATA[メソッド]]></category>
		<category><![CDATA[Closeメソッド]]></category>
		<category><![CDATA[Excel閉じる]]></category>
		<category><![CDATA[Excel開く]]></category>
		<category><![CDATA[既存ファイル]]></category>

		<guid isPermaLink="false">http://excelcsharp.lance40.com/?p=106</guid>
		<description><![CDATA[既存のExcelファイルを開く処理です。 新規ファイルを開く際はAddメソッドを使用しましたが、既存ファイルを開く場合はOpenメソッドを使用します。 xlBooks.Open(fileNm)の、[fileNm]には、対 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>既存のExcelファイルを開く処理です。<br />
新規ファイルを開く際はAddメソッドを使用しましたが、既存ファイルを開く場合はOpenメソッドを使用します。</p>
<p>xlBooks.Open(fileNm)の、[fileNm]には、対象ファイルの絶対パスを指定します。</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ブックを開く◆
            // Openメソッド
            xlBooks = xlApp.Workbooks;
            xlBook = xlBooks.Open(System.IO.Path.GetFullPath(@&quot;..\..\..\data\work01.xlsx&quot;));

            // シートを選択する
            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/bookopen.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Excelファイルを新規で作成する</title>
		<link>http://excelcsharp.lance40.com/newbook.html</link>
		<comments>http://excelcsharp.lance40.com/newbook.html#comments</comments>
		<pubDate>Wed, 20 Nov 2013 14:19:22 +0000</pubDate>
		<dc:creator><![CDATA[Janga]]></dc:creator>
				<category><![CDATA[シート操作]]></category>
		<category><![CDATA[ブック操作]]></category>
		<category><![CDATA[新規BOOK]]></category>
		<category><![CDATA[新規シート]]></category>
		<category><![CDATA[新規作成]]></category>

		<guid isPermaLink="false">http://excelcsharp.lance40.com/?p=102</guid>
		<description><![CDATA[Excelファイルを新規で開く処理です。 以下の処理を実行すると、Excelの新規Bookが1つ起動します。]]></description>
				<content:encoded><![CDATA[<p>Excelファイルを新規で開く処理です。</p>
<p>以下の処理を実行すると、Excelの新規Bookが1つ起動します。</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;

            // 3秒停止
            System.Threading.Thread.Sleep(3000);

            // ■■■以下、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/newbook.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
