Files
2025-11-11 11:03:42 +01:00

115 lines
4.0 KiB
C#

using System;
using System.IO;
using System.Windows;
using Microsoft.Win32;
using Excel = Microsoft.Office.Interop.Excel;
using Newtonsoft.Json;
using System.Reflection;
using C4IT.Logging;
using static C4IT.Logging.cLogManager;
namespace FasdExcelToJsonConverter
{
public partial class MainWindow : Window
{
#region Properties
#region SelectedFile
public static readonly DependencyProperty SelectedFileProperty =
DependencyProperty.Register("SelectedFile", typeof(string), typeof(MainWindow), new PropertyMetadata(""));
public string SelectedFile
{
get { return (string)GetValue(SelectedFileProperty); }
set { SetValue(SelectedFileProperty, value); }
}
#endregion
private readonly Excel.Application excelApp = new Excel.Application();
private Excel.Workbook excelWorkBook;
#endregion
public MainWindow()
{
InitializeComponent();
}
private void SaveSelectedFileAsJson()
{
var CM = MethodBase.GetCurrentMethod();
LogMethodBegin(CM);
try
{
excelWorkBook = excelApp.Workbooks.Open(SelectedFile);
cExcelHealthcardParser healthCardParser = new cExcelHealthcardParser(excelWorkBook);
if (!healthCardParser.GetHealthcardDataFromExcel("%RawData%", out var healthCardData))
return;
SaveFileDialog saveFileDialog = new SaveFileDialog
{
Filter = "Json files (*.json)|*.json"
};
if (saveFileDialog.ShowDialog() == true)
{
healthCardData.SampleDataName = Path.GetFileNameWithoutExtension(saveFileDialog.FileName);
healthCardData.SampleDataId = Guid.NewGuid();
if (healthCardData.SampleDataName == "Kohl, Carlos")
healthCardData.SampleDataId = new Guid("6180aa17-ba2d-455a-bf2f-ec4a075c2d64");
else if (healthCardData.SampleDataName == "Anwender, Peter")
healthCardData.SampleDataId = new Guid("436e8d67-1b9b-4b1a-83e9-0b1e8fa0173b");
else if(healthCardData.SampleDataName == "Ticket, Timo")
healthCardData.SampleDataId = new Guid("42c760d6-90e8-469f-b2fe-ac7d4cc6cb0a");
else if (healthCardData.SampleDataName == "Nichtanwender, Frauke")
healthCardData.SampleDataId = new Guid("2183284f-ba95-4eb8-ac13-6c2f70857e98");
using (StreamWriter streamWriter = File.CreateText(saveFileDialog.FileName))
{
streamWriter.WriteLine(JsonConvert.SerializeObject(healthCardData, Formatting.Indented));
}
MessageBox.Show("Sample Json was created.");
}
}
catch (Exception E)
{
LogException(E);
}
finally
{
excelWorkBook.Close();
excelApp.Quit();
LogMethodEnd(CM);
}
}
private void SearchFileButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "Excel Files|*.xls;*.xlsx;*.xlsm"
};
if (openFileDialog.ShowDialog() == true)
SelectedFile = openFileDialog.FileName;
}
private void ConvertButton_Click(object sender, RoutedEventArgs e)
{
ConvertButton.IsEnabled = false;
ConvertButton.Content = "Converting...";
SaveSelectedFileAsJson();
ConvertButton.IsEnabled = true;
ConvertButton.Content = "Convert to JSON!";
}
}
}