-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOfficeHandler.Excel.cs
110 lines (100 loc) · 3.57 KB
/
OfficeHandler.Excel.cs
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
using System;
using System.IO;
using Microsoft.Office.Interop.Excel;
using static OfficeFileVersionUpdater.Program;
namespace OfficeFileVersionUpdater;
public class ExcelHandler : OfficeHandler
{
private Application _excelApp;
/// <summary>
/// Fetches a new Excel process
/// </summary>
/// <returns>A Excel process</returns>
internal Application StartApp()
{
try
{
_excelApp = new Application
{
WindowState = XlWindowState.xlMinimized,
AskToUpdateLinks = false,
Visible = true
};
}
catch
{
ProgramHelpers.ExitWithMessage(exitReason: ExitReasons.ExcelNotInstalled);
}
return _excelApp;
}
internal void ProcessAndSaveFile(Workbook excelWbk)
{
string origFileName = Path.Combine(path1: FolderName, path2: excelWbk.Name);
string newFileName = null;
// check compatibility [aka extension-eqsue]
if (excelWbk.Excel8CompatibilityMode)
{
bool savedOk = false;
try
{
newFileName = SaveActualFile(workbook: excelWbk,
savedOk: out savedOk);
}
catch
{
Console.WriteLine(value: "-- Save failed for " + origFileName);
}
if (savedOk)
{
// kill old version file if applicable
File.Delete(path: origFileName);
File.SetLastWriteTime(path: newFileName, lastWriteTime: LastModified);
PrintSavedAs(fileNameToSaveAs: newFileName,
lastModified:
LastModified); // kinda fake here for lastModified but if we got this far then setting the TS should be ok.
}
}
else // current-version file.
{
excelWbk.Close(SaveChanges: false);
Console.WriteLine(value: "- Ignored up-to-date version file: " + origFileName);
}
}
public Workbook OpenFile(string fileNameWithPath, Application excelApp)
{
FileInfo fi = new(fileName: fileNameWithPath);
Workbook excelWbk = null;
if (!fi.Name.Contains(value: "~$") && fi.Extension == ".xls")
try
{
Console.WriteLine(value: "Processing " + fileNameWithPath);
FolderName = fi.DirectoryName;
LastModified = GetLastModifiedDt(fileToCheck: fileNameWithPath);
excelWbk = excelApp.Workbooks.Open(Filename: fileNameWithPath);
}
catch (Exception ex)
// System.Runtime.InteropServices.COMException -- generally this will be stuff like pre-Office97 files.
// HRESULT: 0x800A03EC -- you most likely provided the wrong password.
{
Console.WriteLine(value: "-- " + ex.Message);
}
return excelWbk;
}
private string SaveActualFile(Workbook workbook, out bool savedOk)
{
string newFileName =
Path.Combine(path1: FolderName, path2: workbook.HasVBProject ? workbook.Name + "m" : workbook.Name + "x");
workbook.SaveAs(Filename: newFileName,
FileFormat: workbook.HasVBProject
? XlFileFormat.xlOpenXMLWorkbookMacroEnabled
: XlFileFormat.xlOpenXMLWorkbook);
savedOk = true;
workbook.Close(SaveChanges: false);
return newFileName;
}
public void QuitApp()
{
_excelApp.Quit();
Console.WriteLine(value: "Excel files done");
}
}