44
jingh
2021-08-07 fae0af9f4ffdcbdea44f99bcbf1625fb239a6231
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
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_DataIn_Material_K3 : Form
    {
        DAL.ClsK3_ErpCls_View oErpCls = new DAL.ClsK3_ErpCls_View();//K3ÎïÁÏÊôÐÔ
        DAL.ClsK3_MaterType_View oMaterType = new DAL.ClsK3_MaterType_View();//K3ÎïÁÏ·ÖÀà
        DAL.ClsK3_Unit_View oUnit = new DAL.ClsK3_Unit_View();//K3¼ÆÁ¿µ¥Î»
        DAL.ClsK3_UnitGroup_View oUnitGroup = new DAL.ClsK3_UnitGroup_View();//K3¼ÆÁ¿µ¥Î»×é
        DAL.ClsK3_Warehouse_View oWh = new DAL.ClsK3_Warehouse_View();//K3²Ö¿â
        DAL.ClsK3_StockPlace_View oSP = new DAL.ClsK3_StockPlace_View();//K3²Öλ
        //
        private string fileName = null; //ÎļþÃû
        private IWorkbook workbook = null;
        private FileStream fs = null;
        private bool disposed;
 
        public Gy_DataIn_Material_K3()
        {
            InitializeComponent();
        }
 
        private void initHeadCaption()
        {
 
        }
 
        private void yl_Click(object sender, EventArgs e)
        {
            WriteClass();
        }
 
        //Êý¾Ýµ¼Èë
        public void WriteClass()
        {
            DAL.ClsK3_Material_Ctl BillNew = new DAL.ClsK3_Material_Ctl();
 
            int sHNumberCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÎïÁÏ´úÂë", grdmain);
            int sHNameCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÎïÁÏÃû³Æ", grdmain);
            int sHHelpCodeCol = DBUtility.Gy_BaseFun.Fun_GetCol("Öú¼ÇÂë", grdmain);
            int sHModelCol = DBUtility.Gy_BaseFun.Fun_GetCol("¹æ¸ñÐͺÅ", grdmain);
            int sHPICNoCol = DBUtility.Gy_BaseFun.Fun_GetCol("ͼºÅ", grdmain);
            int sHMaterTypeNumberCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÎïÁÏÊôÐÔ", grdmain);
            int sHMaterClsNumberCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÎïÁÏ·ÖÀà", grdmain);
            int sHUnitNumberCol = DBUtility.Gy_BaseFun.Fun_GetCol("¼ÆÁ¿µ¥Î»", grdmain);
            int sHUnitGroupNumberCol = DBUtility.Gy_BaseFun.Fun_GetCol("¼ÆÁ¿µ¥Î»×é", grdmain);
            int sHSecUnitNumberCol = DBUtility.Gy_BaseFun.Fun_GetCol("¸¨Öú¼ÆÁ¿µ¥Î»", grdmain);
            int sHWhNumberCol = DBUtility.Gy_BaseFun.Fun_GetCol("ĬÈϲֿâ", grdmain);
            int sHSPNumberCol = DBUtility.Gy_BaseFun.Fun_GetCol("ĬÈϲÖλ", grdmain);
            int sHQtyDecimalCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÊýÁ¿¾«¶È", grdmain);
            int sHLowStockCol = DBUtility.Gy_BaseFun.Fun_GetCol("×îµÍ´æÁ¿", grdmain);
            int sHHighStockCol = DBUtility.Gy_BaseFun.Fun_GetCol("×î¸ß´æÁ¿", grdmain);
            int sHSafeStockCol = DBUtility.Gy_BaseFun.Fun_GetCol("°²È«¿â´æÊýÁ¿", grdmain);
            int sHUseFlagCol = DBUtility.Gy_BaseFun.Fun_GetCol("ʹÓÃ״̬", grdmain);
            int sHSourceCol = DBUtility.Gy_BaseFun.Fun_GetCol("À´Ô´", grdmain);
            int sHOrderPriceCol = DBUtility.Gy_BaseFun.Fun_GetCol("²É¹ºµ¥¼Û", grdmain);
            
            for (int i = 0; i <= grdmain.Rows.Count - 1; i++)
            {
                if (oErpCls.GetInfoByNumber(DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHMaterTypeNumberCol].Value)))
                {
                    BillNew.oModel.HMaterTypeID = oErpCls.omodel.HItemID;
                }
                else
                {
                    BillNew.oModel.HMaterTypeID = 0;
                }
                //
                if (oMaterType.GetInfoByNumber(DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHMaterClsNumberCol].Value)))
                {
                    BillNew.oModel.HMaterClsID = oMaterType.omodel.HItemID;
                }
                else
                {
                    BillNew.oModel.HMaterClsID = 0;
                }
                //
                if (oUnit.GetInfoByNumber(DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHUnitNumberCol].Value)))
                {
                    BillNew.oModel.HUnitID = oUnit.omodel.HItemID;
                }
                else
                {
                    BillNew.oModel.HUnitID = 0;
                }
                //
                if (oUnitGroup.GetInfoByNumber(DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHUnitGroupNumberCol].Value)))
                {
                    BillNew.oModel.HUnitGroupID = oUnitGroup.omodel.HItemID;
                }
                else
                {
                    BillNew.oModel.HUnitGroupID = 0;
                }
                //
                if (oUnit.GetInfoByNumber(DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHSecUnitNumberCol].Value)))
                {
                    BillNew.oModel.HSecUnitID = oUnit.omodel.HItemID;
                }
                else
                {
                    BillNew.oModel.HSecUnitID = 0;
                }
                //
                if (oWh.GetInfoByNumber(DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHWhNumberCol].Value)))
                {
                    BillNew.oModel.HWhID = oWh.omodel.HItemID;
                }
                else
                {
                    BillNew.oModel.HWhID = 0;
                }
                //
                if (oSP.GetInfoByNumber(DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHSPNumberCol].Value)))
                {
                    BillNew.oModel.HSPID = oSP.omodel.HItemID;
                }
                else
                {
                    BillNew.oModel.HSPID = 0;
                }
                //¸ù¾Ý´úÂë»ñÈ¡Éϼ¶×é´úÂë¼°ÄÚÂë
                Int64 iParentID = 0;
                string sParent = DBUtility.ClsPub.GetParentCode(DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHNumberCol].Value));
                if (sParent.Trim() == "")
                {
                    BillNew.oModel.HParentID  = 0;
                }
                else
                {
                    if (BillNew.HavParentCode(sParent.Trim(), ref iParentID))
                    {
                        BillNew.oModel.HParentID = iParentID;
                    }
                    else
                    {
                        MessageBox.Show("Éϼ¶´úÂë²»´æÔÚ»ò±»½ûÓã¡", "Ìáʾ");
                        return ;
                    }
                }
                //
                BillNew.oModel.HNumber = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHNumberCol].Value);
                BillNew.oModel.HEndFlag = false;
                BillNew.oModel.HShortNumber = DBUtility.ClsPub.GetShortNumber(DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHNumberCol].Value));
                BillNew.oModel.HLevel = DBUtility.ClsPub.GetLevel(DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHNumberCol].Value));
                BillNew.oModel.HName = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHNameCol].Value);
                BillNew.oModel.HHelpCode = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHHelpCodeCol].Value);
                BillNew.oModel.HModel = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHModelCol].Value);
                BillNew.oModel.HPICNo = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHPICNoCol].Value);
                BillNew.oModel.HQtyDecimal = DBUtility.ClsPub.isLong(grdmain.Rows[i].Cells[sHQtyDecimalCol].Value);
                BillNew.oModel.HLowStock = DBUtility.ClsPub.isDoule(grdmain.Rows[i].Cells[sHLowStockCol].Value);
                BillNew.oModel.HHighStock = DBUtility.ClsPub.isDoule(grdmain.Rows[i].Cells[sHHighStockCol].Value);
                BillNew.oModel.HSafeStock = DBUtility.ClsPub.isDoule(grdmain.Rows[i].Cells[sHSafeStockCol].Value);
                BillNew.oModel.HUseFlag = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHUseFlagCol].Value);
                BillNew.oModel.HSource = DBUtility.ClsPub.isLong(grdmain.Rows[i].Cells[sHSourceCol].Value);
                BillNew.oModel.HOrderPrice = DBUtility.ClsPub.isDoule(grdmain.Rows[i].Cells[sHOrderPriceCol].Value);
                BillNew.oModel.HRemark = "EXCELµ¼Èë";
                BillNew.AddNew_K3();
            }
            MessageBox.Show("±£´æÍê±Ï£¡");
        }
 
        //дÈëÀ࠴ӿؼþ
        private bool Mater_WriteClass(int row)
        {
            //
            
            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() == "")
            {
                txtHRemark.Text = "1";
                //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 = "";
            }
        }
 
        private Int32 Fun_GetCol(string sCol)
        {
            return DBUtility.Xt_BaseBillFun.Fun_GetCol(sCol, grdmain);
        }
 
 
    }
}