セルの結合と解除
紹介
すべての行や列に常に同じ数のセルを必ずしも欲しいわけではありません。 たとえば、複数の列にまたがるタイトルを置きたい場合があります。 または、請求書を作成する場合、合計に対して少ない列を望むことがあります。 セルを1つに結合してみてください。 Microsoft Excelは、ユーザーがファイルを選択して自分の望むようにスプレッドシートの構造を結合できます。
ワークシート内でセルを結合する
Microsoft Excelでセルを結合する
以下の手順では、MS Excelを使用してワークシート内のセルを結合する方法について説明します。
- 範囲内で左上のセルにデータをコピーします。
- 結合したいセルを選択します。
- 行または列内のセルを結合してセルの内容を中央に配置するには、書式設定ツールバーの結合して中央配置アイコンをクリックします。
Aspose.Cellsでセルの結合
Aspose.Cells.Cellsクラスには、そのようなタスクに便利なメソッドがいくつかあります。 たとえば、メソッドMerge()は指定された範囲内でセルを1つのセルに結合します。
以下の例は、ワークシート内のセル(C6:E7)を結合する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Create a Workbook. | |
Workbook wbk = new Workbook(); | |
// Create a Worksheet and get the first sheet. | |
Worksheet worksheet = wbk.Worksheets[0]; | |
// Create a Cells object ot fetch all the cells. | |
Cells cells = worksheet.Cells; | |
// Merge some Cells (C6:E7) into a single C6 Cell. | |
cells.Merge(5, 2, 2, 3); | |
// Input data into C6 Cell. | |
worksheet.Cells[5, 2].PutValue("This is my value"); | |
// Create a Style object to fetch the Style of C6 Cell. | |
Style style = worksheet.Cells[5, 2].GetStyle(); | |
// Create a Font object | |
Font font = style.Font; | |
// Set the name. | |
font.Name = "Times New Roman"; | |
// Set the font size. | |
font.Size = 18; | |
// Set the font color | |
font.Color = System.Drawing.Color.Blue; | |
// Bold the text | |
font.IsBold = true; | |
// Make it italic | |
font.IsItalic = true; | |
// Set the backgrond color of C6 Cell to Red | |
style.ForegroundColor = System.Drawing.Color.Red; | |
style.Pattern = BackgroundType.Solid; | |
// Apply the Style to C6 Cell. | |
cells[5, 2].SetStyle(style); | |
// Save the Workbook. | |
wbk.Save(dataDir + "mergingcells.out.xls"); |
結合されたセルの結合解除(分割)
Microsoft Excel の使用
以下の手順では、Microsoft Excelを使用して結合されたセルを分割する方法について説明します。
- 結合されたセルを選択します。 セルが結合されている場合、結合して中央配置が書式設定ツールバーで選択されます。
- 書式設定ツールバーで結合して中央配置をクリックします。
Aspose.Cellsの使用
Aspose.Cells.Cellsクラスには、セルの参照を使用して結合されたセルを元の状態に分割するメソッドUnMerge()があります。 このメソッドは、結合されたセルの範囲内のセルの参照を使用してセルを分割します。
以下の例は、結合されたセル(C6)を分割する方法を示しています。 この例では、前の例で作成されたファイルを使用し、結合されたセルを分割しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create a Workbook. | |
// Open the excel file. | |
Workbook wbk = new Aspose.Cells.Workbook(dataDir + "mergingcells.xls"); | |
// Create a Worksheet and get the first sheet. | |
Worksheet worksheet = wbk.Worksheets[0]; | |
// Create a Cells object ot fetch all the cells. | |
Cells cells = worksheet.Cells; | |
// Unmerge the cells. | |
cells.UnMerge(5, 2, 2, 3); | |
// Save the file. | |
wbk.Save(dataDir + "unmergingcells.out.xls"); |