Example #1
0
        private void importBt_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();

            fdlg.Filter = "*.XLS,*.XLSX|*.XLS;*.XLSX";
            if (fdlg.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            FileStream fs  = File.OpenRead(fdlg.FileName);
            string     ext = Path.GetExtension(fdlg.FileName);
            IWorkbook  wk  = null;

            if (ext.ToLower().Equals(".xls"))
            {
                wk = new HSSFWorkbook(fs);
            }
            else
            {
                wk = new XSSFWorkbook(fs);
            }
            fs.Close();

            persons.Clear();

            ISheet sheet = wk.GetSheetAt(0);

            System.Data.SQLite.SQLiteConnection cn    = persons.OpenTran();
            System.Data.Common.DbTransaction    trans = cn.BeginTransaction();
            for (int i = 1; i <= sheet.LastRowNum; i++)
            {
                IRow row = sheet.GetRow(i);
                if (row.GetCell(0) == null)
                {
                    break;
                }
                string name = row.GetCell(0).ToString();
                string text = "";
                if (row.GetCell(1) != null)
                {
                    text = row.GetCell(1).ToString();
                }
                persons.Add(new Person(name, text), cn);
            }
            trans.Commit();
            persons.CloseTran(cn);

            MessageBox.Show("导入完毕");

            this.personsLv.VirtualListSize = persons.Count;
            personsLv.Invalidate();
        }