C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト

■103060 / 親階層)  検索を早く
□投稿者/ よし (1回)-(2024/04/15(Mon) 10:40:55)

分類:[C#] 

Windows フォームアプリケーション.NetFramework c#

宜しくお願い致します

現在フォルダの中にエクセルファイルが10個入っています
そのエクセルファイルの中に1行に26列までデータが入っていまして1000行まであります


txtModelNoとtxtModelNo2に入れた文字が合致したものをデータグリッドビューに表示させています

txtModelNoはエクセルファイルのA列、txtModelNo2はB列を見に行っています

合致したものは正しく表示されるのですが
凄く遅く感じます

y列に何か入っている場合は次の行に飛びまして
空白が8行続いたら次のエクセルファイルに移るようにしています

速く検索できる方法はあるでしょうか
宜しくお願い致します


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;

namespace チェックPGM
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.WindowState = FormWindowState.Maximized;
            dataGridView1.DefaultCellStyle.Font = new Font("Arial", 11);
            dataGridView1.ColumnHeadersHeight = 600;

            // 列の追加
            string[] columnHeaders = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA" };
            foreach (var columnHeader in columnHeaders)
            {
                dataGridView1.Columns.Add("", columnHeader);
            }
        }

        private void buttonSearch_Click_1(object sender, EventArgs e)
        {
            string searchValue1 = txtModelNo.Text;
            string searchValue2 = txtModelNo2.Text;
            bool ignoreSuccess = checkBoxIgnoreSuccess.Checked;

          SearchExcelFiles(searchValue1, searchValue2, @"C:\Users\AZ\Documents\az-test", ignoreSuccess);
        }

        private void SearchExcelFiles(string searchValue1, string searchValue2, string folderPath, bool ignoreSuccess)
        {
            if (!Directory.Exists(folderPath))
            {
                MessageBox.Show("指定されたフォルダーは存在しません。");
                return;
            }

            Excel.Application excelApp = new Excel.Application();
            Excel.Workbook excelWorkbook = null;

            bool matchFound = false;
            try
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(folderPath);
                foreach (FileInfo fileInfo in directoryInfo.GetFiles("*.xlsx"))
                {
                    excelWorkbook = excelApp.Workbooks.Open(fileInfo.FullName);
                    Excel.Worksheet excelWorksheet = excelWorkbook.Sheets[1];

                    textBox3.Text = fileInfo.Name;

                    Excel.Range usedRange = excelWorksheet.UsedRange;
                    int rowCount = usedRange.Rows.Count;

                    int emptyCellCount = 0;
                    for (int i = 1; i <= rowCount; i++)
                    {
                        textBox4.Text = i.ToString();

                        if (usedRange.Cells[i, "Y"].Value2 != null && !string.IsNullOrEmpty(usedRange.Cells[i, "Y"].Value2.ToString()))
                        {
                            continue;
                        }

                        if (usedRange.Cells[i, 1].Value2 == null || string.IsNullOrEmpty(usedRange.Cells[i, 1].Value2.ToString()))
                        {
                            emptyCellCount++;
                        }
                        else
                        {
                            emptyCellCount = 0;
                        }

                        if (emptyCellCount >= 8) //空白セルが8回続けば次のファイルへ
                        {
                            break;
                        }

                        bool matchesSearch1 = usedRange.Cells[i, 1].Value2 != null && usedRange.Cells[i, 1].Value2.ToString() == searchValue1;
                        bool matchesSearch2 = usedRange.Cells[i, 2].Value2 != null && usedRange.Cells[i, 2].Value2.ToString() == searchValue2;

                        if (matchesSearch1 && matchesSearch2)
                        {
                            matchFound = true;
                            List<object> rowData = new List<object>();
                            for (int j = 1; j <= 26; j++)
                            {
                                rowData.Add(usedRange.Cells[i, j].Value2);
                            }
                            rowData.Add(fileInfo.FullName);
                            rowData.Add(i);
                            dataGridView1.Invoke((MethodInvoker)delegate
                            {
                                dataGridView1.Rows.Add(rowData.ToArray());
                            });

                            break; //検索終
                        }

                    }

                    Marshal.ReleaseComObject(usedRange);
                    Marshal.ReleaseComObject(excelWorksheet);
                    excelWorkbook.Close(false);
                    Marshal.ReleaseComObject(excelWorkbook);

                    if (matchFound)
                    {
                        break; // 検索を終了する
                    }

                }
                if (!matchFound)
                {
                    MessageBox.Show("検索結果が見つかりませんでした。");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("エラーが発生しました: " + ex.Message);
            }
            finally
            {
                excelApp.Quit();
                Marshal.ReleaseComObject(excelApp);
            }
        }
    }
}

編集キー/

前の記事(元になった記事) 次の記事(この記事の返信)
親記事 →Re[1]: 検索を早く /WebSurfer
→Re[1]: 検索を早く /魔界の仮面弁士
 
上記関連ツリー

検索を早く / よし (24/04/15(Mon) 10:40) #103060 ←Now
Re[1]: 検索を早く / WebSurfer (24/04/15(Mon) 12:43) #103061
│└ Re[2]: 検索を早く / ぶなっぷ (24/04/15(Mon) 13:31) #103062
│  └ Re[3]: 検索を早く / ぶなっぷ (24/04/15(Mon) 13:36) #103063
Re[1]: 検索を早く / 魔界の仮面弁士 (24/04/15(Mon) 14:40) #103064
  └ Re[2]: 検索を早く / furu (24/04/16(Tue) 11:28) #103065
    └ Re[3]: 検索を早く / よし (24/04/19(Fri) 17:11) #103084 解決済み

上記ツリーを一括表示 / 上記ツリーをトピック表示
 
上記の記事へ返信