Skip to content

Commit a057a21

Browse files
authored
Merge pull request #101 from aspose-cells/v3/open-workbook
[AUTO] Open Workbook Examples Update
2 parents 185b80a + f8c2456 commit a057a21

123 files changed

Lines changed: 6850 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

index.json

Lines changed: 455 additions & 0 deletions
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Cells;
4+
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
const string inputPath = "input.xlsx";
10+
11+
// Ensure the input file exists before attempting to load it
12+
if (!File.Exists(inputPath))
13+
{
14+
Console.WriteLine($"Error: File \"{inputPath}\" not found.");
15+
return;
16+
}
17+
18+
try
19+
{
20+
// Load the workbook from the specified file
21+
Workbook workbook = new Workbook(inputPath);
22+
23+
// NOTE: In some Aspose.Cells versions the LoadWarnings collection is not available.
24+
// If needed, warnings can be retrieved via other mechanisms provided by the library.
25+
26+
Console.WriteLine("Workbook loaded successfully.");
27+
28+
// (Optional) Save the workbook if further processing is needed
29+
// workbook.Save("output.xlsx");
30+
}
31+
catch (Exception ex)
32+
{
33+
// Catch any runtime exceptions and display a friendly message
34+
Console.WriteLine($"An error occurred: {ex.Message}");
35+
}
36+
}
37+
}

open-workbook/agents.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
category: open-workbook
3+
framework: .NET
4+
parent: ../agents.md
5+
version: v2
6+
---
7+
8+
# Persona
9+
10+
You are a C# developer specializing in workbook loading and opening operations using Aspose.Cells for .NET.
11+
12+
Generate simple, correct, production-quality examples that demonstrate ONE workbook-opening scenario at a time.
13+
14+
---
15+
16+
# Scope
17+
18+
- Standalone .cs examples
19+
- One operation per example
20+
- Fully runnable with dotnet run
21+
- No external dependencies
22+
23+
---
24+
25+
# Required Namespaces
26+
27+
using System;
28+
using Aspose.Cells;
29+
30+
---
31+
32+
# Key APIs
33+
34+
- Workbook
35+
- LoadOptions
36+
- TxtLoadOptions
37+
- HtmlLoadOptions
38+
- PdfSaveOptions
39+
40+
---
41+
42+
# Common Pattern
43+
44+
1. Create or prepare workbook source
45+
2. Configure load options if needed
46+
3. Open workbook
47+
4. Access workbook content
48+
5. Save workbook or verify results
49+
6. Print success message
50+
51+
---
52+
53+
# Open Workbook Rules
54+
55+
- Demonstrate one loading feature per example
56+
- Use LoadOptions only when relevant
57+
- Show how workbook content is accessed after loading
58+
- Keep examples focused and easy to understand
59+
60+
---
61+
62+
# Input Strategy
63+
64+
- Avoid dependency on unknown external files
65+
- Create source content programmatically when possible
66+
- Keep examples self-contained
67+
68+
---
69+
70+
# Output Rules
71+
72+
- Always generate output.xlsx when appropriate
73+
- Ensure workbook is processed successfully
74+
- Output files are written to the working directory
75+
76+
---
77+
78+
# Common Tasks
79+
80+
- Open workbook
81+
- Open CSV file
82+
- Open HTML file
83+
- Configure LoadOptions
84+
- Detect file format
85+
- Access workbook metadata after loading
86+
87+
---
88+
89+
# Common Mistakes
90+
91+
❌ var workbook = new Workbook();
92+
✅ Workbook workbook = new Workbook();
93+
94+
❌ Use incorrect LoadOptions type
95+
✅ Match LoadOptions to source format
96+
97+
❌ Assume workbook contains worksheets without validation
98+
✅ Verify workbook content before processing
99+
100+
---
101+
102+
# Code Simplicity
103+
104+
- Keep examples concise
105+
- Avoid unnecessary abstractions
106+
- Focus on one workbook-loading capability per example
107+
108+
---
109+
110+
# General Rules
111+
112+
Refer to the root agents.md for:
113+
- Boundaries
114+
- Testing requirements
115+
- Build and run instructions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using Aspose.Cells;
3+
4+
namespace AsposeCellsLoadFilterExample
5+
{
6+
// Custom LoadFilter that returns a specific sheet loading order
7+
public class CustomLoadFilter : LoadFilter
8+
{
9+
private readonly int[] _sheetsOrder;
10+
11+
// Accept the desired sheet indices via constructor
12+
public CustomLoadFilter(int[] sheetsOrder) : base(LoadDataFilterOptions.All)
13+
{
14+
_sheetsOrder = sheetsOrder;
15+
}
16+
17+
// Override the read‑only property to supply the indices
18+
public override int[] SheetsInLoadingOrder => _sheetsOrder;
19+
}
20+
21+
class Program
22+
{
23+
static void Main()
24+
{
25+
// User‑provided list of sheet indexes to load (e.g., load sheet 0 and sheet 2)
26+
int[] selectedSheetIndexes = new int[] { 0, 2 };
27+
28+
// Create the custom filter with the desired order
29+
LoadFilter filter = new CustomLoadFilter(selectedSheetIndexes);
30+
31+
// Configure load options to use the filter
32+
LoadOptions loadOptions = new LoadOptions();
33+
loadOptions.LoadFilter = filter;
34+
35+
// Load the workbook using the filter – only the specified sheets will be loaded
36+
Workbook workbook = new Workbook("Template.xlsx", loadOptions);
37+
38+
// Demonstrate which sheets were loaded
39+
Console.WriteLine("Loaded worksheets count: " + workbook.Worksheets.Count);
40+
foreach (Worksheet ws in workbook.Worksheets)
41+
{
42+
Console.WriteLine($"- Index: {ws.Index}, Name: {ws.Name}");
43+
}
44+
45+
// Save the workbook (optional, demonstrates that saving works with filtered sheets)
46+
workbook.Save("FilteredOutput.xlsx");
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using Aspose.Cells;
3+
4+
namespace AsposeCellsFilterExample
5+
{
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
// Path to the template workbook
11+
string templatePath = "TemplateWorkbook.xlsx";
12+
13+
// Create LoadOptions and set a LoadFilter that loads only numeric (including date/time) cells
14+
LoadOptions loadOptions = new LoadOptions();
15+
loadOptions.LoadFilter = new LoadFilter(LoadDataFilterOptions.CellNumeric);
16+
17+
// Load the workbook using the specified load options
18+
Workbook workbook = new Workbook(templatePath, loadOptions);
19+
20+
// At this point, only cells with numeric or date values are loaded.
21+
// Perform any statistical analysis here, e.g., iterate through worksheets and cells.
22+
23+
// Example: Count numeric cells in the first worksheet
24+
Worksheet sheet = workbook.Worksheets[0];
25+
int numericCellCount = 0;
26+
foreach (Cell cell in sheet.Cells)
27+
{
28+
if (cell.Type == CellValueType.IsNumeric) // includes dates
29+
{
30+
numericCellCount++;
31+
}
32+
}
33+
Console.WriteLine($"Numeric/Date cells loaded: {numericCellCount}");
34+
35+
// Save the filtered workbook (optional, for verification)
36+
workbook.Save("FilteredWorkbook.xlsx");
37+
}
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Text;
3+
using Aspose.Cells;
4+
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
// Paths to the template and the output summary workbook
10+
string templatePath = "template.xlsx";
11+
string outputPath = "summary.xlsx";
12+
13+
// Configure load options to load only cells that contain string values
14+
LoadOptions loadOptions = new LoadOptions();
15+
loadOptions.LoadFilter = new LoadFilter(LoadDataFilterOptions.CellString);
16+
17+
// Load the workbook from the template using the specified filter
18+
Workbook sourceWorkbook = new Workbook(templatePath, loadOptions);
19+
20+
// StringBuilder to accumulate all string values from the loaded workbook
21+
StringBuilder concatenatedStrings = new StringBuilder();
22+
23+
// Iterate through each worksheet and its used cells
24+
foreach (Worksheet sheet in sourceWorkbook.Worksheets)
25+
{
26+
Cells cells = sheet.Cells;
27+
int maxRow = cells.MaxDataRow;
28+
int maxCol = cells.MaxDataColumn;
29+
30+
for (int row = 0; row <= maxRow; row++)
31+
{
32+
for (int col = 0; col <= maxCol; col++)
33+
{
34+
Cell cell = cells[row, col];
35+
// After applying the filter, only string cells are loaded,
36+
// but we still verify the cell type for safety.
37+
if (cell != null && cell.Type == CellValueType.IsString)
38+
{
39+
concatenatedStrings.Append(cell.StringValue);
40+
concatenatedStrings.Append(' '); // separator between values
41+
}
42+
}
43+
}
44+
}
45+
46+
// Create a new workbook that will hold the summary report
47+
Workbook summaryWorkbook = new Workbook();
48+
Worksheet summarySheet = summaryWorkbook.Worksheets[0];
49+
summarySheet.Name = "Summary";
50+
51+
// Write the concatenated string into cell A1 of the summary sheet
52+
summarySheet.Cells["A1"].PutValue(concatenatedStrings.ToString().Trim());
53+
54+
// Save the summary workbook to the specified output path
55+
summaryWorkbook.Save(outputPath);
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using Aspose.Cells;
3+
4+
class FilterDefinedNamesDemo
5+
{
6+
static void Main()
7+
{
8+
// Path to the source workbook (replace with your actual file)
9+
string sourcePath = "Source.xlsx";
10+
11+
// Path where the processed workbook will be saved
12+
string outputPath = "FilteredNames.xlsx";
13+
14+
// Create LoadOptions and assign a custom LoadFilter that loads only defined names
15+
LoadOptions loadOptions = new LoadOptions();
16+
loadOptions.LoadFilter = new DefinedNamesOnlyLoadFilter();
17+
18+
// Load the workbook using the specified LoadOptions
19+
Workbook workbook = new Workbook(sourcePath, loadOptions);
20+
21+
// Retrieve all defined names (both workbook‑scoped and worksheet‑scoped)
22+
Name[] allNames = workbook.Worksheets.Names.Filter(NameScopeType.All, -1);
23+
24+
// Output information about the loaded names
25+
Console.WriteLine($"Loaded {allNames.Length} defined name(s):");
26+
foreach (Name name in allNames)
27+
{
28+
Console.WriteLine($"Name: {name.Text}, RefersTo: {name.RefersTo}");
29+
}
30+
31+
// Save the workbook (structure and names are preserved)
32+
workbook.Save(outputPath);
33+
}
34+
35+
// Custom LoadFilter that restricts loading to defined name objects only
36+
private class DefinedNamesOnlyLoadFilter : LoadFilter
37+
{
38+
public override void StartSheet(Worksheet sheet)
39+
{
40+
// Load only the defined names; other data (cells, formats, etc.) is ignored
41+
LoadDataFilterOptions = LoadDataFilterOptions.DefinedNames;
42+
}
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using Aspose.Cells;
3+
4+
class Program
5+
{
6+
static void Main()
7+
{
8+
// Path to the source workbook
9+
string sourcePath = "input.xlsx";
10+
11+
// Create a LoadFilter that loads everything except data validations
12+
// LoadDataFilterOptions.All includes all data; we remove DataValidation using bitwise NOT
13+
LoadDataFilterOptions filterOptions = LoadDataFilterOptions.All & ~LoadDataFilterOptions.DataValidation;
14+
LoadFilter loadFilter = new LoadFilter(filterOptions);
15+
16+
// Assign the filter to LoadOptions
17+
LoadOptions loadOptions = new LoadOptions();
18+
loadOptions.LoadFilter = loadFilter;
19+
20+
// Load the workbook with the specified filter
21+
Workbook workbook = new Workbook(sourcePath, loadOptions);
22+
23+
// Demonstrate that worksheets are loaded and data validations are excluded
24+
Console.WriteLine("Number of worksheets loaded: " + workbook.Worksheets.Count);
25+
foreach (Worksheet sheet in workbook.Worksheets)
26+
{
27+
Console.WriteLine($"Worksheet '{sheet.Name}' - Cells: {sheet.Cells.Count}, Validations: {sheet.Validations.Count}");
28+
}
29+
30+
// Save the workbook (optional, shows lifecycle usage)
31+
workbook.Save("output.xlsx");
32+
}
33+
}

0 commit comments

Comments
 (0)