Skip to content

Commit 4cb0b13

Browse files
authored
Merge pull request #98 from aspose-cells/v3/globalization-and-localization
[AUTO] Globalization and Localization Examples Update
2 parents ec61f87 + 41cffab commit 4cb0b13

65 files changed

Lines changed: 4827 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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using Aspose.Cells;
3+
using Aspose.Cells.Pivot;
4+
5+
namespace AsposeCellsSubtotalDemo
6+
{
7+
// Custom globalization settings that overrides the total name for the SUM function
8+
public class CustomGlobalizationSettings : SettableGlobalizationSettings
9+
{
10+
public override string GetTotalName(ConsolidationFunction functionType)
11+
{
12+
// Return a custom label for SUM totals; other functions use the base implementation
13+
if (functionType == ConsolidationFunction.Sum)
14+
return "Custom Sum Total";
15+
return base.GetTotalName(functionType);
16+
}
17+
}
18+
19+
public class Program
20+
{
21+
public static void Main()
22+
{
23+
// Create a new workbook and get the first worksheet
24+
Workbook workbook = new Workbook();
25+
Worksheet worksheet = workbook.Worksheets[0];
26+
Cells cells = worksheet.Cells;
27+
28+
// Populate sample data (Category | Value)
29+
cells["A1"].PutValue("Category");
30+
cells["B1"].PutValue("Value");
31+
cells["A2"].PutValue("A");
32+
cells["B2"].PutValue(10);
33+
cells["A3"].PutValue("A");
34+
cells["B3"].PutValue(20);
35+
cells["A4"].PutValue("B");
36+
cells["B4"].PutValue(30);
37+
cells["A5"].PutValue("B");
38+
cells["B5"].PutValue(40);
39+
cells["A6"].PutValue("C");
40+
cells["B6"].PutValue(50);
41+
42+
// Assign the custom globalization settings to the workbook
43+
workbook.Settings.GlobalizationSettings = new CustomGlobalizationSettings();
44+
45+
// Define the range that contains the data (including header)
46+
CellArea dataArea = CellArea.CreateCellArea(0, 0, 5, 1); // rows 0-5, columns 0-1
47+
48+
// Add subtotal rows:
49+
// - Group by column 0 (Category)
50+
// - Use SUM function
51+
// - Apply subtotal to column 1 (Value)
52+
// - Replace existing subtotals, no page breaks, place summary below data
53+
cells.Subtotal(
54+
dataArea,
55+
groupBy: 0,
56+
function: ConsolidationFunction.Sum,
57+
totalList: new int[] { 1 },
58+
replace: true,
59+
pageBreaks: false,
60+
summaryBelowData: true);
61+
62+
// Verify that the subtotal label uses the custom total name
63+
// Search for the custom label in the worksheet
64+
string customLabel = "Custom Sum Total";
65+
Cell foundCell = cells.Find(customLabel, null, new FindOptions() { LookInType = LookInType.Values });
66+
67+
if (foundCell != null)
68+
{
69+
Console.WriteLine($"Verified custom total label found at {foundCell.Name}: \"{foundCell.StringValue}\"");
70+
}
71+
else
72+
{
73+
Console.WriteLine("Custom total label not found. Verification failed.");
74+
}
75+
76+
// Save the workbook
77+
workbook.Save("SubtotalWithCustomGlobalization.xlsx");
78+
}
79+
}
80+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
category: globalization-and-localization
3+
framework: .NET
4+
parent: ../agents.md
5+
version: v2
6+
---
7+
8+
# Persona
9+
10+
You are a C# developer specializing in globalization, localization, regional settings, and culture-specific workbook processing using Aspose.Cells for .NET.
11+
12+
Generate simple, correct, production-quality examples that demonstrate ONE globalization or localization 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 System.Globalization;
29+
using Aspose.Cells;
30+
31+
---
32+
33+
# Key APIs
34+
35+
- GlobalizationSettings
36+
- Workbook.Settings
37+
- CultureInfo
38+
- Style.Custom
39+
- Cell.PutValue()
40+
41+
---
42+
43+
# Common Pattern
44+
45+
1. Create workbook
46+
2. Configure culture or globalization settings
47+
3. Add localized data
48+
4. Format values using regional settings
49+
5. Save workbook
50+
6. Print success message
51+
52+
---
53+
54+
# Globalization and Localization Rules
55+
56+
- Demonstrate one localization feature per example
57+
- Use CultureInfo when culture-specific behavior is required
58+
- Use deterministic sample data
59+
- Clearly show the effect of regional settings
60+
61+
---
62+
63+
# Input Strategy
64+
65+
- Do NOT rely on external XLSX files
66+
- Generate workbook content programmatically
67+
- Keep examples self-contained
68+
69+
---
70+
71+
# Output Rules
72+
73+
- Always generate output.xlsx
74+
- Ensure workbook is saved successfully
75+
- Output files are written to the working directory
76+
77+
---
78+
79+
# Common Tasks
80+
81+
- Apply culture-specific formatting
82+
- Localize dates and numbers
83+
- Configure globalization settings
84+
- Work with regional formats
85+
- Customize language-specific workbook behavior
86+
87+
---
88+
89+
# Common Mistakes
90+
91+
❌ var workbook = new Workbook();
92+
✅ Workbook workbook = new Workbook();
93+
94+
❌ Assume formatting behaves the same across cultures
95+
✅ Explicitly configure culture settings
96+
97+
❌ Workbook workbook = new Workbook("input.xlsx");
98+
✅ Workbook workbook = new Workbook();
99+
100+
---
101+
102+
# Code Simplicity
103+
104+
- Keep examples concise
105+
- Avoid unnecessary abstractions
106+
- Focus on one localization 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,41 @@
1+
using System;
2+
using System.Globalization;
3+
using Aspose.Cells;
4+
5+
namespace AsposeCellsPercentageFormatting
6+
{
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
// Load the workbook with Brazilian Portuguese culture (pt-BR)
12+
LoadOptions loadOptions = new LoadOptions(LoadFormat.Xlsx);
13+
loadOptions.CultureInfo = new CultureInfo("pt-BR");
14+
Workbook workbook = new Workbook("input.xlsx", loadOptions);
15+
16+
// Iterate through all cells in the first worksheet
17+
Worksheet sheet = workbook.Worksheets[0];
18+
Cells cells = sheet.Cells;
19+
20+
foreach (Cell cell in cells)
21+
{
22+
// Retrieve the cell's style
23+
Style style = cell.GetStyle();
24+
25+
// Check if the current style is a percentage format
26+
if (style.IsPercent)
27+
{
28+
// Apply a custom percentage format that respects the culture
29+
// "#,##0.00%" will use the culture's decimal and group separators
30+
style.SetCustom("#,##0.00%", true);
31+
32+
// Re-apply the modified style to the cell
33+
cell.SetStyle(style);
34+
}
35+
}
36+
37+
// Save the modified workbook
38+
workbook.Save("output.xlsx");
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using Aspose.Cells;
3+
4+
// Custom globalization settings derived from GlobalizationSettings
5+
class CustomGlobalizationSettings : GlobalizationSettings
6+
{
7+
// Override boolean display strings
8+
public override string GetBooleanValueString(bool value)
9+
{
10+
return value ? "ИСТИНА" : "ЛОЖЬ";
11+
}
12+
13+
// Override error value strings
14+
public override string GetErrorValueString(string error)
15+
{
16+
return error switch
17+
{
18+
"#NAME?" => "#ИМЯ?",
19+
"#DIV/0!" => "#ДЕЛ/0!",
20+
"#REF!" => "#ССЫЛКА!",
21+
"#VALUE!" => "#ЗНАЧ!",
22+
"#N/A" => "#Н/Д",
23+
"#NUM!" => "#ЧИСЛО!",
24+
"#NULL!" => "#ПУСТО!",
25+
_ => base.GetErrorValueString(error)
26+
};
27+
}
28+
}
29+
30+
class Program
31+
{
32+
static void Main()
33+
{
34+
// Create LoadOptions (no special options needed for this example)
35+
LoadOptions loadOptions = new LoadOptions();
36+
37+
// Load the workbook; at this point no worksheet has been accessed yet
38+
Workbook workbook = new Workbook("input.xlsx", loadOptions);
39+
40+
// Assign the custom globalization settings BEFORE any worksheet is accessed
41+
workbook.Settings.GlobalizationSettings = new CustomGlobalizationSettings();
42+
43+
// Now worksheets can be used safely with the custom settings applied
44+
Worksheet sheet = workbook.Worksheets[0];
45+
Cell cell = sheet.Cells["A1"];
46+
Console.WriteLine($"Cell[0,0] value: {cell.StringValue}");
47+
48+
// Save the workbook if needed
49+
workbook.Save("output.xlsx");
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using Aspose.Cells;
3+
using Aspose.Cells.Pivot;
4+
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
// Create a new workbook
10+
Workbook workbook = new Workbook();
11+
Worksheet sheet = workbook.Worksheets[0];
12+
Cells cells = sheet.Cells;
13+
14+
// Populate sample data
15+
cells["A1"].PutValue("Category");
16+
cells["B1"].PutValue("Amount");
17+
cells["A2"].PutValue("A");
18+
cells["B2"].PutValue(10);
19+
cells["A3"].PutValue("A");
20+
cells["B3"].PutValue(20);
21+
cells["A4"].PutValue("B");
22+
cells["B4"].PutValue(30);
23+
cells["A5"].PutValue("B");
24+
cells["B5"].PutValue(40);
25+
26+
// Assign custom globalization settings before adding subtotals
27+
workbook.Settings.GlobalizationSettings = new CustomGlobalizationSettings();
28+
29+
// Define the range for subtotal (rows 0‑4, columns 0‑1)
30+
CellArea area = CellArea.CreateCellArea(0, 0, 4, 1);
31+
32+
// Add subtotal: group by column 0 (Category) and calculate Sum on column 1 (Amount)
33+
// Parameters: area, columnIndexToGroup, function, columnsToSubtotal, replace, pageBreaks, summaryBelowData
34+
cells.Subtotal(area, 0, ConsolidationFunction.Sum, new int[] { 1 }, true, false, true);
35+
36+
// Save the workbook
37+
workbook.Save("CustomGlobalizationSubtotal.xlsx");
38+
}
39+
40+
// Custom globalization settings to provide localized total label
41+
class CustomGlobalizationSettings : GlobalizationSettings
42+
{
43+
public override string GetTotalName(ConsolidationFunction functionType)
44+
{
45+
// Return a custom label for the Sum total; fall back to default for others
46+
return functionType == ConsolidationFunction.Sum
47+
? "Custom Sum Total"
48+
: base.GetTotalName(functionType);
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)