added .xlt

This commit is contained in:
Domi 2024-01-30 13:39:23 +01:00
parent 55e1b7a73d
commit d66c521db0
2 changed files with 77 additions and 3 deletions

View File

@ -29,12 +29,13 @@
<Button x:Name="btnDestFolder" Content="btnDest" Margin="545,171,0,0" VerticalAlignment="Top" Click="btnDestFolder_Click"/> <Button x:Name="btnDestFolder" Content="btnDest" Margin="545,171,0,0" VerticalAlignment="Top" Click="btnDestFolder_Click"/>
</Grid> </Grid>
</GroupBox> </GroupBox>
<GroupBox x:Name="grpFiles" Header="grpFiles" Margin="709,10,0,0" Height="182" VerticalAlignment="Top" HorizontalAlignment="Left" Width="300"> <GroupBox x:Name="grpFiles" Header="grpFiles" Margin="709,10,0,0" Height="243" VerticalAlignment="Top" HorizontalAlignment="Left" Width="300">
<Grid> <Grid>
<CheckBox x:Name="chkWord" Content="Word .doc" HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top"/> <CheckBox x:Name="chkWord" Content="Word .doc" HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top"/>
<CheckBox x:Name="chkExcel" Content="Excel .xls" HorizontalAlignment="Left" Margin="0,45,0,0" VerticalAlignment="Top"/> <CheckBox x:Name="chkExcel" Content="Excel .xls" HorizontalAlignment="Left" Margin="0,45,0,0" VerticalAlignment="Top"/>
<CheckBox x:Name="chkPowerpoint" Content="PowerPoint .ppt" HorizontalAlignment="Left" Margin="0,80,0,0" VerticalAlignment="Top"/> <CheckBox x:Name="chkPowerpoint" Content="PowerPoint .ppt" HorizontalAlignment="Left" Margin="0,80,0,0" VerticalAlignment="Top"/>
<CheckBox x:Name="chkWordTmpl" Content="Wordtemplates .dot" HorizontalAlignment="Left" Margin="0,115,0,0" VerticalAlignment="Top"/> <CheckBox x:Name="chkWordTmpl" Content="Wordtemplates .dot" HorizontalAlignment="Left" Margin="0,115,0,0" VerticalAlignment="Top"/>
<CheckBox x:Name="chkExcelTmpl" Content="Exceltemplates .xlt" HorizontalAlignment="Left" Margin="0,150,0,0" VerticalAlignment="Top"/>
</Grid> </Grid>
</GroupBox> </GroupBox>
<GroupBox x:Name="grpSourceFiles" Header="grpSourceFiles" Margin="10,291,0,0" Height="259" VerticalAlignment="Top" HorizontalAlignment="Left" Width="497"> <GroupBox x:Name="grpSourceFiles" Header="grpSourceFiles" Margin="10,291,0,0" Height="259" VerticalAlignment="Top" HorizontalAlignment="Left" Width="497">
@ -47,7 +48,7 @@
<ListBox x:Name="lstDestFiles" ItemsSource="{Binding convertedFiles}" /> <ListBox x:Name="lstDestFiles" ItemsSource="{Binding convertedFiles}" />
</Grid> </Grid>
</GroupBox> </GroupBox>
<Button x:Name="btnConvert" Content="Button" HorizontalAlignment="Left" Margin="10,577,0,0" VerticalAlignment="Top" Width="174" Height="42" Click="btnConvert_Click"/> <Button x:Name="btnConvert" Content="Button" HorizontalAlignment="Left" Margin="10,555,0,0" VerticalAlignment="Top" Width="174" Height="42" Click="btnConvert_Click"/>
<Label x:Name="lblState" Content="lblState" HorizontalAlignment="Left" Margin="709,258,0,0" VerticalAlignment="Top" Width="300" Height="28"/> <Label x:Name="lblState" Content="lblState" HorizontalAlignment="Left" Margin="709,258,0,0" VerticalAlignment="Top" Width="300" Height="28"/>
<Button x:Name="btnExport" Content="btnExport" HorizontalAlignment="Left" Height="33" Margin="915,555,0,0" VerticalAlignment="Top" Width="94" Click="btnExport_Click"/> <Button x:Name="btnExport" Content="btnExport" HorizontalAlignment="Left" Height="33" Margin="915,555,0,0" VerticalAlignment="Top" Width="94" Click="btnExport_Click"/>
<Button x:Name="btnDelete" Content="btnDelete" HorizontalAlignment="Left" Margin="793,555,0,0" VerticalAlignment="Top" Width="117" Height="33" Click="btnDelete_Click"/> <Button x:Name="btnDelete" Content="btnDelete" HorizontalAlignment="Left" Margin="793,555,0,0" VerticalAlignment="Top" Width="117" Height="33" Click="btnDelete_Click"/>

