I'm currently working on a C# project, to convert alot of files (14000+ files) from an old Office format, to the newest. It's all .doc and .xls files.
I use b2xtranslator to do it.
The following code works for converting .doc files, and the files appear in the target folder. However the .xlsx files doesn't show up. The code for converting .xls files does not throw any exceptions, or fail, the results just doesn't show up in the folder.
static void Main(string[] args) {
string oldFilesPath = "C://Old Files";
string newFilesPath = "C://New Files";
string[] files = Directory.GetFiles(oldFilesPath);
Console.WriteLine($"Fetched {files.Length} files");
int counter = 1;
foreach(var filePath in files) {
FileInfo fileInfo = new FileInfo(filePath);
if(fileInfo.Extension == ".xls" || fileInfo.Extension == ".doc") {
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); //https://stackoverflow.com/a/58074654/10447665
StructuredStorageReader reader = new StructuredStorageReader(fileInfo.FullName);
DocumentType outType;
string newPath = $"{newFilesPath}//{fileInfo.Name}x";
var start = DateTime.Now;
string diff;
switch (fileInfo.Extension) {
case ".doc":
// https://github.com/EvolutionJobs/b2xtranslator/blob/master/Shell/doc2x/Program.cs
WordDocument fileDoc = new WordDocument(reader);
outType = WordConverter.DetectOutputType(fileDoc);
WordprocessingDocument fileDocx = WordprocessingDocument.Create(newPath, outType);
WordConverter.Convert(fileDoc, fileDocx);
diff = DateTime.Now.Subtract(start).TotalSeconds.ToString(CultureInfo.InvariantCulture);
Console.WriteLine($"{counter}/{files.Length} > File Converted from doc to docx in {diff} seconds > {fileInfo.Name}");
break;
case ".xls":
// https://github.com/EvolutionJobs/b2xtranslator/blob/master/Shell/xls2x/Program.cs
XlsDocument fileXls = new XlsDocument(reader);
outType = ExcelConverter.DetectOutputType(fileXls);
SpreadsheetDocument fileXlsx = SpreadsheetDocument.Create(newPath, outType);
ExcelConverter.Convert(fileXls, fileXlsx);
diff = DateTime.Now.Subtract(start).TotalSeconds.ToString(CultureInfo.InvariantCulture);
Console.WriteLine($"{counter}/{files.Length} > File Converted from xls to xlsx in {diff} seconds > {fileInfo.Name}");
break;
default:
Console.WriteLine("It shouldn't be possible to reach this point");
break;
}
reader.Close();
} else {
Console.WriteLine($"{counter}/{files.Length} > File does not need update, or can't be handled > {fileInfo.Name}");
}
counter++;
}
Console.ReadLine();
}