使用openxml合并excel中的相邻单元格

本文关键字:单元格 openxml 合并 excel 使用 | 更新日期: 2023-09-27 18:18:23

我想在excel中使用openxml合并五个相邻的单元格。我试图通过添加一个单元格来修改以下代码片段,但它不起作用。它只对两个细胞正常工作。如果我想合并两个以上的单元格,该怎么办

// Given a document name, a worksheet name, and the names of two adjacent cells, merges the two cells.
    // When two cells are merged, only the content from one cell is preserved:
    // the upper-left cell for left-to-right languages or the upper-right cell for right-to-left languages.
    private static void MergeTwoCells(Worksheet worksheet, string cell1Name, string cell2Name)
    {
        // Open the document for editing.
        // Verify if the specified cells exist, and if they do not exist, create them.
        //CreateSpreadsheetCellIfNotExist(worksheet, cell1Name);
        //CreateSpreadsheetCellIfNotExist(worksheet, cell2Name);
            MergeCells mergeCells;
            if (worksheet.Elements<MergeCells>().Count() > 0)
            {
                mergeCells = worksheet.Elements<MergeCells>().First();
            }
            else
            {
                mergeCells = new MergeCells();
                // Insert a MergeCells object into the specified position.
                if (worksheet.Elements<CustomSheetView>().Count() > 0)
                {
                    worksheet.InsertAfter(mergeCells, worksheet.Elements<CustomSheetView>().First());
                }
                else if (worksheet.Elements<DataConsolidate>().Count() > 0)
                {
                    worksheet.InsertAfter(mergeCells, worksheet.Elements<DataConsolidate>().First());
                }
                else if (worksheet.Elements<SortState>().Count() > 0)
                {
                    worksheet.InsertAfter(mergeCells, worksheet.Elements<SortState>().First());
                }
                else if (worksheet.Elements<AutoFilter>().Count() > 0)
                {
                    worksheet.InsertAfter(mergeCells, worksheet.Elements<AutoFilter>().First());
                }
                else if (worksheet.Elements<Scenarios>().Count() > 0)
                {
                    worksheet.InsertAfter(mergeCells, worksheet.Elements<Scenarios>().First());
                }
                else if (worksheet.Elements<ProtectedRanges>().Count() > 0)
                {
                    worksheet.InsertAfter(mergeCells, worksheet.Elements<ProtectedRanges>().First());
                }
                else if (worksheet.Elements<SheetProtection>().Count() > 0)
                {
                    worksheet.InsertAfter(mergeCells, worksheet.Elements<SheetProtection>().First());
                }
                else if (worksheet.Elements<SheetCalculationProperties>().Count() > 0)
                {
                    worksheet.InsertAfter(mergeCells, worksheet.Elements<SheetCalculationProperties>().First());
                }
                else
                {
                    worksheet.InsertAfter(mergeCells, worksheet.Elements<SheetData>().First());
                }
            }
            // Create the merged cell and append it to the MergeCells collection.
            string s1 = cell1Name + ":" + cell2Name ;
            MergeCell mergeCell = new MergeCell() { Reference = s1 };
            mergeCells.Append(mergeCell);
            worksheet.Save();
    }

使用openxml合并excel中的相邻单元格

您不需要更改代码。如果要合并多个单元格,只需确保cell1Name是要合并的单元格的左上角单元格cell2Name右下角单元格(即A1:B2将合并单元格A1,A2,B1和B2)。

同样,只有左上角单元格中的值将被保留(对于从右到左的文本,则保留右上角)。