合并和取消合并 Cells
Contents
[
Hide
]
Aspose.Cells 支持这个功能,也可以合并工作表中的单元格。您也可以取消合并或拆分合并的单元格。合并单元格的单元格引用是原始选定区域中左上角单元格的引用。
介绍
您并不总是希望每行或每列中的单元格数量相同。例如,您可能希望将一个标题放在一个跨多列的单元格中。或者,如果创建发票,您可能需要更少的总计列。要从两个或多个单元格生成一个单元格,请合并它们。 Microsoft Excel 允许用户选择文件并合并它们以按照他们想要的方式构建电子表格。
请注意,合并单元格时,仅保留左上角单元格中的数据。如果该范围内的其他单元格中有数据,则删除该数据。
同样,格式设置基于引用单元格,因此当您合并单元格时,范围内左上角单元格的格式设置将应用于合并的单元格。拆分单元格时,新单元格会保留其原始格式设置。
在工作表中合并 Cells
在 Microsoft Excel 中合并 Cells
以下步骤描述了如何使用 MS Excel 合并工作表中的单元格。
- 将所需数据复制到范围内最左上角的单元格中。
- 选择要合并的单元格。
- 要合并行或列中的单元格并将单元格内容居中,请单击合并和居中上的图标格式化工具栏。
将 Cells 与 Aspose.Cells 合并
Aspose.Cells.Cells 类有一些对任务有用的方法。例如,方法 Merge() 将单元格合并为指定范围内的单个单元格。
以下示例显示如何合并工作表中的单元格 (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 Excel
以下步骤描述了如何使用 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"); |