public void NhapDuLieu()
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "Excel file (*.xls; *.xlsx; *.xlsm; *.xlsb)|*.xls";
            openFile.ShowDialog();

            if (openFile.FileName == String.Empty)
                return;

            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            Excel.Worksheet sheetDocGia, sheetHocSinh, sheetNhanVien, sheetViPham;
            Excel.Worksheet sheetTuaSach, sheetDauSach, sheetCuonSach;
            Excel.Worksheet sheetMuonSach;
            Excel.Worksheet sheetDkChoMuon;
            Excel.Range rangeDocGia;

            string str;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open(openFile.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);

            sheetDocGia = xlWorkBook.Sheets["Độc giả"] as Excel.Worksheet;
            sheetHocSinh = xlWorkBook.Sheets["Học sinh"] as Excel.Worksheet;
            sheetNhanVien = xlWorkBook.Sheets["Nhân viên"] as Excel.Worksheet;
            sheetViPham = xlWorkBook.Sheets["Vi phạm"] as Excel.Worksheet;
            sheetTuaSach = xlWorkBook.Sheets["Tựa sách"] as Excel.Worksheet;
            sheetDauSach = xlWorkBook.Sheets["Đầu sách"] as Excel.Worksheet;
            sheetCuonSach = xlWorkBook.Sheets["Cuốn sách"] as Excel.Worksheet;
            sheetMuonSach = xlWorkBook.Sheets["Mượn sách"] as Excel.Worksheet;
            sheetDkChoMuon = xlWorkBook.Sheets["ĐK chờ mượn"] as Excel.Worksheet;
            //sheetDocGia = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            /*sheetHocSinh = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
            sheetNhanVien = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(3);
            sheetViPham = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(4);
            sheetTuaSach = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(5);
            sheetDauSach = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(6);
            sheetCuonSach = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(7);
            sheetMuonSach = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(8);
            sheetDkChoMuon = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(9);*/

            //range = xlWorkSheet.UsedRange;

            //for (int rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
            //{
            //    for (int cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
            //    {
            //        str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
            //        MessageBox.Show(str);
            //    }
            //}

            //rangeDocGia = sheetDocGia.UsedRange;

            //if ((sheetDocGia.Cells[2, 6] as Excel.Range).Value == false)
            //    MessageBox.Show("false");
            //MessageBox.Show(Convert.ToString((sheetDocGia.Cells[2,6] as Excel.Range).Value));

            DangKyCho dataDkCho = new DangKyCho();
            MuonTraSach dataMuonSach = new MuonTraSach();
            Sach dataSach = new Sach();
            DocGia dataDocGia = new DocGia();
            ViPham dataViPham = new ViPham();
            dataDkCho.xoaTatCa();
            dataMuonSach.xoaTatCa();
            dataSach.xoaTatCa();
            dataViPham.xoaTatCa();
            dataDocGia.xoaTatCa();
            //Excel.Range rangeDkChoMuon = sheetDkChoMuon.UsedRange;

            DOCGIA dg;
            for (int i = 2; i <= sheetDocGia.UsedRange.Rows.Count; i++)
            {
                dg = new DOCGIA();
                dg.madocgia = Convert.ToString((sheetDocGia.Cells[i, 1] as Excel.Range).Value);
                dg.hoten = Convert.ToString((sheetDocGia.Cells[i, 2] as Excel.Range).Value);
                dg.gioitinh = Convert.ToString((sheetDocGia.Cells[i, 3] as Excel.Range).Value);
                String date = Convert.ToString((sheetDocGia.Cells[i, 4] as Excel.Range).Value);
                if (date != null)
                    dg.ngaysinh = DateTime.Parse(date);
                String date1 = Convert.ToString((sheetDocGia.Cells[i, 5] as Excel.Range).Value);
                if (date1 != null)
                    dg.ngaylap = DateTime.Parse(date1);
                dg.tinhtrang = (bool)(sheetDocGia.Cells[i, 6] as Excel.Range).Value;
                dataDocGia.themDocGia(dg);
            }

            HOCSINH hs;
            for (int i = 2; i <= sheetHocSinh.UsedRange.Rows.Count; i++)
            {
                hs = new HOCSINH();
                hs.madocgia = Convert.ToString((sheetHocSinh.Cells[i, 1] as Excel.Range).Value);
                hs.lop = Convert.ToString((sheetHocSinh.Cells[i, 2] as Excel.Range).Value);
                dataDocGia.themHocSinh(hs);
            }

            NHANVIEN nv;
            for (int i = 2; i <= sheetNhanVien.UsedRange.Rows.Count; i++)
            {
                nv = new NHANVIEN();
                nv.madocgia = Convert.ToString((sheetNhanVien.Cells[i, 1] as Excel.Range).Value);
                dataDocGia.themNhanVien(nv);
            }

            VIPHAM vp;
            for (int i = 2; i <= sheetViPham.UsedRange.Rows.Count; i++)
            {
                vp = new VIPHAM();
                vp.madocgia = Convert.ToString((sheetViPham.Cells[i, 1] as Excel.Range).Value);
                vp.vipham1 = Convert.ToInt32((sheetViPham.Cells[i, 2] as Excel.Range).Value);
                String date = Convert.ToString((sheetViPham.Cells[i, 3] as Excel.Range).Value);
                if (date != null)
                    vp.ngayhethan = DateTime.Parse(date);
                dataViPham.themViPham(vp);
            }

            TUASACH ts;
            for (int i = 2; i <= sheetTuaSach.UsedRange.Rows.Count; i++)
            {
                ts = new TUASACH();
                ts.matuasach = Convert.ToString((sheetTuaSach.Cells[i, 1] as Excel.Range).Value);
                ts.tentuasach = Convert.ToString((sheetTuaSach.Cells[i, 2] as Excel.Range).Value);
                ts.tacgia = Convert.ToString((sheetTuaSach.Cells[i, 3] as Excel.Range).Value);
                ts.gioithieu = Convert.ToString((sheetTuaSach.Cells[i, 4] as Excel.Range).Value);
                dataSach.themTuaSach(ts);
            }

            DAUSACH ds;
            for (int i = 2; i <= sheetDauSach.UsedRange.Rows.Count; i++)
            {
                ds = new DAUSACH();
                ds.madausach = Convert.ToString((sheetDauSach.Cells[i, 1] as Excel.Range).Value);
                ds.matuasach = Convert.ToString((sheetDauSach.Cells[i, 2] as Excel.Range).Value);
                ds.ngonngu = Convert.ToString((sheetDauSach.Cells[i, 3] as Excel.Range).Value);
                ds.tinhtrang = (bool)(sheetDauSach.Cells[i, 4] as Excel.Range).Value;
                dataSach.themDauSach(ds);
            }

            CUONSACH cs;
            for (int i = 2; i <= sheetCuonSach.UsedRange.Rows.Count; i++)
            {
                cs = new CUONSACH();
                cs.macuonsach = Convert.ToString((sheetCuonSach.Cells[i, 1] as Excel.Range).Value);
                cs.madausach = Convert.ToString((sheetCuonSach.Cells[i, 2] as Excel.Range).Value);
                cs.tinhtrang = (bool)(sheetCuonSach.Cells[i, 3] as Excel.Range).Value;
                dataSach.themCuonSach(cs);
            }

            DKCHOMUON dk;
            for (int i = 2; i <= sheetDkChoMuon.UsedRange.Rows.Count; i++)
            {
                dk = new DKCHOMUON();
                dk.madocgia = Convert.ToString((sheetDkChoMuon.Cells[i, 1] as Excel.Range).Value);
                dk.madausach = Convert.ToString((sheetDkChoMuon.Cells[i, 2] as Excel.Range).Value);
                String date = Convert.ToString((sheetDkChoMuon.Cells[i, 3] as Excel.Range).Value);
                if (date != null)
                    dk.ngaygiodk = DateTime.Parse(date);
                dk.tinhtrang = (bool)(sheetDkChoMuon.Cells[i, 4] as Excel.Range).Value;
                dataDkCho.themDangKyCho(dk);
            }

            MUONSACH ms;
            for (int i = 2; i <= sheetMuonSach.UsedRange.Rows.Count; i++)
            {
                ms = new MUONSACH();
                ms.madocgia = Convert.ToString((sheetMuonSach.Cells[i, 1] as Excel.Range).Value);
                ms.macuonsach = Convert.ToString((sheetMuonSach.Cells[i, 2] as Excel.Range).Value);
                String date = Convert.ToString((sheetMuonSach.Cells[i, 3] as Excel.Range).Value);
                if (date != null)
                    ms.ngaygiomuon = DateTime.Parse(date);
                String date1 = Convert.ToString((sheetMuonSach.Cells[i, 4] as Excel.Range).Value);
                if (date1 != null)
                    ms.ngayhethan = DateTime.Parse(date1);
                String date2 = Convert.ToString((sheetMuonSach.Cells[i, 5] as Excel.Range).Value);
                if (date2 != null)
                    ms.ngaygiotra = DateTime.Parse(date2);
                dataMuonSach.themMuonTraSach(ms);
            }

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

            MessageBox.Show("Hoàn thành!");
            noticeImport();

            //releaseObject(xlWorkSheet);
            //releaseObject(xlWorkBook);
            //releaseObject(xlApp);
        }
Пример #2
0
        private void btndelalldg_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult delete = MessageBox.Show("Bạn có thực muốn xóa hết dữ liệu này?", "Exit", MessageBoxButtons.YesNo);
                if (delete == DialogResult.No)
                    return;
                dataDocGia.xoaTatCa();
                ViPham dataViPham = new ViPham();
                dataViPham.xoaTatCa();

                updateDgvDocGia();
            }
            catch (SqlException)
            {
                MessageBox.Show("Có ràng buộc dữ liệu, không thể xóa!");
            }
        }