commit 69308264836239635732fbe2c43122f546cb3c39 Author: Eugen Höglinger Date: Wed Feb 26 15:15:19 2020 +0100 Projekt neu angelegt. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5a5f80a --- /dev/null +++ b/.gitignore @@ -0,0 +1,32 @@ + +#Ignore thumbnails created by Windows +Thumbs.db +#Ignore files built by Visual Studio +*.obj +*.exe +*.pdb +*.user +*.aps +*.pch +*.vspscc +*_i.c +*_p.c +*.ncb +*.suo +*.tlb +*.tlh +*.bak +*.cache +*.ilk +*.log +[Bb]in +[Dd]ebug*/ +*.lib +*.sbr +obj/ +[Rr]elease*/ +_ReSharper*/ +[Tt]est[Rr]esult* +.vs/ +#Nuget packages folder +packages/ diff --git a/Daten/Excel.zip b/Daten/Excel.zip new file mode 100644 index 0000000..7be32ba Binary files /dev/null and b/Daten/Excel.zip differ diff --git a/Excel/Excel.csproj b/Excel/Excel.csproj new file mode 100644 index 0000000..39b4622 --- /dev/null +++ b/Excel/Excel.csproj @@ -0,0 +1,114 @@ + + + + + Debug + AnyCPU + {6CD3DB7C-2EEF-489D-AA5D-FF9E2382EF08} + WinExe + Properties + CSV20.Excel + Excel + v3.5 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + Form + + + Form1.cs + + + + + Form1.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + True + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + {2DF8D04C-5BFA-101B-BDE5-00AA0044DE52} + 2 + 7 + 0 + primary + False + True + + + {00020813-0000-0000-C000-000000000046} + 1 + 8 + 0 + primary + False + True + + + {0002E157-0000-0000-C000-000000000046} + 5 + 3 + 0 + primary + False + True + + + + + \ No newline at end of file diff --git a/Excel/Excel.sln b/Excel/Excel.sln new file mode 100644 index 0000000..b291d03 --- /dev/null +++ b/Excel/Excel.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.572 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Excel", "Excel.csproj", "{6CD3DB7C-2EEF-489D-AA5D-FF9E2382EF08}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6CD3DB7C-2EEF-489D-AA5D-FF9E2382EF08}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6CD3DB7C-2EEF-489D-AA5D-FF9E2382EF08}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6CD3DB7C-2EEF-489D-AA5D-FF9E2382EF08}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6CD3DB7C-2EEF-489D-AA5D-FF9E2382EF08}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B75E968F-90F4-4B98-BAF1-B4CC7A40C2B5} + EndGlobalSection +EndGlobal diff --git a/Excel/Form1.Designer.cs b/Excel/Form1.Designer.cs new file mode 100644 index 0000000..36ac7ea --- /dev/null +++ b/Excel/Form1.Designer.cs @@ -0,0 +1,92 @@ +namespace CSV20.Excel +{ + partial class Form1 + { + /// + /// Erforderliche Designervariable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Verwendete Ressourcen bereinigen. + /// + /// True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Vom Windows Form-Designer generierter Code + + /// + /// Erforderliche Methode für die Designerunterstützung. + /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden. + /// + private void InitializeComponent() + { + this.buttonSpeichern = new System.Windows.Forms.Button(); + this.dataGridViewExcel = new System.Windows.Forms.DataGridView(); + this.openFileDialogExcel = new System.Windows.Forms.OpenFileDialog(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridViewExcel)).BeginInit(); + this.SuspendLayout(); + // + // buttonSpeichern + // + this.buttonSpeichern.Dock = System.Windows.Forms.DockStyle.Bottom; + this.buttonSpeichern.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.buttonSpeichern.Location = new System.Drawing.Point(0, 428); + this.buttonSpeichern.Name = "buttonSpeichern"; + this.buttonSpeichern.Size = new System.Drawing.Size(782, 25); + this.buttonSpeichern.TabIndex = 0; + this.buttonSpeichern.Text = "Änderungen speichern"; + this.buttonSpeichern.UseVisualStyleBackColor = true; + this.buttonSpeichern.Click += new System.EventHandler(this.buttonSpeichern_Click); + // + // dataGridViewExcel + // + this.dataGridViewExcel.AllowUserToAddRows = false; + this.dataGridViewExcel.AllowUserToDeleteRows = false; + this.dataGridViewExcel.AllowUserToResizeRows = false; + this.dataGridViewExcel.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridViewExcel.Dock = System.Windows.Forms.DockStyle.Fill; + this.dataGridViewExcel.Location = new System.Drawing.Point(0, 0); + this.dataGridViewExcel.Name = "dataGridViewExcel"; + this.dataGridViewExcel.RowTemplate.Height = 24; + this.dataGridViewExcel.Size = new System.Drawing.Size(782, 428); + this.dataGridViewExcel.TabIndex = 1; + // + // openFileDialogExcel + // + this.openFileDialogExcel.DefaultExt = "xlsx"; + this.openFileDialogExcel.Filter = "Excel 2003 (*.xls)|*.xls|Excel 2007 (*.xlsx)|*.xlsx"; + this.openFileDialogExcel.FilterIndex = 2; + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(782, 453); + this.Controls.Add(this.dataGridViewExcel); + this.Controls.Add(this.buttonSpeichern); + this.Name = "Form1"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "Excel Datenblatt"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); + this.Load += new System.EventHandler(this.Form1_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridViewExcel)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.Button buttonSpeichern; + private System.Windows.Forms.DataGridView dataGridViewExcel; + private System.Windows.Forms.OpenFileDialog openFileDialogExcel; + } +} + diff --git a/Excel/Form1.cs b/Excel/Form1.cs new file mode 100644 index 0000000..5775627 --- /dev/null +++ b/Excel/Form1.cs @@ -0,0 +1,107 @@ +/*************************************\ + * Excel-Dokumente lesen und * + * bearbeiten * + * * + * Kapitel: Erweiterte * + * Programmierthemen * + * Zuordnung: C#-Buch V2.0 14.2 * + * * + * Copyright 2015 by Benjamin Jung * +\*************************************/ +using System; +using System.Windows.Forms; +using Microsoft.Office.Interop.Excel; // zusätzlich ist der Projektverweis auf COM -> Microsoft Excel xx-Objectlibrary + +namespace CSV20.Excel +{ + public partial class Form1 : Form + { + // oExcelApp benötigt den kompletten Namensraum, da andernfalls der Compiler den Unterschied zwischen + // "Microsoft.Office.Interop.Excel.Application" und "System.Windows.Forms.Application" nicht erkennen kann + private Microsoft.Office.Interop.Excel.Application oExcelApp = new Microsoft.Office.Interop.Excel.Application(); + private Workbook oWorkbook = null; + private Worksheet oWorksheet = null; + + public Form1() + { + InitializeComponent(); + } + + private void Form1_Load(object sender, EventArgs e) + { + Range oRange; + string sSpaltenName; + object oValue; + + if (openFileDialogExcel.ShowDialog() == System.Windows.Forms.DialogResult.OK) + { + // Datei öffnen und aktives Blatt und benutzten Bereich auswählen + oWorkbook = oExcelApp.Workbooks.Open(openFileDialogExcel.FileName); + oWorksheet = (Worksheet)oWorkbook.ActiveSheet; + oRange = oWorksheet.UsedRange; + + // Zellen vom Dokument in die Ansicht laden + for (int i = 0; i < oRange.Columns.Count; i++) + { + if (i >= dataGridViewExcel.Columns.Count) + { + // bilden der Spalten-Namen: A, B, C, D, E, ..., A1, B1, C1, ..., A2, B2, ... + sSpaltenName = ((char)('A' + i % 26)).ToString(); + if (Math.Floor(i / 26.0) != 0) + sSpaltenName += Math.Floor(i / 26.0).ToString(); + dataGridViewExcel.Columns.Add(sSpaltenName, sSpaltenName); + } + + for (int j = 0; j < oRange.Rows.Count; j++) + { + // Zeilen werden nur beim 1. Durchlauf hinzugefügt + if (i == 0) + { + dataGridViewExcel.Rows.Add(); + } + oValue = (oRange.Cells[j + 1, i + 1] as Range).Value; + // muss unbedingt abgefangen werden + if (oValue != null) + { + dataGridViewExcel.Rows[j].Cells[i].Value = oValue.ToString(); + } + } + } + + // Fenster minimieren und danach wieder in normale Fenstergröße zurückkehren, + // dies ist ein Trick, sodass wir die Anwendung wieder in den Vordergrund bekommen, + // da Excel unsichtbar geöffnet wird + this.WindowState = FormWindowState.Minimized; + this.WindowState = FormWindowState.Normal; + } + else // Bei Abbruch, Fenster schließen + { + Close(); + } + } + + private void Form1_FormClosing(object sender, FormClosingEventArgs e) + { + // Beim Schließen des Fensters, Dokument schließen + if (oWorkbook != null) + oWorkbook.Close(); + } + + private void buttonSpeichern_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Sind Sie sicher dass Sie die Änderungen übernehmen möchten?\nDie Quelldatei wird überschrieben!", "Speichern?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) + { + // Daten von der Ansicht in das Dokument schreiben + for (int i = 0; i < dataGridViewExcel.Rows.Count - 1; i++) + for (int j = 0; j < dataGridViewExcel.Columns.Count; j++) + if (dataGridViewExcel.Rows[i].Cells[j].Value != null) + (oWorksheet.Cells[i + 1, j + 1] as Range).Value = dataGridViewExcel.Rows[i].Cells[j].Value.ToString(); + else + (oWorksheet.Cells[i + 1, j + 1] as Range).Value = ""; + + // Speichervorgang auslösen + oWorkbook.Save(); + } + } + } +} \ No newline at end of file diff --git a/Excel/Form1.resx b/Excel/Form1.resx new file mode 100644 index 0000000..29d0f4a --- /dev/null +++ b/Excel/Form1.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/Excel/Program.cs b/Excel/Program.cs new file mode 100644 index 0000000..9faaceb --- /dev/null +++ b/Excel/Program.cs @@ -0,0 +1,29 @@ +/*************************************\ + * Excel-Dokumente lesen und * + * bearbeiten * + * * + * Kapitel: Erweiterte * + * Programmierthemen * + * Zuordnung: C#-Buch V2.0 14.2 * + * * + * Copyright 2015 by Benjamin Jung * +\*************************************/ +using System; +using System.Windows.Forms; + +namespace CSV20.Excel +{ + static class Program + { + /// + /// Der Haupteinstiegspunkt für die Anwendung. + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Form1()); + } + } +} \ No newline at end of file diff --git a/Excel/Properties/AssemblyInfo.cs b/Excel/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..b304415 --- /dev/null +++ b/Excel/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die mit einer Assembly verknüpft sind. +[assembly: AssemblyTitle("Excel")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Excel")] +[assembly: AssemblyCopyright("Copyright © 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar +// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von +// COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("5e6b7c01-e7a8-4cf7-b36f-ff6a23ec141e")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern +// übernehmen, indem Sie "*" eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Excel/Properties/Resources.Designer.cs b/Excel/Properties/Resources.Designer.cs new file mode 100644 index 0000000..7bd6840 --- /dev/null +++ b/Excel/Properties/Resources.Designer.cs @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// Dieser Code wurde von einem Tool generiert. +// Laufzeitversion:4.0.30319.34209 +// +// Änderungen an dieser Datei können falsches Verhalten verursachen und gehen verloren, wenn +// der Code erneut generiert wird. +// +//------------------------------------------------------------------------------ + +namespace CSV20.Excel.Properties { + using System; + + + /// + /// Eine stark typisierte Ressourcenklasse zum Suchen von lokalisierten Zeichenfolgen usw. + /// + // Diese Klasse wurde von der StronglyTypedResourceBuilder automatisch generiert + // -Klasse über ein Tool wie ResGen oder Visual Studio automatisch generiert. + // Um einen Member hinzuzufügen oder zu entfernen, bearbeiten Sie die .ResX-Datei und führen dann ResGen + // mit der /str-Option erneut aus, oder Sie erstellen Ihr VS-Projekt neu. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Gibt die zwischengespeicherte ResourceManager-Instanz zurück, die von dieser Klasse verwendet wird. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("CSV20.Excel.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Überschreibt die CurrentUICulture-Eigenschaft des aktuellen Threads für alle + /// Ressourcenzuordnungen, die diese stark typisierte Ressourcenklasse verwenden. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + } +} diff --git a/Excel/Properties/Resources.resx b/Excel/Properties/Resources.resx new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ b/Excel/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Excel/Properties/Settings.Designer.cs b/Excel/Properties/Settings.Designer.cs new file mode 100644 index 0000000..231d67c --- /dev/null +++ b/Excel/Properties/Settings.Designer.cs @@ -0,0 +1,26 @@ +//------------------------------------------------------------------------------ +// +// Dieser Code wurde von einem Tool generiert. +// Laufzeitversion:4.0.30319.34209 +// +// Änderungen an dieser Datei können falsches Verhalten verursachen und gehen verloren, wenn +// der Code erneut generiert wird. +// +//------------------------------------------------------------------------------ + +namespace CSV20.Excel.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "12.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + } +} diff --git a/Excel/Properties/Settings.settings b/Excel/Properties/Settings.settings new file mode 100644 index 0000000..3964565 --- /dev/null +++ b/Excel/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + +