Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
5ca96d2
Update index.json
agent-aspose-cells-examples May 18, 2026
2fd42d0
Add example: add-the-library-reference-to-a-net-project-via-nuget-bef…
agent-aspose-cells-examples May 18, 2026
fa01b76
Add example: create-a-console-application-project-in-visual-studio-to…
agent-aspose-cells-examples May 18, 2026
0eb07da
Add example: load-source-xls-workbooks-using-new-workbookfilepath-for…
agent-aspose-cells-examples May 18, 2026
179592f
Add example: load-large-xls-files-with-cellshelpermergefiles-by-provi…
agent-aspose-cells-examples May 18, 2026
3a8abf2
Add example: use-workbookcombine-to-merge-two-or-more-workbooks-when-…
agent-aspose-cells-examples May 18, 2026
44e3a2f
Add example: copy-specific-worksheets-from-source-workbooks-into-the-…
agent-aspose-cells-examples May 18, 2026
e783bea
Add example: preserve-charts-and-images-during-merge-by-employing-def…
agent-aspose-cells-examples May 18, 2026
c4ac610
Add example: ensure-formulas-remain-intact-by-keeping-calculation-mod…
agent-aspose-cells-examples May 18, 2026
7d4146f
Add example: maintain-original-cell-formatting-by-not-altering-style-…
agent-aspose-cells-examples May 18, 2026
2d50dcb
Add example: verify-that-the-merged-workbook-contains-the-expected-nu…
agent-aspose-cells-examples May 18, 2026
0d5ad5d
Add example: check-that-all-charts-from-source-workbooks-appear-corre…
agent-aspose-cells-examples May 18, 2026
16c45b3
Add example: confirm-that-embedded-images-are-retained-in-the-merged-…
agent-aspose-cells-examples May 18, 2026
8593b5f
Add example: save-the-merged-workbook-to-a-specified-output-path-usin…
agent-aspose-cells-examples May 18, 2026
11f3ad2
Add example: export-the-combined-workbook-to-pdf-format-to-verify-vis…
agent-aspose-cells-examples May 18, 2026
00b1d67
Add example: export-each-worksheet-of-the-merged-workbook-to-csv-file…
agent-aspose-cells-examples May 18, 2026
21dfaa6
Add example: generate-an-html-representation-of-the-merged-workbook-t…
agent-aspose-cells-examples May 18, 2026
7899a56
Add example: save-the-merged-workbook-into-a-memory-stream-for-immedi…
agent-aspose-cells-examples May 18, 2026
4593118
Add example: attach-the-merged-workbook-file-to-an-email-using-system…
agent-aspose-cells-examples May 18, 2026
68da929
Add example: encrypt-the-merged-workbook-with-a-password-before-savin…
agent-aspose-cells-examples May 18, 2026
fa68f6e
Add example: set-the-author-property-of-the-merged-workbook-to-the-cu…
agent-aspose-cells-examples May 18, 2026
edadbeb
Add example: assign-a-descriptive-title-property-to-the-merged-workbo…
agent-aspose-cells-examples May 18, 2026
93017fb
Add example: add-a-timestamp-worksheet-indicating-the-exact-date-and-…
agent-aspose-cells-examples May 18, 2026
2ffa204
Add example: insert-a-company-logo-image-on-the-first-worksheet-of-th…
agent-aspose-cells-examples May 18, 2026
f217def
Add example: apply-autofit-to-all-columns-in-the-merged-workbook-to-i…
agent-aspose-cells-examples May 18, 2026
c9a9d19
Add example: apply-autofit-to-all-rows-in-the-merged-workbook-to-ensu…
agent-aspose-cells-examples May 18, 2026
6ecb3ea
Add example: protect-the-merged-workbook-with-a-password-to-restrict-…
agent-aspose-cells-examples May 18, 2026
776aeb0
Add example: set-workbook-calculation-mode-to-automatic-after-merging…
agent-aspose-cells-examples May 18, 2026
025ad09
Add example: recalculate-all-formulas-in-the-merged-workbook-by-invok…
agent-aspose-cells-examples May 18, 2026
c56e9fc
Add example: validate-that-no-ref-errors-exist-in-any-cell-after-merg…
agent-aspose-cells-examples May 18, 2026
6c36175
Add example: log-the-file-size-of-the-merged-workbook-after-saving-to…
agent-aspose-cells-examples May 18, 2026
9f086fc
Update agents.md
agent-aspose-cells-examples May 31, 2026
33aec6b
Resolve merge conflicts with release/26.5.0
agent-aspose-cells-examples May 31, 2026
193328b
Update agents.md
agent-aspose-cells-examples May 31, 2026
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,29 @@
using System;
using Aspose.Cells;

