1
duhe
2024-07-29 e486bdcca9a38e52874c231eb1a36731d747578f
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms; 
using System.Data.OleDb;
using System.Data.Sql;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
 
namespace BaseSet
{
    public partial class Gy_DataInTmp_Customer : Form
    {
        DAL.ClsGy_Customer_Ctl oCus = new DAL.ClsGy_Customer_Ctl();
        DAL.ClsGy_Customer_View oCusHlp = new DAL.ClsGy_Customer_View();
        DAL.ClsGy_SettleStyle_View oSS = new DAL.ClsGy_SettleStyle_View();
        DAL.ClsGy_Currency_View oCur = new DAL.ClsGy_Currency_View();
        //
        private string fileName = null; //ÎļþÃû
        private IWorkbook workbook = null;
        private FileStream fs = null;
        private bool disposed;
 
        string sHavSame = "";
        //
        int sHNumberCol = 0;
        int sNameCol = 0;
        int sHShortNameCol = 0;
        int sHAddressCol = 0;
        int sHLinkManCol = 0;
        int sHLinkPhoneCol = 0;
        int sHFaxCol = 0;
        int sHPostalCodeCol = 0;
        int sHEmailCol = 0;
        int sHBankCol = 0;
        int sHBankAccountCol = 0;
        int sHTaxNumCol = 0;
        int sHTaxRateCol = 0;
        int sHCountryCol = 0;
        int sHCorManCol = 0;
        int sHCurIDCol = 0;
        int sHSSIDCol = 0;
        int sHEmpIDCol = 0;
 
        public Gy_DataInTmp_Customer()
        {
            InitializeComponent();
        }
 
 
        private void initHeadCaption()
        {
 
        }
 
        //
        private void yl_Click(object sender, EventArgs e)
        {
            //WriteClass_ParentCode();
            WriteClass();
        }
 
        //Êý¾Ýµ¼Èë
        public void WriteClass_ParentCode()
        {
            //SQLHelper.ClsCN oCn = new SQLHelper.ClsCN();
            //string sColName;
            //string sFieldName;
            //int sNameCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÎïÁÏÃû³Æ", grdmain);
            //for (int i = 0; i <= grdmain.Rows.Count - 1; i++)
            //{
            //    if (DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sNameCol].Value) != "")
            //    {
            //        if (ParentCode_WriteClass(i))
            //        {
            //            oCus.AddNew();
            //        }
            //    }
            //}
        }
 
        ////дÈëÀ࠴ӿؼþ
        //private bool ParentCode_WriteClass(int row)
        //{
        //    //
        //    string sHNumber = "";
        //    long HItemID = 0;
        //    int sLogoCol = DBUtility.Gy_BaseFun.Fun_GetCol("Æ·ÅÆ", grdmain);
        //    int sCurCol = DBUtility.Gy_BaseFun.Fun_GetCol("±Ò±ð", grdmain);
        //    int sCountryCol = DBUtility.Gy_BaseFun.Fun_GetCol("²úµØ", grdmain);
        //    //
        //    sHNumber = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sLogoCol].Value);
 
        //    //ÊÇ·ñÖØ¸´´úÂë
        //    if (oCus.HavSameNumber(HItemID, sHNumber))
        //    {
        //        return false;
        //    }
 
        //    //дÈëÐÅÏ¢
        //    oCus.oModel.HShortNumber = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sLogoCol].Value);
        //    oCus.oModel.HEndFlag = false;
        //    oCus.oModel.HLevel = 1;
        //    oCus.oModel.HNumber = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sLogoCol].Value);
        //    oCus.oModel.HHelpCode = "";
        //    oCus.oModel.HName = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sLogoCol].Value);
        //    oCus.oModel.HModel = "";
        //    oCus.oModel.HStopflag = false;
        //    oCus.oModel.HRemark = "";
        //    //if (oCur.GetInfoByName(DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sCurCol].Value)))
        //    //{
        //    //    oCus.oModel.HCurID = oCur.omodel.HItemID;
        //    //}
        //    //else
        //    //{
        //    //    oCus.oModel.HCurID = 0;
        //    //}
        //    oCus.oModel.HUnitID = 7;
        //    oCus.oModel.HSalePrice = 0;
 
        //    oCus.oModel.HSubjoin = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sLogoCol].Value);
        //    oCus.oModel.HSubjoin2 = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sCountryCol].Value);
        //    //oCus.oModel.HCountry = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sCountryCol].Value);
 
