/
Form1.cs
231 lines (190 loc) · 7.2 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data;
using System.IO;
namespace InventurUpdater
{
public partial class frmInventurUpdater : Form
{
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
string pathNr = System.AppContext.BaseDirectory + "STI-Inventur-ArtikelNr_Datenbank.xml";
System.Data.DataTable dtNr;
DataSet dsNr;
public frmInventurUpdater()
{
InitializeComponent();
}
private void frmInventurUpdater_Load(object sender, EventArgs e)
{
openFileDialogExcel.InitialDirectory = System.AppContext.BaseDirectory;
Open_NrDB();
}
private void Open_NrDB()
{
try
{
dsNr = new DataSet("Root");
if (File.Exists(pathNr))
{
dsNr.ReadXml(pathNr, XmlReadMode.ReadSchema);
dtNr = dsNr.Tables[0];
}
else
{
dtNr = new System.Data.DataTable("dt");
dtNr.Columns.Add("ArtikelNr", typeof(string));
dtNr.Columns.Add("LfdNr", typeof(int));
dsNr.Tables.Add(dtNr);
}
grdNr.DataSource = dtNr;
grdNr.Columns["ArtikelNr"].Width = 203;
grdNr.Columns["LfdNr"].Width = 80;
}
catch (Exception theException)
{
string errorMessage = String.Concat(theException.Message, theException.Source);
MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Save_NrDB()
{
try
{
if (dsNr != null)
dsNr.WriteXml(pathNr, XmlWriteMode.WriteSchema);
}
catch (Exception theException)
{
string errorMessage = String.Concat(theException.Message, theException.Source);
MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButOpenInvList_Click(object sender, EventArgs e)
{
try
{
if (openFileDialogExcel.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
edInvListe.Text = openFileDialogExcel.FileName;
//Start Excel and get Application object.
oXL = new Excel.Application();
oXL.Visible = true;
// Datei öffnen und aktives Blatt und benutzten Bereich auswählen
oWB = oXL.Workbooks.Open(openFileDialogExcel.FileName);
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
}
}
catch (Exception theException)
{
string errorMessage = String.Concat(theException.Message, theException.Source);
MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private Int32 getLfdNr(string ArtNr)
{
Int32 LfdNr = 0;
try
{
// ArtikelNr suchen
DataRow[] rows = dtNr.Select("ArtikelNr='" + ArtNr + "'");
// ArtikelNr existiert in der Liste: dessen laufende Nummer zurückgeben.
if (rows.Length > 0)
{
LfdNr = (Int32)rows[0][1];
}
// Artikelnummer fehlt in der Liste: neuen Artikel einfügen mit der nächste freien laufenden Nummer.
else
{
// Höchste laufende Nummer ermitteln
int maxLfdNr = 0;
if (dtNr.Rows.Count > 0)
{
maxLfdNr = (int)dtNr.Compute("MAX([LfdNr])", "");
}
LfdNr = maxLfdNr + 1;
// Neuen Eintrag einfügen
DataRow newRow = dtNr.NewRow();
newRow[0] = ArtNr;
newRow[1] = LfdNr;
dtNr.Rows.Add(newRow);
}
return LfdNr;
}
catch (Exception theException)
{
string errorMessage = String.Concat(theException.Message, theException.Source);
MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return LfdNr;
}
}
private void butInvListSave_Click(object sender, EventArgs e)
{
try
{
if (oSheet != null)
{
oXL.ActiveWorkbook.Save();
oXL.ActiveWorkbook.Close(true);
oSheet = null;
oXL.Quit();
}
}
catch (Exception theException)
{
string errorMessage = String.Concat(theException.Message, theException.Source);
MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void butAssignNum_Click(object sender, EventArgs e)
{
string artNr;
Int32 lfdNr;
try
{
if (oSheet == null)
{
MessageBox.Show("Bitte vorher eine Inventurliste öffnen!", "Hinweis", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
Cursor = Cursors.WaitCursor;
int zeileVon = int.Parse(edStartZeile.Text);
int zeileBis = oSheet.UsedRange.Rows.Count;
progressBar.Minimum = zeileVon;
progressBar.Maximum = zeileBis;
progressBar.Value = zeileVon;
progressBar.Visible = true;
int spalteArtNr = int.Parse(edArtSpalte.Text);
int spalteLfdNr = int.Parse(edNrSpalte.Text);
oSheet.Cells[8, 3].Value = "Laufende Nr";
for (int i = zeileVon; i <= zeileBis; i++)
{
artNr = oSheet.Cells[i, spalteArtNr].Value;
if ((artNr != null) & (artNr != ""))
{
lfdNr = getLfdNr(artNr);
oSheet.Cells[i, spalteLfdNr].Value = lfdNr;
}
progressBar.PerformStep();
}
Save_NrDB();
progressBar.Visible = false;
}
}
catch (Exception theException)
{
Cursor = Cursors.Default;
string errorMessage = String.Concat(theException.Message, theException.Source);
MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
Cursor = Cursors.Default;
progressBar.Visible = false;
}
}
}
}