在 VSTO 的工作表中合并或取消合并 Cells 和 Aspose.Cells
Contents
[
Hide
]
打开现有的excel文件,合并工作簿中第一个工作表中的一些单元格并保存excel文件。
合并 Cells
VSTO
以下是 VSTO (C#) 和 Aspose.Cells for .NET (C#) 的并行代码片段。
//Instantiate the Application object.
Excel.Application excelApp = Application;
//Specify the template excel file path.
string myPath = "Book1.xls";
//Open the excel file.
excelApp.Workbooks.Open(myPath, Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
//Get the range of cells i.e.., A1:C1.
Excel.Range rng1 = excelApp.get_Range("A1", "C1");
//Merge the cells.
rng1.Merge(Missing.Value);
rng1 = excelApp.get_Range("A1", Missing.Value);
//Save the file.
excelApp.ActiveWorkbook.Save();
//Quit the Application.
excelApp.Quit();
Aspose.Cells
//Instantiate a new Workbook.
Workbook workbook = new Workbook();
//Specify the template excel file path.
string myPath = "Book1.xls";
//Open the excel file.
workbook.Open(myPath);
//Get the range of cells i.e.., A1:C1.
Aspose.Cells.Range rng1 = workbook.Worksheets[0].Cells.CreateRange("A1", "C1");
//Merge the cells.
rng1.Merge();
//Save the file.
workbook.Save("Book1.xls");
取消合并 Cells
要取消合并单元格,请对 VSTO (C#) 和 Aspose.Cells for .NET (C#) 使用以下代码行。
VSTO
//UnMerge the cell.
rng1.UnMerge();
Aspose.Cells
Cells rng = workbook.Worksheets[0].Cells;
//UnMerge the cell.
rng.UnMerge(0, 0, 1, 3);
下载示例代码
- Github
- [Sourceforge](https://sourceforge.net/projects/asposevsto/files/Aspose.Cells%20Vs%20VSTO%20Excel/Merge%20or%20UnMerge%20Cells%20in%20a%20Worksheet%20(Aspose.Cells).zip/下载)
- [比特桶](https://bitbucket.org/asposemarketplace/aspose-for-vsto/downloads/Merge%20or%20UnMerge%20Cells%20in%20a%20Worksheet%20(Aspose.Cells)。压缩)