namespace MergeWithTimestampDemo
{
public class Program
{
public static void Main()
{
// Load the first workbook (destination workbook)
Workbook destWorkbook = new Workbook("Source1.xlsx");

// Load the second workbook to be merged
Workbook sourceWorkbook = new Workbook("Source2.xlsx");

// Combine the second workbook into the destination workbook
destWorkbook.Combine(sourceWorkbook);

// Add a new worksheet to hold the merge timestamp
Worksheet timestampSheet = destWorkbook.Worksheets.Add("MergeTimestamp");

// Write the exact date and time of the merge operation into cell A1
timestampSheet.Cells["A1"].PutValue(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

// Save the merged workbook with the timestamp worksheet
destWorkbook.Save("MergedWithTimestamp.xlsx");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.IO;
using Aspose.Cells;

namespace AsposeCellsMergeDemo
{
class Program
{
static void Main(string[] args)
{
// NOTE: Before running this code, add the Aspose.Cells via NuGet:
// Install-Package Aspose.Cells
// This ensures the required library is referenced in the project.

// Prepare sample workbooks to demonstrate merge logic.
string[] filesToMerge = new string[2];
filesToMerge[0] = "File1.xlsx";
filesToMerge[1] = "File2.xlsx";

// Create first workbook and add some data.
Workbook wb1 = new Workbook();
wb1.Worksheets[0].Cells["A1"].PutValue("Content from File 1");
wb1.Save(filesToMerge[0]);

// Create second workbook and add some data.
Workbook wb2 = new Workbook();
wb2.Worksheets[0].Cells["A1"].PutValue("Content from File 2");
wb2.Save(filesToMerge[1]);

// Define a temporary cached file required by CellsHelper.MergeFiles.
string cachedFile = "CacheFile.tmp";

// Define the destination file that will contain the merged result.
string outputFile = "MergedOutput.xlsx";

try
{
// Merge the source files into the destination file.
// This uses the CellsHelper.MergeFiles method as defined in the API.
CellsHelper.MergeFiles(filesToMerge, cachedFile, outputFile);

Console.WriteLine($"Files merged successfully. Output saved to: {outputFile}");

// Verify the merge by loading the resulting workbook.
Workbook mergedWorkbook = new Workbook(outputFile);
Console.WriteLine("Merged workbook content:");
Console.WriteLine($"Sheet1!A1 = {mergedWorkbook.Worksheets[0].Cells["A1"].StringValue}");
if (mergedWorkbook.Worksheets.Count > 1)
{
Console.WriteLine($"Sheet2!A1 = {mergedWorkbook.Worksheets[1].Cells["A1"].StringValue}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error during merge: {ex.Message}");
}
finally
{
// Clean up temporary files used for the demo.
foreach (var file in filesToMerge)
{
if (File.Exists(file))
File.Delete(file);
}

if (File.Exists(cachedFile))
File.Delete(cachedFile);
}
}
}
}
116 changes: 116 additions & 0 deletions workbook-merger/agents.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
---
category: workbook-merger
framework: .NET
parent: ../agents.md
version: v2
---

# Persona

You are a C# developer specializing in workbook merging and workbook consolidation using Aspose.Cells for .NET.

Generate simple, correct, production-quality examples that demonstrate ONE workbook-merging scenario at a time.

---

# Scope

- Standalone .cs examples
- One operation per example
- Fully runnable with dotnet run
- No external dependencies

---

# Required Namespaces

using System;
using Aspose.Cells;

Add additional namespaces only when required.

---

# Key APIs

- Workbook
- Workbook.Combine()
- Worksheet.Copy()
- Worksheets.Add()
- SaveFormat

---

# Common Pattern

1. Create source workbooks
2. Populate sample data
3. Merge or copy worksheets
4. Save merged workbook
5. Print success message

---

# Workbook Merger Rules

- Prefer Workbook.Combine() when merging entire workbooks
- Prefer Worksheet.Copy() when merging selected worksheets
- Generate source workbooks programmatically
- Ensure merged workbook contains visible data
- One example = one merge scenario

---

# Input Strategy

- Do NOT depend on file1.xlsx or file2.xlsx
- Create sample workbooks in code
- Keep examples self-contained

---

# Output Rules

- Always generate output.xlsx
- Ensure merged workbook is saved successfully
- Output files are written to the working directory

---

# Common Tasks

- Merge two workbooks
- Merge multiple worksheets
- Copy worksheet between workbooks
- Consolidate workbook data

---

# Common Mistakes

❌ var workbook = new Workbook();
✅ Workbook workbook = new Workbook();

❌ Workbook workbook = new Workbook("input.xlsx");
✅ Workbook workbook = new Workbook();

❌ Merge empty workbooks
✅ Populate sample data before merging

---

# Code Simplicity

- Keep examples concise
- Avoid unnecessary helper methods
- Prefer clarity over abstraction

---

# General Rules

Refer to the root agents.md for:
- Boundaries
- Testing requirements
- Error-handling guidance
- Build and run instructions
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.IO;
using Aspose.Cells;

namespace AsposeCellsExamples
{
public class AutoFitColumnsAfterCombineDemo
{
public static void Main(string[] args)
{
try
{
string sourcePath = "SourceWorkbook.xlsx";
string secondPath = "SecondWorkbook.xlsx";
string outputPath = "MergedAutoFitWorkbook.xlsx";

// Ensure input files exist
if (!File.Exists(sourcePath))
throw new FileNotFoundException($"Source workbook not found: {sourcePath}");
if (!File.Exists(secondPath))
throw new FileNotFoundException($"Second workbook not found: {secondPath}");

// Load workbooks
Workbook sourceWorkbook = new Workbook(sourcePath);
Workbook secondWorkbook = new Workbook(secondPath);

// Combine the second workbook into the source workbook
sourceWorkbook.Combine(secondWorkbook);

// AutoFit columns in each worksheet
foreach (Worksheet sheet in sourceWorkbook.Worksheets)
{
sheet.AutoFitColumns();
}

// Save the merged workbook
sourceWorkbook.Save(outputPath, SaveFormat.Xlsx);
Console.WriteLine($"Merged workbook saved to {outputPath}");
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error: {ex.Message}");
}
}

// Preserve original Run method for compatibility
public static void Run()
{
Main(null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using Aspose.Cells;

class Program
{
static void Main()
{
// Load the merged workbook (replace with actual path)
Workbook workbook = new Workbook("merged.xlsx");

// Apply AutoFitRows to every worksheet to adjust row heights based on content
foreach (Worksheet sheet in workbook.Worksheets)
{
sheet.AutoFitRows();
}

// Save the workbook after autofitting rows
workbook.Save("merged_autofit.xlsx");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using Aspose.Cells;

class MergeWorkbooksWithTitle
{
static void Main()
{
// Paths of the source workbooks to be merged
string[] sourceFiles = { "Source1.xlsx", "Source2.xlsx" };

// Create an empty destination workbook
Workbook mergedWorkbook = new Workbook();

// Iterate through each source file, load it and combine into the destination workbook
foreach (string filePath in sourceFiles)
{
Workbook source = new Workbook(filePath);
mergedWorkbook.Combine(source);
}

// Assign a descriptive title that reflects the combined source files
mergedWorkbook.BuiltInDocumentProperties.Title = "Combined Workbook: Source1.xlsx + Source2.xlsx";

// Save the merged workbook to disk
mergedWorkbook.Save("MergedWorkbook.xlsx", SaveFormat.Xlsx);
}
}
Loading