iT邦幫忙

0

C# 讀取不同格式的檔案內容

Ming 2022-11-08 14:15:221433 瀏覽
  • 分享至 

  • xImage

https://ithelp.ithome.com.tw/upload/images/20221108/201522755Xa19rvkly.png
請教各位先進

小弟遇到一個問題:在一個資料夾下有不同的文件,我要如何讀取他們內容中的字串。或是有什麼方法可以比對他們的內容是否有我所提供的關鍵字。

感謝各位。

看更多先前的討論...收起先前的討論...
froce iT邦大師 1 級 ‧ 2022-11-08 14:17:56 檢舉
上一次跟你說過啦,去找全文檢索的程式裝一裝,你要自己寫得會不少東西。
Ming iT邦新手 5 級 ‧ 2022-11-08 14:27:48 檢舉
好的,我嘗試看看謝謝大大。
allenlwh iT邦高手 1 級 ‧ 2022-11-08 14:45:01 檢舉
真心建議如 froce 所說,您的這些需求要全文檢索才能做到既快又準!!
還能兼顧執行效能
Ming iT邦新手 5 級 ‧ 2022-11-08 15:01:42 檢舉
目前還不熟悉全文檢索這一塊,請問有相關資料可以參考嗎?還不太懂怎麼運用在C# 程式
allenlwh iT邦高手 1 級 ‧ 2022-11-08 15:49:41 檢舉
市面上全文檢索的套裝工具 應該有很多,上google 查一下 就有
copemoe iT邦研究生 1 級 ‧ 2022-11-08 17:08:28 檢舉
開源檔案搜尋與全文檢索軟體:Recoll
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
JamesDoge
iT邦高手 1 級 ‧ 2022-12-23 05:16:58
  1. 首先,你需要安裝 EPPlus 套件(我用4.5.3.3)。你可以在 NuGet 管理器中安裝它。
  2. 範例如下:
using System;
using System.IO;
using OfficeOpenXml;

namespace FileContentSearch
{
    class Program
    {
        static void Main(string[] args)
        {
            string folderPath = "C:\\MyFolder\\"; // 資料夾路徑
            string keyword = "hello"; // 關鍵字

            // 檢查資料夾是否存在
            if (!Directory.Exists(folderPath))
            {
                Console.WriteLine($"資料夾 '{folderPath}' 不存在");
                return;
            }

            bool found = false; // 是否找到關鍵字

            // 取得資料夾內的所有文件
            string[] files = Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories);

            foreach (string file in files)
            {
                // 判斷文件副檔名
                string extension = Path.GetExtension(file);

                if (extension == ".txt") // 若是 .txt 檔
                {
                    // 使用 StreamReader 讀取文件內容
                    using (StreamReader reader = new StreamReader(file))
                    {
                        string content = reader.ReadToEnd(); // 讀取整個文件

                        // 判斷是否包含關鍵字
                        if (content.Contains(keyword))
                        {
                            Console.WriteLine($"找到關鍵字 '{keyword}' 於 {file}");
                            found = true;
                        }
                    }
                }
                else if (extension == ".docx" || extension == ".xlsx" || extension == ".pptx") // 若是 Office 檔
                {
                    // 使用 EPPlus 套件讀取文件內容
                    using (ExcelPackage package = new ExcelPackage(new FileInfo(file)))
                    {
                        // 取得第一個工作表
                        ExcelWorksheet worksheet = package.Workbook.Worksheets[1];

                        // 取得工作表中的所有內容 (使用 All 表示所有列所有欄)
                        object[,] values = worksheet.Cells.Value as object[,];

                        // 迭代每個儲存格
                        for (int row = 1; row <= values.GetLength(0); row++)
                        {
                            for (int col = 1; col <= values.GetLength(1); col++)
                            {
                                // 判斷是否包含關鍵字
                                if (values[row, col]?.ToString().Contains(keyword) == true)
                                {
                                    Console.WriteLine($"找到關鍵字 '{keyword}' 於 {file} (第 {row} 行第 {col} 列)");
                                    found = true;
                                }
                            }
                        }

                        // 若找不到關鍵字,則輸出訊息
                        if (!found)
                        {
                            Console.WriteLine($"在資料夾 '{folderPath}' 內的文件中,找不到關鍵字 '{keyword}'");
                        }

                        // 等待使用者按下 Enter 鍵結束程式
                        Console.ReadLine();
                    }
                }
            }
        }
    }
}    

3.完整專案範例我放到這裡 https://github.com/Jia-Hong-Peng/C-

我要發表回答

立即登入回答