<?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/cells/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-4.html</link>
		<comments>http://excelcsharp.lance40.com/post-4.html#comments</comments>
		<pubDate>Sat, 07 Nov 2015 17:07:37 +0000</pubDate>
		<dc:creator><![CDATA[Janga]]></dc:creator>
				<category><![CDATA[セル操作]]></category>
		<category><![CDATA[プロパティ]]></category>

		<guid isPermaLink="false">http://excelcsharp.lance40.com/?p=230</guid>
		<description><![CDATA[二次元配列の値をそのままセルに一括出力する方法です。 二次元配列の各要素が１つのセルに設定されます。 以下の処理では、適当な二次元配列を作成後、指定したセルの範囲に値を出力しています。]]></description>
				<content:encoded><![CDATA[<p>二次元配列の値をそのままセルに一括出力する方法です。</p>
<p>二次元配列の各要素が１つのセルに設定されます。</p>
<p>以下の処理では、適当な二次元配列を作成後、指定したセルの範囲に値を出力しています。</p>
<pre class="brush: csharp; title: ; notranslate">

            #region &quot;work 貼り付ける値を作成&quot;

            // 10行4列イメージの二次元配列
            object[,] setValue = new object[9, 4];

            for (int i = 0; i &lt; 9; i++)
            {
                for (int j = 0; j &lt; 4; j++)
                {
                    setValue[i, j] = string.Format(&quot;{0}行{1}列目&quot;, i + 1, j + 1);
                }
            }

            #endregion


            // 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シート目を操作対象に設定する
            // ※Worksheets[n]はオブジェクト型を返すため、Worksheet型にキャスト
            xlSheet = xlSheets[1] as Microsoft.Office.Interop.Excel.Worksheet;

            // 表示
            xlApp.Visible = true;

            int iColCnt = 0;    //列数カウンタ
            int iRowCnt = 0;    //行数カウンタ
            Microsoft.Office.Interop.Excel.Range xlCellsFrom = null;   //セル始点（中継用）
            Microsoft.Office.Interop.Excel.Range xlRangeFrom = null;   //セル始点
            Microsoft.Office.Interop.Excel.Range xlCellsTo = null;     //セル終点（中継用）
            Microsoft.Office.Interop.Excel.Range xlRangeTo = null;     //セル終点   
            Microsoft.Office.Interop.Excel.Range xlTarget = null;      //配列設定対象レンジ

            //配列の要素数取得
            iRowCnt = setValue.GetLength(0) - 1;
            iColCnt = setValue.GetLength(1) - 1;

            // 貼り付け位置
            int col = 4;
            int row = 5;

            //始点セル取得
            xlCellsFrom = xlSheet.Cells;
            xlRangeFrom = xlCellsFrom[row, col] as Microsoft.Office.Interop.Excel.Range;

            //終点セル取得
            xlCellsTo = xlSheet.Cells;
            xlRangeTo = xlCellsTo[row + iRowCnt, col + iColCnt] as Microsoft.Office.Interop.Excel.Range;

            //貼り付け対象レンジ
            xlTarget = xlSheet.Range[xlRangeFrom, xlRangeTo];

            //◆値設定◆
            xlTarget.Value = setValue;

            // ■■■以下、COMオブジェクトの解放■■■

            // cell解放
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlTarget);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRangeTo);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlCellsTo);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRangeFrom);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlCellsFrom);

            // 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-4.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>セルの値を取得／設定する</title>
		<link>http://excelcsharp.lance40.com/post-3.html</link>
		<comments>http://excelcsharp.lance40.com/post-3.html#comments</comments>
		<pubDate>Sat, 07 Nov 2015 17:04:15 +0000</pubDate>
		<dc:creator><![CDATA[Janga]]></dc:creator>
				<category><![CDATA[セル操作]]></category>
		<category><![CDATA[プロパティ]]></category>

		<guid isPermaLink="false">http://excelcsharp.lance40.com/?p=228</guid>
		<description><![CDATA[指定したセルの値を取得／設定します。 以下の処理は、行列番号でセルを指定し、そのセルに任意の値を設定し、入力された値を取得しています。]]></description>
				<content:encoded><![CDATA[<p>指定したセルの値を取得／設定します。</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シート目を操作対象に設定する
            // ※Worksheets[n]はオブジェクト型を返すため、Worksheet型にキャスト
            xlSheet = xlSheets[1] as Microsoft.Office.Interop.Excel.Worksheet;

            // 表示
            xlApp.Visible = true;

            // セルのオブジェクト
            Microsoft.Office.Interop.Excel.Range xlRange = null;
            Microsoft.Office.Interop.Excel.Range xlCells = null;

            // B3セルを指定
            xlCells = xlSheet.Cells;
            xlRange = xlCells[3, 2] as Microsoft.Office.Interop.Excel.Range;

            // ◆現在の値を表示◆
            MessageBox.Show(xlRange.Value);

            // ◆値を設定◆
            xlRange.Value = &quot;変更後の値&quot;;

            // 変更後の値を表示
            MessageBox.Show(xlRange.Value);

            // ■■■以下、COMオブジェクトの解放■■■

            // cell解放
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRange);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlCells);

            // 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-3.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
