マージとアンマージ Cells
Contents
[
Hide
]
Aspose.Cells はこの機能をサポートしており、ワークシート内のセルを結合することもできます。結合されたセルを結合解除または分割することもできます。結合セルのセル参照は、元の選択範囲の左上のセルの参照です。
序章
すべての行または列に常に同じ数のセルが必要なわけではありません。たとえば、複数の列にまたがるセルにタイトルを入れたい場合があります。または、請求書を作成する場合は、合計の列を少なくしたい場合があります。 2 つ以上のセルから 1 つのセルを作成するには、それらをマージします。 Microsoft Excel では、ユーザーがファイルを選択して結合し、スプレッドシートを好きなように構成できます。
セルを結合すると、左上のセルのデータのみが保持されることに注意してください。範囲内の他のセルにデータがある場合、このデータは削除されます。
同様に、書式設定は参照セルに基づいているため、セルを結合すると、範囲内の左上のセルの書式設定が結合されたセルに適用されます。セルが分割されると、新しいセルは元の書式設定を保持します。
ワークシートで Cells をマージする
Cells を Microsoft Excel にマージする
次の手順では、MS Excel を使用してワークシート内のセルを結合する方法について説明します。
- 必要なデータを範囲内の左上端のセルにコピーします。
- 結合するセルを選択します。
- 行または列のセルを結合してセルの内容を中央に配置するには、マージして中央揃えアイコン書式設定ツールバー。
Cells と Aspose.Cells のマージ
Aspose.Cells.Cells クラスには、タスクに役立つメソッドがいくつかあります。たとえば、Merge() メソッドは、指定された範囲内のセルを 1 つのセルに結合します。
次の例は、ワークシートのセル (C6:E7) を結合する方法を示しています。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
アンマージ (分割) マージ済み Cells
Microsoft エクセルを使う
次の手順では、Microsoft Excel を使用して結合セルを分割する方法について説明します。
- 結合セルを選択します。 セルを結合すると、マージして中央揃えで選択されます書式設定ツールバー。
- クリックマージして中央揃え上で書式設定ツールバー。
Aspose.Cells を使用
クラス Aspose.Cells.Cells には、セルを元の状態に分割する UnMerge() という名前のメソッドがあります。このメソッドは、結合されたセル範囲内のセルの参照を使用して、セルの結合を解除します。
次の例は、結合されたセル (C6) を分割する方法を示しています。この例では、前の例で作成したファイルを使用して、結合されたセルを分割します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |