Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Edit Excel document using C#

The [.NET Excel Library](https://www.syncfusion.com/document-sdk/net-excel-library) enables you to create, read, and edit Excel documents programmatically without Microsoft Excel or interop dependencies.

## Steps to edit an Excel document programmatically

Step 1: Create a new C# Console Application project.

Step 2: Name the project.

Step 3: Install the [Syncfusion.XlsIO.Net.Core](https://www.nuget.org/packages/Syncfusion.XlsIO.Net.Core) NuGet package as reference to your .NET Standard applications from [NuGet.org](https://www.nuget.org).

Step 4: Include the following namespaces in the **Program.cs** file.
```csharp
using System.IO;
using Syncfusion.XlsIO;
```
Step 5: Include the below code snippet in **Program.cs** to edit an Excel document.
```csharp
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;

IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Input.xlsx"));
IWorksheet sheet = workbook.Worksheets[0];

sheet.Range["D1"].Text = "In Stock";
sheet.Range["E1"].Text = "Total Price";
sheet.Range["F1"].Text = "Discount (10%)";
sheet.Range["G1"].Text = "Final Price";

sheet.Range["D2"].Boolean = true;
sheet.Range["D3"].Boolean = true;

sheet.Range["A4"].Text = "Bag";
sheet.Range["B4"].Number = 500;
sheet.Range["C4"].Number = 5;
sheet.Range["D4"].Boolean = false;

sheet.Range["A5"].Text = "Bottle";
sheet.Range["B5"].Number = 100;
sheet.Range["C5"].Number = 10;
sheet.Range["D5"].Boolean = true;

sheet.Range["E2"].Formula = "B2*C2";
sheet.Range["F2"].Formula = "E2*0.1";
sheet.Range["G2"].Formula = "E2-F2";

sheet.Range["E3"].Formula = "B3*C3";
sheet.Range["F3"].Formula = "E3*0.1";
sheet.Range["G3"].Formula = "E3-F3";

sheet.Range["E4"].Formula = "B4*C4";
sheet.Range["F4"].Formula = "E4*0.1";
sheet.Range["G4"].Formula = "E4-F4";

sheet.Range["E5"].Formula = "B5*C5";
sheet.Range["F5"].Formula = "E5*0.1";
sheet.Range["G5"].Formula = "E5-F5";

sheet.Range["A6"].Text = "Totals";
sheet.Range["E6"].Formula = "SUM(E2:E5)";
sheet.Range["F6"].Formula = "SUM(F2:F5)";
sheet.Range["G6"].Formula = "SUM(G2:G5)";

sheet.Range["A1:G1"].CellStyle.Font.Bold = true;
sheet.Range["A1:G6"].CellStyle.HorizontalAlignment = ExcelHAlign.HAlignCenter;

IListObject table = sheet.ListObjects.Create("SalesTable", sheet.Range["A1:G6"]);
table.BuiltInTableStyle = TableBuiltInStyles.TableStyleMedium23;

workbook.SaveAs(Path.GetFullPath(@"Output/Output.xlsx"));
workbook.Close();
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Merge Multiple Excel Files into One Excel File in C#

The [.NET Excel Library](https://www.syncfusion.com/document-sdk/net-excel-library) enables you to create, read, and edit Excel documents programmatically without Microsoft Excel or interop dependencies. Using this library, you can **merge multiple Excel files into one Excel file** using C#.

## Steps to Merge Multiple Excel Files into One Excel File

Step 1: Create a new C# Console Application project.

Step 2: Name the project.

Step 3: Install the [Syncfusion.XlsIO.Net.Core](https://www.nuget.org/packages/Syncfusion.XlsIO.Net.Core) NuGet package as reference to your .NET Standard applications from [NuGet.org](https://www.nuget.org).

Step 4: Include the following namespaces in the **Program.cs** file.
```csharp
using System.IO;
using Syncfusion.XlsIO;
```
Step 5: Include the below code snippet in **Program.cs** to merge multiple Excel files into one Excel file
```csharp
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(0);

//Loop through each Excel document and add the worksheets to the new workbook
foreach (Stream stream in streams)
{
stream.Position = 0;
IWorkbook tempWorkbook = application.Workbooks.Open(stream);
workbook.Worksheets.AddCopy(tempWorkbook.Worksheets);
tempWorkbook.Close();
}

//Save the workbook to a memory stream
MemoryStream memoryStream = new MemoryStream();
workbook.Version = ExcelVersion.Xlsx;
workbook.SaveAs(memoryStream);

return memoryStream;
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34310.174
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MergeExcel", "MergeExcel\MergeExcel.csproj", "{B68720C9-71D9-4706-B1E2-7C207699EF1D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B68720C9-71D9-4706-B1E2-7C207699EF1D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B68720C9-71D9-4706-B1E2-7C207699EF1D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B68720C9-71D9-4706-B1E2-7C207699EF1D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B68720C9-71D9-4706-B1E2-7C207699EF1D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C8553F0E-B638-4BC4-9E5B-2FC2C818FB4A}
EndGlobalSection
EndGlobal
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<Folder Include="Data\" />
<Folder Include="Output\" />
</ItemGroup>

<ItemGroup>
<None Update="Data\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>







Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// See https://aka.ms/new-console-template for more information
using Syncfusion.XlsIO;


namespace MergeExcel
{
class Program
{
static void Main(string[] args)
{
string inputPath = @"../../../Data/";

string outputPath = @"../../../Output/";

FileInfo[] files = new DirectoryInfo(inputPath).GetFiles();

List<Stream> streams = new List<Stream>();

foreach (FileInfo file in files)
{
streams.Add(file.OpenRead());
}

Stream mergedStream = MergeExcelDocuments(streams);

FileStream fileStream = new FileStream(outputPath + "MergedExcel.xlsx", FileMode.Create, FileAccess.Write);
mergedStream.Position = 0;
mergedStream.CopyTo(fileStream);
fileStream.Close();
}

/// <summary>
/// Merge Excel documents from the list of Excel streams
/// </summary>
/// <param name="streams">List of Excel document stream to be merged</param>
/// <returns></returns>
public static Stream MergeExcelDocuments(List<Stream> streams)
{
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(0);

//Loop through each Excel document and add the worksheets to the new workbook
foreach (Stream stream in streams)
{
stream.Position = 0;
IWorkbook tempWorkbook = application.Workbooks.Open(stream);
workbook.Worksheets.AddCopy(tempWorkbook.Worksheets);
tempWorkbook.Close();
}

//Save the workbook to a memory stream
MemoryStream memoryStream = new MemoryStream();
workbook.Version = ExcelVersion.Xlsx;
workbook.SaveAs(memoryStream);

return memoryStream;
}
}
}
}







Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Merge Multiple Excel Files into One Excel File in C#

The [.NET Excel Library](https://www.syncfusion.com/document-sdk/net-excel-library) enables you to create, read, and edit Excel documents programmatically without Microsoft Excel or interop dependencies. Using this library, you can **merge multiple Excel files into one Excel file** using C#.

## Steps to Merge Multiple Excel Files into One Excel File

Step 1: Create a new C# Console Application project.

Step 2: Name the project.

Step 3: Install the [Syncfusion.XlsIO.Net.Core](https://www.nuget.org/packages/Syncfusion.XlsIO.Net.Core) NuGet package as reference to your .NET Standard applications from [NuGet.org](https://www.nuget.org).

Step 4: Include the following namespaces in the **Program.cs** file.
```csharp
using System.IO;
using Syncfusion.XlsIO;
```
Step 5: Include the below code snippet in **Program.cs** to merge multiple Excel files into one Excel file
```csharp
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(0);

//Loop through each Excel document and add the worksheets to the new workbook
foreach (Stream stream in streams)
{
stream.Position = 0;
IWorkbook tempWorkbook = application.Workbooks.Open(stream);
workbook.Worksheets.AddCopy(tempWorkbook.Worksheets);
tempWorkbook.Close();
}

//Save the workbook to a memory stream
MemoryStream memoryStream = new MemoryStream();
workbook.Version = ExcelVersion.Xlsx;
workbook.SaveAs(memoryStream);

return memoryStream;
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34310.174
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SplitExcelDocument", "SplitExcelDocument\SplitExcelDocument.csproj", "{F84710FF-A205-4540-8A1F-8C05813225DD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F84710FF-A205-4540-8A1F-8C05813225DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F84710FF-A205-4540-8A1F-8C05813225DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F84710FF-A205-4540-8A1F-8C05813225DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F84710FF-A205-4540-8A1F-8C05813225DD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6FCE571C-9967-4868-B912-EC01287A7610}
EndGlobalSection
EndGlobal
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Syncfusion.XlsIO;


namespace SplitExcel
{
class Program
{
private static string inputPath = @"../../../Data/";

private static string outputPath = @"../../../Output/";
static void Main(string[] args)
{
string fileName = "Report.xlsx";

//Split the Excel document
SplitExcelDocument(inputPath + fileName);
}
/// <summary>
/// Split the Excel document from the given path
/// </summary>
/// <param name="filePath">Excel file path</param>
private static void SplitExcelDocument(string filePath)
{
FileStream inputData = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);

using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(inputData);
IWorksheets worksheets = workbook.Worksheets;

workbook.Version = ExcelVersion.Xlsx;

//Loop through each Excel worksheet and save it as a new workbook
foreach (IWorksheet worksheet in worksheets)
{
IWorkbook newBook = application.Workbooks.Create(0);
newBook.Worksheets.AddCopy(worksheet);

FileStream outputData = new FileStream(outputPath + worksheet.Name + ".xlsx", FileMode.Create, FileAccess.ReadWrite);
newBook.SaveAs(outputData);
outputData.Close();
}
}
}
}
}






Loading
Loading