View File

@ -35,6 +35,7 @@ namespace OfficeConverter
bool doExcel = false; bool doExcel = false;
bool doPPoint = false; bool doPPoint = false;
bool doWordTmpl = false; bool doWordTmpl = false;
bool doExcelTmpl = false;
string errorFolderEmpty = ""; string errorFolderEmpty = "";
public MainWindow() public MainWindow()
@ -78,6 +79,7 @@ namespace OfficeConverter
doExcel = (bool)chkExcel.IsChecked; doExcel = (bool)chkExcel.IsChecked;
doPPoint = (bool)chkPowerpoint.IsChecked; doPPoint = (bool)chkPowerpoint.IsChecked;
doWordTmpl = (bool)chkWordTmpl.IsChecked; doWordTmpl = (bool)chkWordTmpl.IsChecked;
doExcelTmpl = (bool)chkExcelTmpl.IsChecked;
// Check if the background worker is not already running // Check if the background worker is not already running
if (!backgroundWorker.IsBusy) if (!backgroundWorker.IsBusy)
@ -163,6 +165,8 @@ namespace OfficeConverter
string[] xlsFiles = null; string[] xlsFiles = null;
string[] pptFiles = null; string[] pptFiles = null;
string[] dotFiles = null; string[] dotFiles = null;
string[] xltFiles = null;
if (doWord) if (doWord)
{ {
@ -180,6 +184,10 @@ namespace OfficeConverter
{ {
dotFiles = Directory.GetFiles(folderPath, "*.dot"); dotFiles = Directory.GetFiles(folderPath, "*.dot");
} }
if (doExcelTmpl)
{
dotFiles = Directory.GetFiles(folderPath, "*.dot");
}
// Check for null before adding to combinedFiles // Check for null before adding to combinedFiles
if (docFiles != null) if (docFiles != null)
@ -198,7 +206,10 @@ namespace OfficeConverter
{ {
combinedFiles.AddRange(dotFiles); combinedFiles.AddRange(dotFiles);
} }
if (xltFiles != null)
{
combinedFiles.AddRange(xltFiles);
}
Console.WriteLine($"Processing files in folder: {folderPath}"); Console.WriteLine($"Processing files in folder: {folderPath}");
/// Create a copy of the collection to avoid modification during iteration /// Create a copy of the collection to avoid modification during iteration
@ -298,6 +309,9 @@ namespace OfficeConverter
case ".dot": case ".dot":
ConvertDocToDotx(filePath, doSubfolders, doReplace); ConvertDocToDotx(filePath, doSubfolders, doReplace);
break; break;
case ".xlt":
ConvertXltToXltxAsync(filePath, doSubfolders, doReplace);
break;
default: default:
// Handle other file types or show an error message // Handle other file types or show an error message
@ -359,6 +373,65 @@ namespace OfficeConverter
KillProcess("EXCEL"); KillProcess("EXCEL");
} }
} }
private async Task ConvertXltToXltxAsync(string xltFile, bool doSubfolders, bool doReplace)
{
await Task.Run(() =>
{
try
{
Excel.Application excelApp = new Excel.Application();
excelApp.DisplayAlerts = false;
try
{
Excel.Workbook workbook = excelApp.Workbooks.Open(xltFile);
string 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
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
System.Windows.Application.Current.Dispatcher.Invoke(() =>
{
combinedFiles.Remove(xltFile);
convertedFiles.Add(xltFile);
DisplayCombinedFiles();
});
}
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();