Files
OnDoc/Tool_API_Dokumentgenerator/Form1.cs
T
Stefan Hutter 83a0703324 Update 20260514
2026-05-14 10:49:27 +02:00

364 lines
14 KiB
C#

using ExcelNet;
using FastExcel;
using Syncfusion.Windows.Forms.Edit;
using Syncfusion.XlsIO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
namespace Tool_API_Dokumentgenerator
{
public partial class Form1 : Form
{
public class ControlState
{
public string Name { get; set; }
public string Type { get; set; }
public string Value { get; set; }
}
public Form1()
{
InitializeComponent();
}
string datapath = Properties.Settings.Default.Datapath;
string inputdata = "";
string tabelle1 = "";
string tabelle2 = "";
string tabelle3 = "";
string tabelle4 = "";
DataTable ExcelMapping = new DataTable();
private void start()
{
ExcelMapping.Columns.Clear();
ExcelMapping.Columns.Add("Tag");
ExcelMapping.Columns.Add("Excelspalte");
ExcelMapping.TableName = "ExcelMappoing";
}
private void button2_Click(object sender, EventArgs e)
{
Generate(false);
}
private void button5_Click(object sender, EventArgs e)
{
System.IO.File.WriteAllText(datapath + txttextinput.Text, editControl1.Text);
Generate(true);
}
private void Generate(bool demo) {
System.Data.DataTable importdata = new System.Data.DataTable();
ExcelNet.ExcelReader reader = new ExcelNet.ExcelReader();
string selected_sheetname = "";
var sheets = new List<string>();
try
{
sheets = reader.Get_ExcelSheets(datapath+txtexcelinput.Text);
}
catch
{
MessageBox.Show("Auf die gewählte Excel-Datei kann aktuell nicht zugegriffen werden. Evtl. ist diese noch geöffnet.", "Excel-Import", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
selected_sheetname = sheets[0];
Import_sf_Excel(datapath+txtexcelinput.Text, selected_sheetname, ref importdata);
DataView dv = new DataView(importdata ); dv.Sort = txtspalten.Text.Replace(";", ",");
importdata.DefaultView.Sort = txtspalten.Text.Replace(";", ",");
string oldkey = "";
int counter = 0;
foreach (DataRowView rowView in dv)
{
DataRow row = rowView.Row;
if (oldkey != row[txtdokumenttrenner.Text].ToString())
{
if (counter > 0)
{
if (tabelle1 != "")
{
tabelle1= "<p><table>" + tabelle1 + "</table>";
inputdata = inputdata.Replace(txttag1.Text, tabelle1);
if (txtueberschrift1.Text != "") { inputdata = inputdata.Replace(txttagueberschrift1.Text, txtueberschrift1.Text); }
}
if (tabelle2 != "")
{
tabelle2 = "<p><table>" + tabelle2 + "</table>";
inputdata = inputdata.Replace(txttag2.Text, tabelle2);
if (txtueberschrift2.Text != "") { inputdata = inputdata.Replace(txttagueberschrift2.Text, txtueberschrift2.Text); }
}
if (tabelle3 != "")
{
tabelle3 = "<p><table>" + tabelle3 + "</table>";
inputdata = inputdata.Replace(txttag3.Text, tabelle3);
if (txtueberschrift3.Text != "") { inputdata = inputdata.Replace(txttagueberschrift3.Text, txtueberschrift3.Text); }
}
if (tabelle4 != "")
{
tabelle4 = "<p><table>" + tabelle4 + "</table>";
inputdata = inputdata.Replace(txttag4.Text, tabelle4);
if (txtueberschrift4.Text != "") { inputdata = inputdata.Replace(txttagueberschrift4.Text, txtueberschrift4.Text); }
}
if (tabelle1 == "" && txttag1.Text!="")
{
inputdata=inputdata.Replace(txttag1.Text,"");
inputdata=inputdata.Replace(txttagueberschrift1.Text, "");
}
if (tabelle2 == "" && txttag2.Text!="")
{
inputdata = inputdata.Replace(txttag2.Text, "");
inputdata = inputdata.Replace(txttagueberschrift2.Text, "");
}
if (tabelle3 == "" && txttag3.Text != "")
{
inputdata = inputdata.Replace(txttag3.Text, "");
inputdata = inputdata.Replace(txttagueberschrift3.Text, "");
}
if (tabelle4 == "" && txttag4.Text != "")
{
inputdata = inputdata.Replace(txttag4.Text, "");
inputdata = inputdata.Replace(txttagueberschrift4.Text, "");
}
System.IO.File.WriteAllText(datapath+@"\output\"+this.txtname.Text+"_Data_" + oldkey + ".json", inputdata);
if (demo)
{
this.editControl2.Text = inputdata;
panel1.Visible = true;
panel1.Dock = DockStyle.Right;
editControl1.Visible = false;
return;
}
}
oldkey = row[txtdokumenttrenner.Text].ToString();
inputdata = System.IO.File.ReadAllText(datapath + txttextinput.Text);
tabelle1 = "";tabelle2 = "";tabelle3 = "";tabelle4 = "";
editControl1.Text=inputdata;
counter++;
}
if (txttnr1.Text != "" && row["tabelle"].ToString() == txttnr1.Text)
{
tabelle1 += "<row>" ;
string[] cols = txttabellenspalten.Text.Split(';');
for (int i = 0; i < cols.Length; i++)
{
tabelle1 = tabelle1 + "<col>" + row[cols[i]].ToString() + "</col>";
}
tabelle1 += "</row>" ;
}
if (txttnr2.Text != "" && row["tabelle"].ToString() == txttnr2.Text)
{
tabelle2 += "<row>";
string[] cols = txttabellenspalten.Text.Split(';');
for (int i = 0; i < cols.Length; i++)
{
tabelle2 = tabelle2 + "<col>" + row[cols[i]].ToString() + "</col>";
}
tabelle2 += "</row>";
}
if (txttnr3.Text != "" && row["tabelle"].ToString() == txttnr2.Text)
{
tabelle3 += "<row>" ;
string[] cols = txttabellenspalten.Text.Split(';');
for (int i = 0; i < cols.Length; i++)
{
tabelle3 = tabelle3 + "<col>" + row[cols[i]].ToString() + "</col>";
}
tabelle3 += "</row>";
}
if (txttnr4.Text != "" && row["tabelle"].ToString() == txttnr2.Text)
{
tabelle4 += "<row>";
string[] cols = txttabellenspalten.Text.Split(';');
for (int i = 0; i < cols.Length; i++)
{
tabelle4 = tabelle4 + "<col>" + row[cols[i]].ToString() + "</col>";
}
tabelle4 += "</row>";
}
foreach (DataRow dr in ExcelMapping.Rows)
{
if (dr["Tag"].ToString() != "" && row[dr["Excelspalte"].ToString()] != "")
{
inputdata = inputdata.Replace(dr["Tag"].ToString(), row[dr["Excelspalte"].ToString()].ToString());
}
}
}
}
private void Import_sf_Excel(string filename, string sheetname, ref System.Data.DataTable importdata)
{
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
IWorkbook workbook = application.Workbooks.Open(filename);
IWorksheet sheet = workbook.Worksheets[sheetname];
importdata = sheet.ExportDataTable(sheet.UsedRange, ExcelExportDataTableOptions.ColumnNames);
sheet = null;
workbook.Close();
workbook = null;
excelEngine = null;
}
private void txtspalten_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
start();
sfDataGrid1.DataSource = ExcelMapping;
}
private void button1_Click(object sender, EventArgs e)
{
SaveControlsToJson(this, datapath+txtname.Text+".json");
ExcelMapping.AcceptChanges();
ExcelMapping.WriteXml(datapath + txtname.Text + "_mapping.xml", XmlWriteMode.WriteSchema);
System.IO.File.WriteAllText(datapath + txttextinput.Text,editControl1.Text);
}
private void button3_Click(object sender, EventArgs e)
{
LoadControlsFromJson(this, datapath + txtname.Text + ".json");
ExcelMapping.Rows.Clear();
ExcelMapping.ReadXml(datapath + txtname.Text + "_mapping.xml");
inputdata = System.IO.File.ReadAllText(datapath+txttextinput.Text);
tabelle1 = ""; tabelle2 = ""; tabelle3 = ""; tabelle4 = "";
editControl1.Text = inputdata;
}
public void SaveControlsToJson(Control parent, string filePath)
{
var list = new List<ControlState>();
foreach (Control ctrl in GetAllControls(parent))
{
var state = new ControlState
{
Name = ctrl.Name,
Type = ctrl.GetType().Name
};
switch (ctrl)
{
case TextBox tb:
state.Value = tb.Text;
break;
case CheckBox cb:
state.Value = cb.Checked.ToString();
break;
case ComboBox cmb:
state.Value = cmb.SelectedIndex.ToString();
break;
case DateTimePicker dtp:
state.Value = dtp.Value.ToString("o"); // ISO 8601
break;
}
list.Add(state);
}
var options = new System.Text.Json.JsonSerializerOptions
{
WriteIndented = true
};
string json = System.Text.Json.JsonSerializer.Serialize(list, options);
System.IO.File.WriteAllText(filePath, json);
}
public void LoadControlsFromJson(Control parent, string filePath)
{
if (!System.IO.File.Exists(filePath))
return;
string json = System.IO.File.ReadAllText(filePath);
var list = System.Text.Json.JsonSerializer.Deserialize<List<ControlState>>(json);
foreach (var state in list)
{
Control ctrl = GetAllControls(parent)
.FirstOrDefault(c => c.Name == state.Name);
if (ctrl == null)
continue;
switch (ctrl)
{
case TextBox tb:
tb.Text = state.Value;
break;
case CheckBox cb:
if (bool.TryParse(state.Value, out bool b))
cb.Checked = b;
break;
case ComboBox cmb:
if (int.TryParse(state.Value, out int i))
cmb.SelectedIndex = i;
break;
case DateTimePicker dtp:
if (DateTime.TryParse(state.Value, out DateTime d))
dtp.Value = d;
break;
}
}
}
private IEnumerable<Control> GetAllControls(Control parent)
{
foreach (Control ctrl in parent.Controls)
{
foreach (Control child in GetAllControls(ctrl))
yield return child;
yield return ctrl;
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void txttagueberschrift1_TextChanged(object sender, EventArgs e)
{
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
panel1.Visible = false;
editControl1.Visible= true;
}
}
}