146 lines
4.6 KiB
C#
146 lines
4.6 KiB
C#
using Microsoft.Win32;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Controls.Primitives;
|
|
using System.Windows.Data;
|
|
using System.Windows.Documents;
|
|
using System.Windows.Media;
|
|
|
|
namespace ZKuP
|
|
{
|
|
public static class TableExporter
|
|
{
|
|
public static void ExportToCsv(string filePath, params DataGrid[] grids)
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
foreach (var dg in grids)
|
|
{
|
|
var table = TryGetDataTable(dg);
|
|
|
|
// sichtbare Spalten
|
|
var cols = dg.Columns
|
|
.Where(c => c.Visibility == Visibility.Visible)
|
|
.ToList();
|
|
|
|
// Header
|
|
sb.AppendLine(string.Join(";", cols.Select(c => Escape(GetHeader(c)))));
|
|
|
|
if (table != null)
|
|
{
|
|
// --- CSV aus DataTable (immer vollständig, unabhängig von Virtualisierung) ---
|
|
foreach (DataRow row in table.Rows)
|
|
{
|
|
var vals = cols.Select(col =>
|
|
{
|
|
var name = GetBindingPath(col);
|
|
if (name != null && table.Columns.Contains(name))
|
|
return Escape(Convert.ToString(row[name]));
|
|
return "";
|
|
});
|
|
|
|
sb.AppendLine(string.Join(";", vals));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// --- Fallback: Items → Reflection über BindingPath ---
|
|
foreach (var item in GetAllItems(dg))
|
|
{
|
|
var vals = cols.Select(col =>
|
|
{
|
|
var path = GetBindingPath(col);
|
|
if (path == null) return "";
|
|
var val = GetPropertyValue(item, path);
|
|
return Escape(val?.ToString() ?? "");
|
|
});
|
|
|
|
sb.AppendLine(string.Join(";", vals));
|
|
}
|
|
}
|
|
|
|
sb.AppendLine();
|
|
}
|
|
|
|
File.WriteAllText(filePath, sb.ToString(), Encoding.UTF8);
|
|
}
|
|
|
|
// ---------------- Hilfsfunktionen ----------------
|
|
|
|
private static DataTable TryGetDataTable(DataGrid dg)
|
|
{
|
|
if (dg.ItemsSource is DataView dv) return dv.Table;
|
|
if (dg.ItemsSource is DataTable dt) return dt;
|
|
|
|
if (dg.ItemsSource is IEnumerable ie)
|
|
{
|
|
var first = ie.Cast<object>().FirstOrDefault();
|
|
if (first is DataRowView drv) return drv.DataView.Table;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private static IEnumerable GetAllItems(DataGrid dg)
|
|
{
|
|
if (dg.ItemsSource == null)
|
|
return dg.Items.Cast<object>().Where(x => x != CollectionView.NewItemPlaceholder);
|
|
|
|
if (dg.ItemsSource is System.ComponentModel.ICollectionView cv)
|
|
return cv.SourceCollection;
|
|
|
|
return dg.ItemsSource as IEnumerable;
|
|
}
|
|
|
|
private static string GetHeader(DataGridColumn col)
|
|
{
|
|
if (col.Header is string s) return s;
|
|
return col.Header?.ToString() ?? "";
|
|
}
|
|
|
|
private static string GetBindingPath(DataGridColumn col)
|
|
{
|
|
if (col is DataGridBoundColumn bc && bc.Binding is Binding b && b.Path != null)
|
|
return b.Path.Path;
|
|
|
|
if (col is DataGridCheckBoxColumn cb && cb.Binding is Binding b2 && b2.Path != null)
|
|
return b2.Path.Path;
|
|
|
|
return null;
|
|
}
|
|
|
|
private static object GetPropertyValue(object obj, string path)
|
|
{
|
|
var parts = path.Split('.');
|
|
object current = obj;
|
|
|
|
foreach (var part in parts)
|
|
{
|
|
if (current == null) return null;
|
|
|
|
var type = current.GetType();
|
|
var prop = type.GetProperty(part, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
|
|
if (prop == null) return null;
|
|
|
|
current = prop.GetValue(current);
|
|
}
|
|
|
|
return current;
|
|
}
|
|
|
|
private static string Escape(string s)
|
|
{
|
|
if (s == null) return "";
|
|
if (s.Contains(";") || s.Contains("\""))
|
|
return "\"" + s.Replace("\"", "\"\"") + "\"";
|
|
return s;
|
|
}
|
|
}
|
|
} |