Exemple #1
0
        /// <summary>
        /// 指定されたExcelファイルを読み込む
        /// </summary>
        private void ReadExcel()
        {
            //Excelファイル選択し、選択されなければ終了。
            if (this.openFileExcel.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            Address_DS ads     = new Address_DS();
            Address_DS errords = new Address_DS();

            // ExcelWorkBookオープン
            ExcelWorkBook wBook = new ExcelWorkBook();

            wBook.Open(this.openFileExcel.FileName);
            try
            {
                //1行目を読み込む
                ExcelWorkSheet sheet      = wBook.WorkSheets[0];
                ArrayList      columnList = new ArrayList();
                for (int col = sheet.FirstColumn; col <= sheet.LastColumn; col++)
                {
                    if (sheet.Cells[0, col].Value == null)
                    {
                        continue;
                    }
                    columnList.Add(sheet.Cells[0, col].Value);
                }

                //読み込み行がない場合は空のデータセットを返す。
                if (columnList.Count == 0)
                {
                    MessageBox.Show(this, "読み込み可能な行がありません。");
                    return;
                }

                using (ColumnSelectForm f = new ColumnSelectForm(columnList))
                {
                    //行選択画面を表示し、ユーザに読み込む行を指定してもらう
                    if (f.ShowDialog() != DialogResult.OK)
                    {
                        //選択されなければ、空のデータセットを返す。
                        return;
                    }

                    int readStartRowIndex = 0;
                    if (f.ReadFirstRow == true)
                    {
                        readStartRowIndex = 0;
                    }
                    else
                    {
                        readStartRowIndex = 1;
                    }


                    //ユーザが指定した列を読み込む
                    for (int row = readStartRowIndex; row <= sheet.LastRow; row++)
                    {
                        //NULLやすでに取り込まれたメールアドレスははずす。
                        if (!((sheet.Cells[row, f.SelectColumnIndex].Value == null) ||
                              ads.Address.FindByMailAddress(sheet.Cells[row, f.SelectColumnIndex].Value.ToString()) != null))
                        {
                            string mailAddr = sheet.Cells[row, f.SelectColumnIndex].Value.ToString();
                            //メールアドレス形式のアドレスのみ登録
                            if (TKMP.Writer.MailAddressCollection.IsAddressPattern(mailAddr) == true)
                            {
                                Address_DS.AddressRow aRow = ads.Address.NewAddressRow();
                                aRow.MailAddress = mailAddr;
                                ads.Address.AddAddressRow(aRow);
                            }
                            else
                            {
                                //メールアドレス形式でないものは、エラーリストに保存
                                Address_DS.AddressRow aeRow = errords.Address.NewAddressRow();
                                aeRow.MailAddress = mailAddr;
                                errords.Address.AddAddressRow(aeRow);
                            }
                        }
                    }
                }
            }
            finally
            {
                // ExcelWorkBookクローズ
                wBook.Close();
            }

            //Excelファイルを読み取る。
            this._addressds.Merge(ads);

            //エラー情報をセットする。
            this._errorForm.ErrorList = errords;
        }
        /// <summary>
        /// �w�肳�ꂽExcel�t�@�C����ǂݍ���
        /// </summary>
        private void ReadExcel()
        {
            //Excel�t�@�C���I����A�I�����Ȃ���ΏI���B
            if (this.openFileExcel.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            Address_DS ads = new Address_DS();
            Address_DS errords = new Address_DS();

            // ExcelWorkBook�I�[�v��
            ExcelWorkBook wBook = new ExcelWorkBook();
            wBook.Open(this.openFileExcel.FileName);
            try
            {
                //�P�s�ڂ�ǂݍ���
                ExcelWorkSheet sheet = wBook.WorkSheets[0];
                ArrayList columnList = new ArrayList();
                for (int col = sheet.FirstColumn; col <= sheet.LastColumn; col++)
                {
                    if (sheet.Cells[0, col].Value == null)
                    {
                        continue;
                    }
                    columnList.Add(sheet.Cells[0, col].Value);
                }

                //�ǂݍ��ݍs���Ȃ��ꍇ�͋�̃f�[�^�Z�b�g��Ԃ��B
                if (columnList.Count == 0)
                {
                    MessageBox.Show(this, "�ǂݍ��݉”\�ȍs������܂���B");
                    return;
                }

                using (ColumnSelectForm f = new ColumnSelectForm(columnList))
                {
                    //�s�I���ʂ�\�����A���[�U�ɓǂݍ��ލs��w�肵�Ă�炤
                    if (f.ShowDialog() != DialogResult.OK)
                    {
                        //�I�����Ȃ���΁A��̃f�[�^�Z�b�g��Ԃ��B
                        return ;
                    }

                    int readStartRowIndex = 0;
                    if (f.ReadFirstRow == true) readStartRowIndex = 0;
                    else readStartRowIndex = 1;

                    //���[�U���w�肵�����ǂݍ���
                    for (int row = readStartRowIndex; row <= sheet.LastRow; row++)
                    {
                        //NULL�₷�łɎ�荞�܂ꂽ���[���A�h���X�͂͂����B
                        if (!((sheet.Cells[row, f.SelectColumnIndex].Value == null) ||
                            ads.Address.FindByMailAddress(sheet.Cells[row, f.SelectColumnIndex].Value.ToString()) != null))
                        {
                            string mailAddr = sheet.Cells[row, f.SelectColumnIndex].Value.ToString();
                            //���[���A�h���X�`���̃A�h���X�̂ݓo�^
                            if (TKMP.Writer.MailAddressCollection.IsAddressPattern(mailAddr) == true)
                            {
                                Address_DS.AddressRow aRow = ads.Address.NewAddressRow();
                                aRow.MailAddress = mailAddr;
                                ads.Address.AddAddressRow(aRow);
                            }
                            else
                            {
                                //���[���A�h���X�`���łȂ���̂́A�G���[���X�g�ɕۑ�
                                Address_DS.AddressRow aeRow = errords.Address.NewAddressRow();
                                aeRow.MailAddress = mailAddr;
                                errords.Address.AddAddressRow(aeRow);
                            }
                        }
                    }
                }
            }
            finally
            {
                // ExcelWorkBook�N���[�Y
                wBook.Close();
            }

            //Excel�t�@�C����ǂݎ��B
            this._addressds.Merge(ads);

            //�G���[����Z�b�g����B
            this._errorForm.ErrorList = errords;
        }