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_Material : Form { ClsGy_DataInTmp_Material oTmp = new ClsGy_DataInTmp_Material(); DAL.ClsGy_Material_Ctl oMater = new DAL.ClsGy_Material_Ctl(); DAL.ClsGy_Material_View oMaterHlp = new DAL.ClsGy_Material_View(); DAL.ClsGy_Unit_View oUnit = new DAL.ClsGy_Unit_View(); DAL.ClsGy_Currency_View oCur = new DAL.ClsGy_Currency_View(); DAL.ClsGy_Warehouse_View oWh = new DAL.ClsGy_Warehouse_View(); DAL.ClsGy_StockPlace_View oSP = new DAL.ClsGy_StockPlace_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 sModelCol = 0; int sUnitCol = 0; int sMaterTypeCol = 0; int sWhCol = 0; int sSPCol = 0; int sPicNoCol = 0; int sColorCol = 0; int sMinQtyCol = 0; int sMaxQtyCol = 0; int sSafeQtyCol = 0; int sPOPriceCol = 0; int sSEPriceCol = 0; int sPlanPriceCol = 0; int sCBPriceCol = 0; int sMinSelQtyCol = 0; int sMaxSelQtyCol = 0; public Gy_DataInTmp_Material() { 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)) { oMater.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 (oMater.HavSameNumber(HItemID, sHNumber)) { return false; } //дÈëÐÅÏ¢ oMater.oModel.HShortNumber = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sLogoCol].Value); oMater.oModel.HEndFlag = false; oMater.oModel.HLevel = 1; oMater.oModel.HNumber = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sLogoCol].Value); oMater.oModel.HHelpCode = ""; oMater.oModel.HName = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sLogoCol].Value); oMater.oModel.HModel = ""; oMater.oModel.HStopflag = false; oMater.oModel.HRemark = ""; //if (oCur.GetInfoByName(DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sCurCol].Value))) //{ // oMater.oModel.HCurID = oCur.omodel.HItemID; //} //else //{ // oMater.oModel.HCurID = 0; //} oMater.oModel.HUnitID = 7; oMater.oModel.HSalePrice = 0; oMater.oModel.HSubjoin = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sLogoCol].Value); oMater.oModel.HSubjoin2 = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sCountryCol].Value); //oMater.oModel.HCountry = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sCountryCol].Value); // oMater.oModel.HMaterClsID = 0; oMater.oModel.HMaterTypeID = 0; oMater.oModel.HUnitGroupID = 0; oMater.oModel.HRoutingID = 0; oMater.oModel.HBomID = 0; oMater.oModel.HSecUnitID = 0; oMater.oModel.HSecUnitRate = 0; oMater.oModel.HHighStock = 0; oMater.oModel.HLowStock = 0; oMater.oModel.HSafeStock = 0; oMater.oModel.HOrderPrice = 0; oMater.oModel.HKeepDays = 0; oMater.oModel.HPlanPrice = 0; oMater.oModel.HstdPrice = 0; oMater.oModel.HQtyMin = 0; oMater.oModel.HQtyMax = 0; oMater.oModel.HVersion = ""; oMater.oModel.HEngName = ""; oMater.oModel.HEngModel = ""; oMater.oModel.HPropertyTypeID = 0; oMater.oModel.HSPGroupID = 0; oMater.oModel.HSPID = 0; return true; } //Êý¾Ýµ¼Èë public void WriteClass() { sHNumberCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÎïÁÏ´úÂë", grdmain); sNameCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÎïÁÏÃû³Æ", grdmain); sModelCol = DBUtility.Gy_BaseFun.Fun_GetCol("¹æ¸ñÐͺÅ", grdmain); sUnitCol = DBUtility.Gy_BaseFun.Fun_GetCol("¼ÆÁ¿µ¥Î»", grdmain); sMaterTypeCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÎïÁÏÊôÐÔ", grdmain); sWhCol = DBUtility.Gy_BaseFun.Fun_GetCol("ĬÈϲֿâ", grdmain); sSPCol = DBUtility.Gy_BaseFun.Fun_GetCol("ĬÈϲÖλ", grdmain); sPicNoCol = DBUtility.Gy_BaseFun.Fun_GetCol("ͼºÅ", grdmain); sColorCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÑÕÉ«", grdmain); sMinQtyCol = DBUtility.Gy_BaseFun.Fun_GetCol("×îµÍ´æÁ¿", grdmain); sMaxQtyCol = DBUtility.Gy_BaseFun.Fun_GetCol("×î¸ß´æÁ¿", grdmain); sSafeQtyCol = DBUtility.Gy_BaseFun.Fun_GetCol("°²È«¿â´æ", grdmain); sPOPriceCol = DBUtility.Gy_BaseFun.Fun_GetCol("²É¹ºµ¥¼Û", grdmain); sSEPriceCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÏúÊÛµ¥¼Û", grdmain); sPlanPriceCol = DBUtility.Gy_BaseFun.Fun_GetCol("¼Æ»®µ¥¼Û", grdmain); sCBPriceCol = DBUtility.Gy_BaseFun.Fun_GetCol("±ê×¼³É±¾", grdmain); sMinSelQtyCol = DBUtility.Gy_BaseFun.Fun_GetCol("×îС¶©»õÁ¿", grdmain); sMaxSelQtyCol = DBUtility.Gy_BaseFun.Fun_GetCol("×î´ó¶©»õÁ¿", grdmain); // if (sHNumberCol == -1) { MessageBox.Show("ûÓÐÕÒµ½¡¾ÎïÁÏ´úÂë¡¿µÄ±êÌâ", "Ìáʾ"); return; } if (sNameCol == -1) { MessageBox.Show("ûÓÐÕÒµ½¡¾ÎïÁÏÃû³Æ¡¿µÄ±êÌâ", "Ìáʾ"); return; } if (sModelCol == -1) { MessageBox.Show("ûÓÐÕÒµ½¡¾¹æ¸ñÐͺš¿µÄ±êÌâ", "Ìáʾ"); return; } if (sUnitCol == -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)) { oMater.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 (oMater.HavSameNumber(HItemID, sHNumber)) { sHavSame = sHavSame + sHNumber + "\r\n"; return false; } //¼ì²é¸¸¼¶ÊÇ·ñ´æÔÚ string sParent; sParent = DBUtility.ClsPub.GetParentCode(sHNumber); if (sParent.Trim() == "") { oMater.oModel.HParentID = 0; } else { if (oMater.HavParentCode(sParent.Trim(), HItemID)) { oMater.oModel.HParentID = oMater.oModel.HItemID; } else { MessageBox.Show("Éϼ¶´úÂë²»´æÔÚ»ò±»½ûÓã¡", "Ìáʾ"); return false; } } //µÃµ½¶Ì´úÂë string sShortNumber; sShortNumber = DBUtility.ClsPub.GetShortNumber(sHNumber); if (sShortNumber.Trim() == "") { return false; } //±£´æÔ­´úÂë if (oMaterHlp.GetInfoByID(HItemID)) { oMater.HOldNumber = oMaterHlp.omodel.HNumber; //ÊÇ·ñдúÂëÊÇ×Ô¼º×ÓÏîÄ¿µÄ¡¡×ÓÏîÄ¿ if (sParent.Length >= oMater.HOldNumber.Length) { if (sParent.Substring(0, oMater.HOldNumber.ToString().Length) == oMater.HOldNumber.Trim()) { MessageBox.Show("дúÂë²»ÄÜÊÇ×Ô¼ºµÄϼ¶µÄ×ÓÏîÄ¿£¡", "Ìáʾ"); return false; } } } else { oMater.HOldNumber = ""; } //дÈëÐÅÏ¢ oMater.oModel.HShortNumber = sShortNumber; oMater.oModel.HEndFlag = true; oMater.oModel.HLevel = DBUtility.ClsPub.GetLevel(sHNumber); oMater.oModel.HNumber = sHNumber; oMater.oModel.HHelpCode = sShortNumber; oMater.oModel.HName = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sNameCol].Value); oMater.oModel.HModel = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sModelCol].Value); oMater.oModel.HStopflag = false; oMater.oModel.HRemark = ""; //if (oCur.GetInfoByName(DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sCurCol].Value))) //{ // oMater.oModel.HCurID = oCur.omodel.HItemID; //} //else //{ // oMater.oModel.HCurID = 0; //} if (oUnit.GetInfoByName(DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sUnitCol].Value))) { oMater.oModel.HUnitID = oUnit.omodel.HItemID; } else { oMater.oModel.HUnitID = 0; } //oMater.oModel.HSubjoin = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sLogoCol].Value); //oMater.oModel.HSubjoin2 = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sCountryCol].Value); //oMater.oModel.HCountry = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sCountryCol].Value); // oMater.oModel.HMaterClsID = 0; if (DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sMaterTypeCol].Value) == "Í⹺") { oMater.oModel.HMaterTypeID = 1; } else if (DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sMaterTypeCol].Value) == "×ÔÖÆ") { oMater.oModel.HMaterTypeID = 2; } else if (DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sMaterTypeCol].Value) == "ίÍâ") { oMater.oModel.HMaterTypeID = 3; } else { oMater.oModel.HMaterTypeID = 0; } oMater.oModel.HUnitGroupID = 0; oMater.oModel.HRoutingID = 0; oMater.oModel.HBomID = 0; oMater.oModel.HSecUnitID = 0; oMater.oModel.HSecUnitRate = 0; oMater.oModel.HHighStock = DBUtility.ClsPub.isDoule(grdmain.Rows[row].Cells[sMaxQtyCol].Value); oMater.oModel.HLowStock = DBUtility.ClsPub.isDoule(grdmain.Rows[row].Cells[sMinQtyCol].Value); oMater.oModel.HSafeStock = DBUtility.ClsPub.isDoule(grdmain.Rows[row].Cells[sSafeQtyCol].Value); oMater.oModel.HOrderPrice = DBUtility.ClsPub.isDoule(grdmain.Rows[row].Cells[sPOPriceCol].Value); oMater.oModel.HSalePrice = DBUtility.ClsPub.isDoule(grdmain.Rows[row].Cells[sSEPriceCol].Value); oMater.oModel.HKeepDays = 0; oMater.oModel.HPlanPrice = DBUtility.ClsPub.isDoule(grdmain.Rows[row].Cells[sPlanPriceCol].Value); oMater.oModel.HstdPrice = DBUtility.ClsPub.isDoule(grdmain.Rows[row].Cells[sCBPriceCol].Value); oMater.oModel.HQtyMin = DBUtility.ClsPub.isDoule(grdmain.Rows[row].Cells[sMinSelQtyCol].Value); oMater.oModel.HQtyMax = DBUtility.ClsPub.isDoule(grdmain.Rows[row].Cells[sMaxSelQtyCol].Value); oMater.oModel.HVersion = ""; oMater.oModel.HEngName = ""; oMater.oModel.HEngModel = ""; oMater.oModel.HPropertyTypeID = 0; oMater.oModel.HColor = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sColorCol].Value); oMater.oModel.HPicNo = DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sPicNoCol].Value); oMater.oModel.HSPGroupID = 0; if (oWh.GetInfoByName(DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sWhCol].Value))) { oMater.oModel.HWhID = oWh.omodel.HItemID; } else { oMater.oModel.HWhID = 0; } if (oSP.GetInfoByNumber(DBUtility.ClsPub.isStrNull(grdmain.Rows[row].Cells[sSPCol].Value))) { oMater.oModel.HSPID = oSP.omodel.HItemID; } else { oMater.oModel.HSPID = 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; /// /// ½«excelÖеÄÊý¾Ýµ¼Èëµ½DataTableÖÐ /// /// excel·¾¶ /// excel¹¤×÷±¡sheetµÄÃû³Æ /// µÚÒ»ÐÐÊÇ·ñÊÇDataTableµÄÁÐÃû /// ·µ»ØµÄDataTable 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 = ""; } } } }