-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
140 lines (120 loc) · 4.98 KB
/
Program.cs
File metadata and controls
140 lines (120 loc) · 4.98 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
using ExcelExporter.Classes;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Excel Exporter - Command Line Interface");
Console.WriteLine("====================================");
Console.WriteLine("A tool for exporting data to Excel files");
Console.WriteLine();
if (!args.Any())
{
Console.WriteLine("Temporarily here for testing. Input args, seperated by ';'");
args = (Console.ReadLine() ?? "").Split(";");
if (args.Length == 0) { return; }
for (int i = 0; i < args.Count(); i++)
{
args[i] = args[i].Trim();
}
}
if (!ArgumentsHandler.HandleArguments(args, out ParsedArguments parsedArguments, out string? messages))
{
Console.WriteLine(messages);
return;
}
if (string.IsNullOrEmpty(parsedArguments.InputFilePath) || string.IsNullOrEmpty(parsedArguments.FileType) || string.IsNullOrEmpty(parsedArguments.OutputDir))
{
Console.WriteLine("No inputs specified. Use --help to see usage.");
return;
}
string workbookName = parsedArguments.OutputName + "_internals";
string workbookDir = Path.Combine(parsedArguments.OutputDir, workbookName);
GenerateRequiredDirectories.Run(parsedArguments.InputFilePath, parsedArguments.FileType, workbookDir);
string worksheetDir = Path.Combine(workbookDir, "Sheets");
string vbaDir = Path.Combine(workbookDir, "VBA");
string ribbonXDir = Path.Combine(workbookDir, "RibbonX");
#region Excel Export
VBA_Handling vbaHandling = new VBA_Handling();
Ribbon_Handling ribbonHandling = new Ribbon_Handling();
Excel.Application xlApp = new Excel.Application()
{
Visible = false,
DisplayAlerts = false,
AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable
};
xlApp.EnableEvents = false;
Excel.Workbook xlWB = null;
try
{
xlWB = xlApp.Workbooks.Open(
parsedArguments.InputFilePath,
UpdateLinks: 0,
ReadOnly: true,
IgnoreReadOnlyRecommended: true,
AddToMru: false
);
foreach (Excel.Worksheet xlWS in xlWB.Sheets)
{
try
{
Console.WriteLine($"Exporting worksheet '{xlWS.Name}'...");
string displaySheetName = xlWS.Name + "_display";
string formulaSheetName = xlWS.Name + "_formula";
Excel.Range xlRng_used = xlWS.UsedRange;
object[,] valueArray = xlRng_used.Value2 as object[,];
object[,] formulaArray = xlRng_used.Formula as object[,];
CSVHandler.WriteToCSV(valueArray, worksheetDir, displaySheetName);
CSVHandler.WriteToCSV(formulaArray, worksheetDir, formulaSheetName);
try
{
vbaHandling.ExportWorksheetVBA(xlWS, vbaDir);
}
catch (Exception ex)
{
Console.WriteLine($"Warning: Could not export VBA code for worksheet '{xlWS.Name}': {ex.Message}");
}
}
finally
{
Marshal.FinalReleaseComObject(xlWS);
}
}
vbaHandling.ExportModulesVBA(xlWB, vbaDir);
vbaHandling.ExportClassesVBA(xlWB, vbaDir);
vbaHandling.ExportFormsVBA(xlWB, vbaDir);
vbaHandling.ExportThisWorkbookVBA(xlWB, vbaDir);
string tempPath = Path.Combine(workbookDir, "TempRibbonExport.xlsm");
xlWB.SaveCopyAs(tempPath);
ribbonHandling.ExportRibbonXML(tempPath, ribbonXDir);
File.Delete(tempPath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
#region Cleanup
Console.WriteLine("Cleaning up...");
if (xlWB != null)
{
xlWB.Close(false);
Marshal.FinalReleaseComObject(xlWB);
xlWB = null;
}
if (xlApp != null)
{
xlApp.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityByUI;
xlApp.Quit();
Marshal.FinalReleaseComObject(xlApp);
xlApp = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
#endregion
}
#endregion
}
}