Przejdź do treści stopki
KORZYSTANIE Z IRONXL

Jak scalic komórki w Excelu bez utraty danych

This article demonstrates how to merge multiple cells into one using Microsoft Excel and IronXL programmatically.

Merge Multiple Cells into a Single Cell Using Microsoft Excel

The built-in Merge and Center option in Excel is the quickest and simplest approach to integrate two or more cells. There are only two fast steps in the entire process:

  • Choose the adjacent Excel cells that you want to combine into one cell.
  • Click the Merge & Center button in the Home tab's Alignment group to merge two cells or multiple columns in Excel.

In this example, the upper left cell A1 contains a list of fruits, and data in two adjacent empty cells (B1 and C1) is combined to create a single, sizable cell that can hold the complete list.

How to Merge Cells in Excel Without Losing Data, Figure 1: Merging Cells Merging Cells

The text is centered, and the selected cells are combined into one large cell once you click Merge & Center, as shown in the above screenshot.

Excel's Options to Combine Multiple Cells

Select the choice you want from the drop-down menu by clicking the tiny drop-down arrow next to the Merge & Center button, which will give you access to a few more merging options supplied by Excel, such as Merge Across, Unmerge Cells.

How to Merge Cells in Excel Without Losing Data, Figure 2: Combine Cells Combine Cells

Using the Merge Across command, each row's individually chosen cells are combined.

Using the Merge Cells command, you can combine the selected cells into one without centering the text or losing data.

Simply pick the merged cell and click the desired alignment in the Alignment group on the Home page to change the text alignment of only the data after merging all the cells.

There are a few things to remember when using Excel's built-in functions to combine adjacent cells:

  • Only the content of the upper-left cell will remain after merging, as all data in the other cells will be removed. Therefore, make sure that all the data you want to include in a merged cell is entered in the leftmost cell of the chosen range.
  • The selected cells are likely in Edit mode if the Merge and Center button is grayed out. Try merging cells after exiting Edit mode by pressing the Enter key.

IronXL Library Features

You can quickly read and change Microsoft Excel documents with the IronXL library in C#. Without installing Microsoft Excel or relying on Microsoft Office Interop Excel, IronXL is a standalone .NET software library that can read other spreadsheet formats.

One of the best Excel spreadsheet libraries for C# is IronXL, which works with both .NET Framework and .NET Core. It supports many versions of .NET Frameworks, including Console Applications, Windows Forms, and Web Applications. IronXL makes it simple and quick to read Excel files with or without merged cells. Numerous Excel file types are supported, including XLSX, XLS, CSV, TSV, XLST, XLSM, and others. Numerous procedures, including import, edit, export of data tables, export of datasets, and others, are available. With IronXL, you can export and save files in a variety of extensions, including XLS, CSV, TSV, JSON, and more.

You can easily read, alter, and create Excel spreadsheet files in the .NET environment with IronXL's user-friendly C# API. It offers full support for Azure, .NET Core, .NET Framework, Xamarin, Mobile, Linux, and macOS.

IronXL supports various Excel column data formats, including text, integers, formulas, dates, currencies, and percentages, and it is capable of carrying out calculations like Excel.

Tworzenie nowego projektu w Visual Studio

Open Visual Studio, select "New project" and choose "Console App" from the File menu. A C# Console Application is used for simplicity.

How to Merge Cells in Excel Without Losing Data, Figure 3: New Project Nowy projekt

In the relevant text box, type the project name and file path. Next, select the necessary .NET Framework by clicking the Create button. The project will now create the program.cs file's structure and open, allowing you to enter the program's code and build or run it.

How to Merge Cells in Excel Without Losing Data, Figure 4: Project Configuration Konfiguracja projektu

The solution's necessary IronXL library must then be downloaded. By entering the following command in the package manager, you can download the package:

Install-Package IronXl.Excel

How to Merge Cells in Excel Without Losing Data, Figure 5: IronXL IronXL

The "IronXL" package can also be found and downloaded using the NuGet Package Manager. Dependency management in your project is made simple with the NuGet Package Manager.

How to Merge Cells in Excel Without Losing Data, Figure 6: NuGet Package Manager Menedżer pakietów NuGet

Merge Cells Using IronXL

IronXL can merge multiple columns/cells in existing Excel sheets. Below is the sample code to merge multiple cells.

using IronXL;

class Program
{
    static void Main()
    {
        // Load the existing Excel workbook
        var excelDoc = WorkBook.Load("demo.xlsx");

        // Get the default worksheet
        WorkSheet workSheet = excelDoc.DefaultWorkSheet;

        // Define the range of cells to merge
        var range = workSheet["A1:C1"];

        // Merge the specified range of cells
        workSheet.Merge(range.RangeAddressAsString);

        // Save the changes made to the workbook
        excelDoc.Save();
    }
}
using IronXL;

class Program
{
    static void Main()
    {
        // Load the existing Excel workbook
        var excelDoc = WorkBook.Load("demo.xlsx");

        // Get the default worksheet
        WorkSheet workSheet = excelDoc.DefaultWorkSheet;

        // Define the range of cells to merge
        var range = workSheet["A1:C1"];

        // Merge the specified range of cells
        workSheet.Merge(range.RangeAddressAsString);

        // Save the changes made to the workbook
        excelDoc.Save();
    }
}
Imports IronXL

