コード例 #1
0
ファイル: MyContact.cs プロジェクト: ayesha159/Domain-Models
        private void button3_Click(object sender, EventArgs e)
        {
            if (cmbCon.Items.Count == 0)
            {
                MessageBox.Show("Add Group Please !"); return;
            }


            Excel.Application xlApp;
            Excel.Workbook    xlWorkBook;
            Excel.Worksheet   xlWorkSheet;
            Excel.Range       range;

            string str;
            int    rCnt;

            int    rw       = 0;
            int    cl       = 0;
            string filename = "";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                filename = openFileDialog1.FileName;
            }
            xlApp       = new Excel.Application();
            xlWorkBook  = xlApp.Workbooks.Open(filename, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            range = xlWorkSheet.UsedRange;
            rw    = range.Rows.Count;
            cl    = range.Columns.Count;
            string _name    = "";
            string _mob     = "";
            string _address = "";
            string _email   = "";
            string _dob     = "";


            for (rCnt = 1; rCnt <= rw; rCnt++)
            {
                //for (cCnt = 1; cCnt <= cl; )
                {
                    _name    = Convert.ToString((range.Cells[rCnt, 1] as Excel.Range).Value2);
                    _mob     = "+" + Convert.ToString((range.Cells[rCnt, 2] as Excel.Range).Value2);
                    _dob     = Convert.ToString((range.Cells[rCnt, 3] as Excel.Range).Value2);
                    _email   = Convert.ToString((range.Cells[rCnt, 4] as Excel.Range).Value2);
                    _address = Convert.ToString((range.Cells[rCnt, 5] as Excel.Range).Value2);
                    str      = _name + ", " + _mob + ", " + _dob;
                    Contacts.Service1 ser = new Contacts.Service1();
                    try
                    {
                        string sv = ser.ImportContacts(_name, _dob, _mob, _email, _address, myUtill.loginUser.Userid, cmbCon.SelectedValue.ToString());
                    } catch (Exception ex) { }
                    BindingSource b = new BindingSource();
                    b.DataSource     = ser.GetCon();
                    gvCon.DataSource = b;
                }
            }

            xlWorkBook.Close(true, null, null);
            xlApp.Quit();

            Marshal.ReleaseComObject(xlWorkSheet);
            Marshal.ReleaseComObject(xlWorkBook);
            Marshal.ReleaseComObject(xlApp);
        }