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_Source_K3 : Form 
 | 
    { 
 | 
        // 
 | 
        private string fileName = null; //ÎļþÃû 
 | 
        private IWorkbook workbook = null; 
 | 
        private FileStream fs = null; 
 | 
        private bool disposed; 
 | 
  
 | 
        public Gy_DataIn_Source_K3() 
 | 
        { 
 | 
            InitializeComponent(); 
 | 
        } 
 | 
  
 | 
        private void initHeadCaption() 
 | 
        { 
 | 
  
 | 
        } 
 | 
  
 | 
        private void yl_Click(object sender, EventArgs e) 
 | 
        { 
 | 
            WriteClass(); 
 | 
        } 
 | 
  
 | 
        //Êý¾Ýµ¼Èë 
 | 
        public void WriteClass() 
 | 
        { 
 | 
            DAL.ClsK3_Source_Ctl BillNew = new DAL.ClsK3_Source_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 sHShortNameCol = DBUtility.Gy_BaseFun.Fun_GetCol("¼ò³Æ", grdmain); 
 | 
            //int sHAddressCol = DBUtility.Gy_BaseFun.Fun_GetCol("µØÖ·", grdmain); 
 | 
            //int sHContactCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÁªÏµÈË", grdmain); 
 | 
            //int sHPhoneCol = DBUtility.Gy_BaseFun.Fun_GetCol("µç»°", grdmain); 
 | 
            //int sHMobilePhoneCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÒÆ¶¯µç»°", grdmain); 
 | 
            //int sHFaxCol = DBUtility.Gy_BaseFun.Fun_GetCol("´«Õæ", grdmain); 
 | 
            //int sHPostalCodeCol = DBUtility.Gy_BaseFun.Fun_GetCol("Óʱà", grdmain); 
 | 
            //int sHEmailCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÓʼþµØÖ·", grdmain); 
 | 
            //int sHBankCol = DBUtility.Gy_BaseFun.Fun_GetCol("¿ª»§ÒøÐÐ", grdmain); 
 | 
            //int sHAccountCol = DBUtility.Gy_BaseFun.Fun_GetCol("ÒøÐÐÕ˺Å", grdmain); 
 | 
             
 | 
            for (int i = 0; i <= grdmain.Rows.Count - 1; i++) 
 | 
            { 
 | 
                //if (oEmp.GetInfoByNumber(DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHEmpNumberCol].Value))) 
 | 
                //{ 
 | 
                //    BillNew.oModel.HEmpID = oEmp.omodel.HItemID; 
 | 
                //} 
 | 
                //else 
 | 
                //{ 
 | 
                //    BillNew.oModel.HEmpID = 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.HShortName = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHShortNameCol].Value); 
 | 
                //BillNew.oModel.HAddress = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHAddressCol].Value); 
 | 
                //BillNew.oModel.HContact = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHContactCol].Value); 
 | 
                //BillNew.oModel.HPhone = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHPhoneCol].Value); 
 | 
                //BillNew.oModel.HMobilePhone = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHMobilePhoneCol].Value); 
 | 
                //BillNew.oModel.HFax = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHFaxCol].Value); 
 | 
                //BillNew.oModel.HPostalCode = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHPostalCodeCol].Value); 
 | 
                //BillNew.oModel.HEmail = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHEmailCol].Value); 
 | 
                //BillNew.oModel.HBank = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHBankCol].Value); 
 | 
                //BillNew.oModel.HAccount = DBUtility.ClsPub.isStrNull(grdmain.Rows[i].Cells[sHAccountCol].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); 
 | 
        } 
 | 
  
 | 
  
 | 
    } 
 | 
} 
 |