Friend Class Program
	Shared Sub Main()
		' Load the existing Excel workbook
		Dim excelDoc = WorkBook.Load("demo.xlsx")

		' Get the default worksheet
		Dim workSheet As WorkSheet = excelDoc.DefaultWorkSheet

		' Define the range of cells to merge
		Dim range = workSheet("A1:C1")

		' Merge the specified range of cells
		workSheet.Merge(range.RangeAddressAsString)

		' Save the changes made to the workbook
		excelDoc.Save()
	End Sub
End Class
$vbLabelText   $csharpLabel

In the example code above:

  1. The existing Excel workbook is loaded into the IronXL workbook object.
  2. The default worksheet is selected.
  3. The range of cells (A1:C1) that you want to merge is specified.
  4. The Merge method is then called to combine the specified range of cells.
  5. Finally, the Save method is called to save the changes to the workbook.

This Merge function merges a group of cells, retaining only the value of the first cell in the range without removing the values from other cells. However, the values from those merged cells are still accessible in IronXL.

Wnioski

A highly popular Excel add-in, IronXL, doesn't rely on external libraries. It is a standalone solution that doesn't require Microsoft Excel to be installed. It works with a variety of platforms.

With IronXL, you may programmatically perform a vast array of functions on Microsoft Excel documents. You can sort strings or numbers, trim and add data in empty cells, look up and replace values in blank cells, merge and unmerge cells, save files, concatenate functions, and perform other operations. You can specify cell data types and evaluate spreadsheet data with it as well. IronXL also has CSV file reading and writing capabilities.

When IronXL is released, it will cost $799 to acquire. Additionally, customers have the option to pay an annual membership fee for product upgrades and support. IronXL offers unrestricted redistribution rights for an extra fee. You can click on the licensing page here to go to the appropriate source and get more specific pricing details.

Często Zadawane Pytania

Jak moge scalic komorki w Excelu bez utraty danych?

Mozesz uzyc IronXL do scalenia komorek w Excelu bez utraty danych. IronXL zapewnia rozwiazanie programistyczne, ktore zachowuje dane ze wszystkich scalentgy komorek, w przeciwienstwie do wbudowanej funkcji Excela, ktora zachowuje tylko zawartosc komorki w gornym-lewym rogu.

Jaka jest metoda programistyczna do scalenia komorek w Excelu za pomoca C#?

Za pomoca IronXL mozesz scalic komorki w Excelu za pomoca C#, ladujac skoroszyt, wybierajac arkusz i zakres, stosujac metode Scal, a nastepnie zapisujac skoroszyt. Ten proces zapewnia zachowanie wszystkich danych.

Jak IronXL obsluguje dane przy scalaniu komorek w porownaniu do wbudowanej funkcji Excela?

IronXL zachowuje dane wszystkich scalentgy komorek, zapewniajac, ze zadne informacje nie zostana utracone. W przeciwienstwie do tego, wbudowana funkcja 'Scal i wyrownaj do srodka' zachowuje jedynie dane z komorki w gornym-lewym rogu.

Czy moge scalic komorki w Excelu na roznych platformach za pomoca IronXL?

Tak, IronXL obsluguje wiele platform, w tym .NET Framework i .NET Core, pozwalajac na scalanie komorek w plikach Excel na roznych srodowiskach, takich jak aplikacje konsolowe, Windows Forms i aplikacje webowe.

Czy konieczne jest zainstalowanie Microsoft Excel, aby uzyc IronXL do operacji w Excelu?

Nie, IronXL jest samodzielna biblioteka .NET, ktora nie wymaga zainstalowanego Microsoft Excel na twoim systemie. Pozwala na wykonywanie operacji w Excelu niezaleznie.

Jakie sa kroki do scalenia komorek w Excelu za pomoca IronXL?

Aby scalic komorki za pomoca IronXL, najpierw zaladuj plik Excel do swojej aplikacji. Wybierz arkusz i zakres komorek do scalenia. Uzyj metody Scal IronXL na wybranym zakresie, a nastepnie zapisz swoje zmiany do pliku.

Jak zainstalowac IronXL w projekcie Visual Studio do manipulacji Excel?

Mozesz zainstalowac IronXL w projekcie Visual Studio przez Menedżer pakietow NuGet. Wyszukaj 'IronXL' i zainstaluj go, lub uzyj polecenia Install-Package IronXL w konsoli Menedżera pakietow.

Czy IronXL mozna uzyc do innych operacji w Excelu oprocz scalania komorek?

Tak, IronXL obsluguje wiele operacji w Excelu poza scalaniem komorek, w tym czytanie, edytowanie i tworzenie plikow Excel, import/eksport danych oraz wykonywanie obliczen podobnych do Excela.

Jakie sa opcje licencjonowania dla uzywania IronXL?

IronXL oferuje opcje licencjonowania, ktore obejmuja roczne czlonkostwo z aktualizacjami produktow i wsparciem. Szczegolowe informacje o cenach sa dostepne na ich stronie licencyjnej.

Jakie formaty Excel obsluguje IronXL?

IronXL obsluguje wiele formatow Excel, takich jak XLSX, XLS i CSV, umozliwiajac szeroka manipulacje dokumentami i zgodnosc z roznych typami plikow Excel.

Jordi Bardia
Inżynier oprogramowania
Jordi jest najbardziej biegły w Pythonie, C# i C++. Kiedy nie wykorzystuje swoich umiejętności w Iron Software, programuje gry. Dzieląc odpowiedzialność za testowanie produktów, rozwój produktów i badania, Jordi wnosi ogromną wartość do ciągłej poprawy produktów. Różnorodne doświadczenia ...
Czytaj więcej

Zespol wsparcia Iron

Jestesmy online 24 godziny, 5 dni w tygodniu.
Czat
Email
Zadzwon do mnie