-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
319 lines (279 loc) · 15.4 KB
/
Program.cs
File metadata and controls
319 lines (279 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System;
using System.IO;
using GroupDocs.Comparison;
using GroupDocs.Comparison.Options;
namespace CompareExcelSpreadsheets
{
internal static class Program
{
static void Main()
{
try
{
ApplyLicense();
string baseDir = AppContext.BaseDirectory;
string samplesDir = Path.Combine(baseDir, "..", "..", "..", "sample-files");
string outputDir = Path.Combine(baseDir, "..", "..", "..", "output");
samplesDir = Path.GetFullPath(samplesDir);
outputDir = Path.GetFullPath(outputDir);
Directory.CreateDirectory(samplesDir);
Directory.CreateDirectory(outputDir);
string sourcePath = Path.Combine(samplesDir, "source.xlsx");
string targetPath = Path.Combine(samplesDir, "target.xlsx");
string basicResult = Path.Combine(outputDir, "result_basic.xlsx");
string styledResult = Path.Combine(outputDir, "result_styled.xlsx");
string hideInsertedResult = Path.Combine(outputDir, "result_hide_inserted.xlsx");
string hideDeletedResult = Path.Combine(outputDir, "result_hide_deleted.xlsx");
string leaveGapsResult = Path.Combine(outputDir, "result_leave_gaps.xlsx");
string hideBothResult = Path.Combine(outputDir, "result_hide_both.xlsx");
Console.WriteLine("Source file: " + sourcePath);
Console.WriteLine("Target file: " + targetPath);
Console.WriteLine("Output directory: " + outputDir);
Console.WriteLine();
Console.WriteLine("Running basic Excel comparison...");
BasicComparison(sourcePath, targetPath, basicResult);
Console.WriteLine("Running styled Excel comparison (highlight changes)...");
StyledComparison(sourcePath, targetPath, styledResult);
Console.WriteLine("Running comparison with ShowInsertedContent = false...");
HideInsertedContentComparison(sourcePath, targetPath, hideInsertedResult);
Console.WriteLine("Running comparison with ShowDeletedContent = false...");
HideDeletedContentComparison(sourcePath, targetPath, hideDeletedResult);
Console.WriteLine("Running comparison with LeaveGaps = true...");
LeaveGapsComparison(sourcePath, targetPath, leaveGapsResult);
Console.WriteLine("Running comparison with both ShowInsertedContent and ShowDeletedContent = false...");
HideBothContentComparison(sourcePath, targetPath, hideBothResult);
Console.WriteLine();
Console.WriteLine("Done.");
Console.WriteLine("Basic result: " + basicResult);
Console.WriteLine("Styled result: " + styledResult);
Console.WriteLine("Hide inserted result: " + hideInsertedResult);
Console.WriteLine("Hide deleted result: " + hideDeletedResult);
Console.WriteLine("Leave gaps result: " + leaveGapsResult);
Console.WriteLine("Hide both result: " + hideBothResult);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
/// <summary>
/// Applies the GroupDocs.Comparison license to enable full functionality of the comparison library.
/// </summary>
/// <remarks>
/// This method initializes and applies the license file for GroupDocs.Comparison.
/// Update the licensePath variable with the path to your license file.
/// Without a valid license, the library will operate in evaluation mode with limitations.
/// </remarks>
private static void ApplyLicense()
{
string licensePath = "path to your license file";
License license = new License();
license.SetLicense(licensePath);
}
/// <summary>
/// Performs a basic comparison between two Excel spreadsheets using default comparison settings,
/// identifying and highlighting all differences including inserted, deleted, and modified cells.
/// </summary>
/// <param name="sourcePath">The path to the source Excel file.</param>
/// <param name="targetPath">The path to the target Excel file to compare against.</param>
/// <param name="resultPath">The path where the comparison result will be saved.</param>
/// <remarks>
/// This method uses GroupDocs.Comparison to compare two Excel files without any custom styling or options.
/// The result file will contain all differences highlighted with default formatting.
/// Both source and target files must exist, otherwise a FileNotFoundException will be thrown.
/// </remarks>
private static void BasicComparison(string sourcePath, string targetPath, string resultPath)
{
EnsureFileExists(sourcePath, "source Excel file");
EnsureFileExists(targetPath, "target Excel file");
using (var comparer = new Comparer(sourcePath))
{
comparer.Add(targetPath);
comparer.Compare(resultPath);
}
Console.WriteLine("Basic comparison completed.");
}
/// <summary>
/// Performs a styled comparison between two Excel spreadsheets with custom formatting options,
/// applying distinct visual styles to different types of changes and generating a summary page
/// for comprehensive change tracking and analysis.
/// </summary>
/// <param name="sourcePath">The path to the source Excel file.</param>
/// <param name="targetPath">The path to the target Excel file to compare against.</param>
/// <param name="resultPath">The path where the styled comparison result will be saved.</param>
/// <remarks>
/// This method uses GroupDocs.Comparison with custom styling options to highlight differences.
/// Inserted items are styled with green font, deleted items with brown font, and changed items with firebrick font.
/// All changes are formatted with underline, bold, and italic styles.
/// A summary page is generated at the beginning of the result document.
/// Deleted content is hidden in the output for cleaner visualization.
/// </remarks>
private static void StyledComparison(string sourcePath, string targetPath, string resultPath)
{
EnsureFileExists(sourcePath, "source Excel file");
EnsureFileExists(targetPath, "target Excel file");
var compareOptions = new CompareOptions
{
InsertedItemStyle = new StyleSettings()
{
FontColor = System.Drawing.Color.Green,
IsUnderline = true,
IsBold = true,
IsItalic = true
},
DeletedItemStyle = new StyleSettings()
{
FontColor = System.Drawing.Color.Brown,
IsUnderline = true,
IsBold = true,
IsItalic = true
},
ChangedItemStyle = new StyleSettings()
{
FontColor = System.Drawing.Color.Firebrick,
IsUnderline = true,
IsBold = true,
IsItalic = true
},
GenerateSummaryPage = true,
ShowDeletedContent = false,
};
using (var comparer = new Comparer(sourcePath))
{
comparer.Add(targetPath);
comparer.Compare(resultPath, compareOptions);
}
Console.WriteLine("Styled comparison completed (changes highlighted, summary page generated).");
}
/// <summary>
/// Performs a comparison between two Excel spreadsheets while hiding inserted content,
/// allowing you to focus on deletions and modifications without the visual clutter of new additions.
/// </summary>
/// <param name="sourcePath">The path to the source Excel file.</param>
/// <param name="targetPath">The path to the target Excel file to compare against.</param>
/// <param name="resultPath">The path where the comparison result will be saved.</param>
/// <remarks>
/// This method uses GroupDocs.Comparison with ShowInsertedContent set to false.
/// When comparing documents, any content that was inserted in the target document will not be displayed in the result.
/// This is useful when you only want to see deletions and changes, but not new additions.
/// </remarks>
private static void HideInsertedContentComparison(string sourcePath, string targetPath, string resultPath)
{
EnsureFileExists(sourcePath, "source Excel file");
EnsureFileExists(targetPath, "target Excel file");
var compareOptions = new CompareOptions
{
ShowInsertedContent = false
};
using (var comparer = new Comparer(sourcePath))
{
comparer.Add(targetPath);
comparer.Compare(resultPath, compareOptions);
}
Console.WriteLine("Comparison completed (inserted content hidden).");
}
/// <summary>
/// Performs a comparison between two Excel spreadsheets while hiding deleted content,
/// enabling you to focus on new additions and modifications without displaying removed elements.
/// </summary>
/// <param name="sourcePath">The path to the source Excel file.</param>
/// <param name="targetPath">The path to the target Excel file to compare against.</param>
/// <param name="resultPath">The path where the comparison result will be saved.</param>
/// <remarks>
/// This method uses GroupDocs.Comparison with ShowDeletedContent set to false.
/// When comparing documents, any content that was deleted from the source document will not be displayed in the result.
/// This is useful when you only want to see insertions and changes, but not deletions.
/// </remarks>
private static void HideDeletedContentComparison(string sourcePath, string targetPath, string resultPath)
{
EnsureFileExists(sourcePath, "source Excel file");
EnsureFileExists(targetPath, "target Excel file");
var compareOptions = new CompareOptions
{
ShowDeletedContent = false
};
using (var comparer = new Comparer(sourcePath))
{
comparer.Add(targetPath);
comparer.Compare(resultPath, compareOptions);
}
Console.WriteLine("Comparison completed (deleted content hidden).");
}
/// <summary>
/// Performs a comparison between two Excel spreadsheets while leaving gaps for deleted content,
/// preserving the original document structure and layout to make it easier to identify where content was removed.
/// </summary>
/// <param name="sourcePath">The path to the source Excel file.</param>
/// <param name="targetPath">The path to the target Excel file to compare against.</param>
/// <param name="resultPath">The path where the comparison result will be saved.</param>
/// <remarks>
/// This method uses GroupDocs.Comparison with LeaveGaps set to true.
/// When content is deleted from the source document, empty cells or gaps will be left in the result document
/// to maintain the original structure and make it easier to identify where content was removed.
/// This helps preserve the document layout and makes deletions more visible.
/// </remarks>
private static void LeaveGapsComparison(string sourcePath, string targetPath, string resultPath)
{
EnsureFileExists(sourcePath, "source Excel file");
EnsureFileExists(targetPath, "target Excel file");
var compareOptions = new CompareOptions
{
LeaveGaps = true
};
using (var comparer = new Comparer(sourcePath))
{
comparer.Add(targetPath);
comparer.Compare(resultPath, compareOptions);
}
Console.WriteLine("Comparison completed (gaps left for deleted content).");
}
/// <summary>
/// Performs a comparison between two Excel spreadsheets while hiding both inserted and deleted content,
/// displaying only modified cells to provide a focused view of actual changes while maintaining document structure.
/// </summary>
/// <param name="sourcePath">The path to the source Excel file.</param>
/// <param name="targetPath">The path to the target Excel file to compare against.</param>
/// <param name="resultPath">The path where the comparison result will be saved.</param>
/// <remarks>
/// This method uses GroupDocs.Comparison with ShowInsertedContent and ShowDeletedContent both set to false,
/// and LeaveGaps set to true. This configuration hides both inserted and deleted content while leaving gaps
/// where deletions occurred. This is useful when you only want to see modified content (changes) without
/// the clutter of insertions and deletions, while still maintaining the document structure.
/// </remarks>
private static void HideBothContentComparison(string sourcePath, string targetPath, string resultPath)
{
EnsureFileExists(sourcePath, "source Excel file");
EnsureFileExists(targetPath, "target Excel file");
var compareOptions = new CompareOptions
{
ShowInsertedContent = false,
ShowDeletedContent = false,
LeaveGaps = true
};
using (var comparer = new Comparer(sourcePath))
{
comparer.Add(targetPath);
comparer.Compare(resultPath, compareOptions);
}
Console.WriteLine("Comparison completed (both inserted and deleted content hidden, gaps left).");
}
/// <summary>
/// Ensures that a file exists at the specified path, throwing a FileNotFoundException with a descriptive
/// error message if the file is not found, to prevent comparison operations on non-existent files.
/// </summary>
/// <param name="path">The file path to check.</param>
/// <param name="description">A description of the file for error messages.</param>
/// <exception cref="FileNotFoundException">Thrown when the file does not exist at the specified path.</exception>
/// <remarks>
/// This helper method validates that a file exists before attempting to use it in comparison operations.
/// If the file is not found, a FileNotFoundException is thrown with a descriptive error message.
/// </remarks>
private static void EnsureFileExists(string path, string description)
{
if (!File.Exists(path))
{
throw new FileNotFoundException($"The {description} was not found. Path:", path);
}
}
}
}