fixed .xlt logic
This commit is contained in:
parent
d66c521db0
commit
b71af71035
|
|
@ -310,9 +310,10 @@ namespace OfficeConverter
|
||||||
ConvertDocToDotx(filePath, doSubfolders, doReplace);
|
ConvertDocToDotx(filePath, doSubfolders, doReplace);
|
||||||
break;
|
break;
|
||||||
case ".xlt":
|
case ".xlt":
|
||||||
ConvertXltToXltxAsync(filePath, doSubfolders, doReplace);
|
ConvertXltToXltx(filePath, doSubfolders, doReplace);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// Handle other file types or show an error message
|
// Handle other file types or show an error message
|
||||||
Console.WriteLine($"Unsupported file type: {filePath}");
|
Console.WriteLine($"Unsupported file type: {filePath}");
|
||||||
|
|
@ -373,54 +374,51 @@ namespace OfficeConverter
|
||||||
KillProcess("EXCEL");
|
KillProcess("EXCEL");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private async Task ConvertXltToXltxAsync(string xltFile, bool doSubfolders, bool doReplace)
|
private void ConvertXltToXltx(string xltFile, bool doSubfolders, bool doReplace)
|
||||||
{
|
{
|
||||||
await Task.Run(() =>
|
try
|
||||||
{
|
{
|
||||||
|
Excel.Application excelApp = new Excel.Application();
|
||||||
|
excelApp.DisplayAlerts = false;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Excel.Application excelApp = new Excel.Application();
|
Excel.Workbook workbook = excelApp.Workbooks.Open(xltFile);
|
||||||
excelApp.DisplayAlerts = false;
|
|
||||||
|
|
||||||
try
|
string targetFolderPath = "";
|
||||||
|
|
||||||
|
// Use Dispatcher.Invoke to execute code on the UI thread
|
||||||
|
Dispatcher.Invoke(() =>
|
||||||
{
|
{
|
||||||
Excel.Workbook workbook = excelApp.Workbooks.Open(xltFile);
|
targetFolderPath = GetTargetFolderPath(doReplace, doSubfolders, xltFile);
|
||||||
|
});
|
||||||
|
|
||||||
string targetFolderPath = "";
|
// Ensure the target folder exists
|
||||||
|
if (!Directory.Exists(targetFolderPath))
|
||||||
// Use Dispatcher.Invoke to execute code on the UI thread
|
|
||||||
Dispatcher.Invoke(() =>
|
|
||||||
{
|
|
||||||
targetFolderPath = GetTargetFolderPath(doReplace, doSubfolders, xltFile);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Ensure the target folder exists
|
|
||||||
if (!Directory.Exists(targetFolderPath))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(targetFolderPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Construct the new path for the .xltx file
|
|
||||||
string newXltxPath = Path.Combine(targetFolderPath, Path.ChangeExtension(Path.GetFileName(xltFile), ".xltx"));
|
|
||||||
workbook.SaveAs(newXltxPath, Excel.XlFileFormat.xlOpenXMLTemplate);
|
|
||||||
workbook.Close();
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
{
|
||||||
// Quit Excel and release resources
|
Directory.CreateDirectory(targetFolderPath);
|
||||||
excelApp.Quit();
|
|
||||||
Marshal.ReleaseComObject(excelApp);
|
|
||||||
|
|
||||||
// Ensure Excel processes are terminated
|
|
||||||
KillProcess("EXCEL");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Construct the new path for the .xltx file
|
||||||
|
string newXltxPath = Path.Combine(targetFolderPath, Path.ChangeExtension(Path.GetFileName(xltFile), ".xltx"));
|
||||||
|
workbook.SaveAs(newXltxPath, Excel.XlFileFormat.xlOpenXMLTemplate);
|
||||||
|
workbook.Close();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
finally
|
||||||
{
|
{
|
||||||
// Handle exceptions during conversion
|
// Quit Excel and release resources
|
||||||
Console.WriteLine($"Error converting {xltFile}: {ex.Message}");
|
excelApp.Quit();
|
||||||
|
Marshal.ReleaseComObject(excelApp);
|
||||||
|
|
||||||
|
// Ensure Excel processes are terminated
|
||||||
|
KillProcess("EXCEL");
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Handle exceptions during conversion
|
||||||
|
Console.WriteLine($"Error converting {xltFile}: {ex.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
// Update UI on the main thread
|
// Update UI on the main thread
|
||||||
System.Windows.Application.Current.Dispatcher.Invoke(() =>
|
System.Windows.Application.Current.Dispatcher.Invoke(() =>
|
||||||
|
|
@ -432,6 +430,7 @@ namespace OfficeConverter
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void ConvertPptToPptx(string pptFile, bool doSubfolders, bool doReplace)
|
private void ConvertPptToPptx(string pptFile, bool doSubfolders, bool doReplace)
|
||||||
{
|
{
|
||||||
PowerPoint.Application pptApp = new PowerPoint.Application();
|
PowerPoint.Application pptApp = new PowerPoint.Application();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user