2
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C# Excelセルの結合と結合解除

Last updated at Posted at 2021-10-20

はじめに

エクセルには、セルの結合と結合解除という機能があります。この機能を使うことで、シートをレイアウトする時に、表内の複数のセルをくっつけて1つにすることができます。さあ、今日はSpire.XLS for .NETといライブラリを利用してExcelでセルの結合と結合解除について紹介したいので、お役に少しでも立てたのであれば嬉しいです。

Spire.XLS for .NETとは?

Spire.XLS for .NETは、開発者がC#VB.NETプラットホームでExcelの文書ファイルを迅速かつ高品質で作成・編集・変換・印刷するために設計された専門的な Excelライブラリです。

中には、商用版と無料版のFree Spire.XLS for .NETがありますSpire.XLS for .NETは商用版ではありますが、基本的な機能を搭載しているので、無料試用で日常の仕事にはもう結構だと思います。

下準備

1.E-iceblueの公式サイトからSpire.XLSをダウンロードしてください。

2.Visual Studioを起動して新規プロジェクトを作成してから、インストールされたファイルで相応しいSpire.XLS.dllを参照に追加してください。

メソッド説明

下記の表には、この機能を実行するには必要なメソッドやプロパティが含まれています。

     アイテム

     説明

Workbook.LoadFromFile

Loads the file.

Workbook.SaveToFile

Saves the file.

XlsRange.Merge Method

Creates a merged cell from the specified Range object.

XlsRange.UnMerge Method

Separates a merged area into individual cells.

Workbook.Worksheets Property

Returns a Sheets collection that represents all the worksheets in the specified workbook

Worksheet.Range Property

Returns a Range object that represents the used range on the specified worksheet.

Worksheet.Rows Property

Returns a Range object that represents the used Row on the specified worksheet.

セルの結合

下記はセルを結合するステップとコードになります。

  1. Workbookオブジェクトを作成します。
  2. Workbook.LoadFromFileメソッドでファイルをロードします。
  3. XlsRange.Merge メソッドでセルを結合します。
  4. Workbook.SaveToFileメソッドでファイルを保存します。
```C# using Spire.Xls;

namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");

        workbook.Worksheets[0].Rows[0].Merge();
        workbook.Worksheets[0].Range["F2:G7"].Merge();

        workbook.SaveToFile("Merge.xlsx");
    }
}

}


<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20211020/20211020112927.png" alt="f:id:lendoris:20211020112927p:plain" width="554" height="115" loading="lazy" title="" class="hatena-fotolife" itemprop="image" /></p>
<h1><strong>セル結合解除</strong></h1>
<p>下記はセル結合を解除するステップとコードになります。</p>
<ol>
<li><strong><a href="https://www.e-iceblue.com/apireference/net/Spire.XLS/html/T_Spire_Xls_Workbook.htm">Workbook</a></strong>オブジェクトを作成します。</li>
<li><strong><a href="https://www.e-iceblue.com/apireference/net/Spire.XLS/html/Overload_Spire_Xls_Workbook_LoadFromFile.htm">Workbook.LoadFromFile</a></strong>メソッドでファイルをロードします。</li>
<li><a href="https://www.e-iceblue.com/apireference/net/Spire.XLS/html/M_Spire_Xls_Core_Spreadsheet_XlsRange_UnMerge.htm"><strong>XlsRange.UnMerge</strong> </a>メソッドで結合を解除します。</li>
<li><strong><a href="https://www.e-iceblue.com/apireference/net/Spire.XLS/html/Overload_Spire_Xls_Workbook_SaveToFile.htm">Workbook.SaveToFile</a></strong>メソッドでファイルを保存します。</li>
</ol>
```C#

using Spire.Xls;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Merge.xlsx");

            workbook.Worksheets[0].Rows[0].UnMerge();
            workbook.Worksheets[0].Range["A3:B5"].UnMerge();
           
            workbook.SaveToFile("Unmerge.xlsx");
        }
    }
}

最後に

ここまで読んでくださってありがとうございます!もしSpire.XLS for .NETを利用している時にご不明なところがございましたら、ぜひご連絡ください

 

 

 

 

 

2
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?