        //    //
        //    oCus.oModel.HMaterClsID = 0;
        //    oCus.oModel.HMaterTypeID = 0;
        //    oCus.oModel.HUnitGroupID = 0;
        //    oCus.oModel.HRoutingID = 0;
        //    oCus.oModel.HBomID = 0;
        //    oCus.oModel.HSecUnitID = 0;
        //    oCus.oModel.HSecUnitRate = 0;
        //    oCus.oModel.HHighStock = 0;
        //    oCus.oModel.HLowStock = 0;
        //    oCus.oModel.HSafeStock = 0;
        //    oCus.oModel.HOrderPrice = 0;
        //    oCus.oModel.HKeepDays = 0;
        //    oCus.oModel.HPlanPrice = 0;
        //    oCus.oModel.HstdPrice = 0;
        //    oCus.oModel.HQtyMin = 0;
        //    oCus.oModel.HQtyMax = 0;
        //    oCus.oModel.HVersion = "";
        //    oCus.oModel.HEngName = "";
        //    oCus.oModel.HEngModel = "";
        //    oCus.oModel.HPropertyTypeID = 0;
        //    oCus.oModel.HSPGroupID = 0;
        //    oCus.oModel.HSPID = 0;
        //    return true;
        //}
 
        //Êý¾Ýµ¼Èë
        public void WriteClass()
        {
            sHNumberCol = DBUtility.Gy_BaseFun.Fun_GetCol("¿Í»§´úÂë", grdmain);
            sNameCol = DBUtility.Gy_BaseFun.Fun_GetCol("¿Í»§Ãû³Æ", grdmain);
            sHShortNameCol = DBUtility.Gy_BaseFun.Fun_GetCol("¿Í»§¼ò³Æ", grdmain);
            sHAddressCol = DBUtility.Gy_BaseFun.Fun_GetCol("µØÖ·", grdmain);
            sHLinkManCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÁªÏµÈË", grdmain);
            sHLinkPhoneCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÁªÏµµç»°", grdmain);
            sHFaxCol = DBUtility.Gy_BaseFun.Fun_GetCol("´«Õæ", grdmain);
            sHPostalCodeCol = DBUtility.Gy_BaseFun.Fun_GetCol("Óʱà", grdmain);
            sHEmailCol = DBUtility.Gy_BaseFun.Fun_GetCol("µç×ÓÓÊÏä", grdmain);
            sHBankCol = DBUtility.Gy_BaseFun.Fun_GetCol("¿ª»§ÒøÐÐ", grdmain);
            sHBankAccountCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÒøÐÐÕ˺Å", grdmain);
            sHTaxNumCol = DBUtility.Gy_BaseFun.Fun_GetCol("˰ÎñµÇ¼ÇºÅ", grdmain);
            sHTaxRateCol = DBUtility.Gy_BaseFun.Fun_GetCol("Ôöֵ˰ÂÊ", grdmain);
            sHCountryCol = DBUtility.Gy_BaseFun.Fun_GetCol("¹ú¼Ò", grdmain);
            sHCorManCol = DBUtility.Gy_BaseFun.Fun_GetCol("·¨ÈË´ú±í", grdmain);
            sHCurIDCol = DBUtility.Gy_BaseFun.Fun_GetCol("ĬÈÏ»õ±Ò", grdmain);
            sHSSIDCol = DBUtility.Gy_BaseFun.Fun_GetCol("½áË㷽ʽ", grdmain);
            sHEmpIDCol = DBUtility.Gy_BaseFun.Fun_GetCol("ËùÊôÒµÎñÔ±", grdmain);
 
            //
            if (sHNumberCol == -1)
            {
                MessageBox.Show("ûÓÐÕÒµ½¡¾¿Í»§´úÂë¡¿µÄ±êÌâ", "Ìáʾ");
                return;
            }
            if (sNameCol == -1)
            {
                MessageBox.Show("ûÓÐÕÒµ½¡¾¿Í»§Ãû³Æ¡¿µÄ±êÌâ", "Ìáʾ");
                return;
            }
            sHavSame = "";
            int sNumberCol = DBUtility.Gy_BaseFun.Fun_GetCol("¿Í»§´úÂë", grdmain);
            for (int i = 0; i <= grdmain.Rows.Count - 1; i++)
            {
                if (DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sNumberCol].Value) != "")
                {
                    if (Mater_WriteClass(i))
                    {
                        oCus.AddNew();
                    }
                }
            }
            if (sHavSame != "")
            {
                MessageBox.Show("´úÂëÖØ¸´£º\r\n" + sHavSame);
            }
            MessageBox.Show("µ¼ÈëÍê±Ï");
            grdmain.DataSource = null;
        }
 
        //дÈëÀ࠴ӿؼþ
        private bool Mater_WriteClass(int row)
        {
            //
            string sHNumber = "";
            long HItemID = 0;
            //
            sHNumber = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHNumberCol].Value);
            //ÅжÏÊÇ·ñÔÊÐí ±£´æ
            //ÉóºË´úÂëÊÇ·ñºÏÀí
            if (!DBUtility.ClsPub.AllowNumber(sHNumber))
            {
                MessageBox.Show("´úÂëÖв»ÄܳöÏÖÁ¬Ðø¡®.¡¯²¢ÇÒÊ×λĩλ²»ÄÜΪ¡®.¡¯£¡", "Ìáʾ");
                return false;
            }
 
            //ÊÇ·ñÖØ¸´´úÂë
            if (oCus.HavSameNumber(HItemID, sHNumber))
            {
                sHavSame = sHavSame + sHNumber + "\r\n";
                return false;
            }
            //¼ì²é¸¸¼¶ÊÇ·ñ´æÔÚ
            string sParent;
            sParent = DBUtility.ClsPub.GetParentCode(sHNumber);
            if (sParent.Trim() == "")
            {
                oCus.oModel.HParentID = 0;
            }
            else
            {
                if (oCus.HavParentCode(sParent.Trim(), HItemID))
                {
                    oCus.oModel.HParentID = oCus.oModel.HItemID;
                }
                else
                {
                    MessageBox.Show("Éϼ¶´úÂë²»´æÔÚ»ò±»½ûÓã¡", "Ìáʾ");
                    return false;
                }
            }
            //µÃµ½¶Ì´úÂë
            string sShortNumber;
            sShortNumber = DBUtility.ClsPub.GetShortNumber(sHNumber);
            if (sShortNumber.Trim() == "")
            {
                return false;
            }
            //±£´æÔ­´úÂë
            if (oCusHlp.GetInfoByID(HItemID))
            {
                oCus.HOldNumber = oCusHlp.omodel.HNumber;
                //ÊÇ·ñдúÂëÊÇ×Ô¼º×ÓÏîÄ¿µÄ¡¡×ÓÏîÄ¿
                if (sParent.Length >= oCus.HOldNumber.Length)
                {
                    if (sParent.Substring(0, oCus.HOldNumber.ToString().Length) == oCus.HOldNumber.Trim())
                    {
                        MessageBox.Show("дúÂë²»ÄÜÊÇ×Ô¼ºµÄϼ¶µÄ×ÓÏîÄ¿£¡", "Ìáʾ");
                        return false;
                    }
                }
            }
            else
            {
                oCus.HOldNumber = "";
            }
            //дÈëÐÅÏ¢
            oCus.oModel.HShortNumber = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHShortNameCol].Value);
            oCus.oModel.HEndFlag = true;
            oCus.oModel.HLevel = DBUtility.ClsPub.GetLevel(sHNumber);
            oCus.oModel.HNumber = sHNumber;
            oCus.oModel.HHelpCode = sShortNumber;
            oCus.oModel.HName = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sNameCol].Value);
            oCus.oModel.HStopflag = false;
            oCus.oModel.HRemark = "";
 
            oCus.oModel.HAddress = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHAddressCol].Value);
            oCus.oModel.HLinkMan = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHLinkManCol].Value);
            oCus.oModel.HLinkPhone = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHLinkPhoneCol].Value);
            oCus.oModel.HMobilePhone = "";
            oCus.oModel.HFax = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHFaxCol].Value);
            oCus.oModel.HPostalCode = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHPostalCodeCol].Value);
            oCus.oModel.HEmail = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHEmailCol].Value);
            oCus.oModel.HBank = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHBankCol].Value);
            oCus.oModel.HBankAccount = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHBankAccountCol].Value);
            oCus.oModel.HTaxNum = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHTaxNumCol].Value);
            oCus.oModel.HTaxRate = DBUtility.ClsPub.isSingle(grdmain.Rows[row].Cells[sHTaxRateCol].Value);
            oCus.oModel.HCountry = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHCountryCol].Value);
            oCus.oModel.HCorMan = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHCorManCol].Value);
            oCus.oModel.HEnglishName = "";
            oCus.oModel.HEnglishAddress = "";
            if (oSS.GetInfoByName(DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHSSIDCol].Value)))
            {
                oCus.oModel.HSSID = oSS.omodel.HItemID;
            }
            else
            {
                oCus.oModel.HSSID = 0;
            }
            if (oCur.GetInfoByName(DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sHCurIDCol].Value)))
            {
                oCus.oModel.HCurID = oCur.omodel.HItemID;
            }
            else
            {
                oCus.oModel.HCurID = 0;
            }
            oCus.oModel.HAreaID = 0;
            oCus.oModel.HCusStatus = "";
            oCus.oModel.HCusTypeID = 0;
            return true;
        }
 
        private void tc_Click(object sender, EventArgs e)
        {
            this.Close();
        }
 
        private void K3_OutMachFeeDataIn_Load(object sender, EventArgs e)
        {
            initHeadCaption();
        }
        public string ExcelPath;
        
 
        /// <summary>
        /// ½«excelÖеÄÊý¾Ýµ¼Èëµ½DataTableÖÐ
        /// </summary>
        /// <param name="fileName">excel·¾¶</param>
        /// <param name="sheetName">excel¹¤×÷±¡sheetµÄÃû³Æ</param>
        /// <param name="isFirstRowColumn">µÚÒ»ÐÐÊÇ·ñÊÇDataTableµÄÁÐÃû</param>
        /// <returns>·µ»ØµÄDataTable</returns>
        public DataTable ExcelToDataTable(string fileName, string sheetName, bool isFirstRowColumn)
        {
            ISheet sheet = null;
            DataTable data = new DataTable();
            int startRow = 0;
            try
            {
                fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                if (fileName.IndexOf(".xlsx") > 0) // 2007°æ±¾
                    workbook = new XSSFWorkbook(fs);
                else if (fileName.IndexOf(".xls") > 0) // 2003°æ±¾
                    workbook = new HSSFWorkbook(fs);
 
                if (sheetName != null)
                {
                    sheet = workbook.GetSheet(sheetName);
                    if (sheet == null) //Èç¹ûûÓÐÕÒµ½Ö¸¶¨µÄsheetName¶ÔÓ¦µÄsheet£¬Ôò³¢ÊÔ»ñÈ¡µÚÒ»¸ösheet
                    {
                        sheet = workbook.GetSheetAt(0);
                    }
                }
                else
                {
                    sheet = workbook.GetSheetAt(0);
                }
                if (sheet != null)
                {
                    IRow firstRow = sheet.GetRow(0);
                    int cellCount = firstRow.LastCellNum; //Ò»ÐÐ×îºóÒ»¸öcellµÄ±àºÅ ¼´×ܵÄÁÐÊý
 
                    if (isFirstRowColumn)
                    {
                        for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                        {
                            ICell cell = firstRow.GetCell(i);
                            if (cell != null)
                            {
                                string cellValue = cell.StringCellValue;
                                if (cellValue != null)
                                {
                                    DataColumn column = new DataColumn(cellValue);
                                    data.Columns.Add(column);
                                }
                            }
                        }
                        startRow = sheet.FirstRowNum + 1;
                    }
                    else
                    {
                        startRow = sheet.FirstRowNum;
                    }
 
                    //×îºóÒ»ÁеıêºÅ
                    int rowCount = sheet.LastRowNum;
                    for (int i = startRow; i <= rowCount; ++i)
                    {
                        IRow row = sheet.GetRow(i);
                        if (row == null) continue; //ûÓÐÊý¾ÝµÄÐÐĬÈÏÊÇnull¡¡¡¡¡¡¡¡¡¡¡¡¡¡
 
                        DataRow dataRow = data.NewRow();
                        for (int j = row.FirstCellNum; j < cellCount; ++j)
                        {
                            if (row.GetCell(j) != null) //ͬÀí£¬Ã»ÓÐÊý¾ÝµÄµ¥Ôª¸ñ¶¼Ä¬ÈÏÊÇnull
                                dataRow[j] = row.GetCell(j).ToString();
                        }
                        data.Rows.Add(dataRow);
                    }
                }
 
                return data;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
                return null;
            }
        }
 
        DataTable d;
        private void button1_Click_1(object sender, EventArgs e)
        {
            if (txtHRemark.Text.Trim() == "")
            {
                MessageBox.Show("ÇëÊäÈëÐèÒªµ¼ÈëµÄ¹¤×÷±¡Ãû³Æ£¡");
                return;
            }
            OpenExcelFile.Title = "Open Excel File  ";
            OpenExcelFile.FileName = "";
            OpenExcelFile.Filter = "Microsoft Office Excel Files(*.xls)|*.xls";
            OpenExcelFile.ShowDialog();
            if (OpenExcelFile.FileName != "")
            {
                ExcelPath = OpenExcelFile.FileName;
                d = ExcelToDataTable(ExcelPath, txtHRemark.Text.Trim(), true);
                grdmain.DataSource = d.DefaultView;
            }
            else
            {
                ExcelPath = "";
            }
        }
 
    }
}