我正在尋找可以讀取 Excel 檔案并可以在該檔案中搜索單詞的程式,謝謝您使用此代碼可以讀取所有單元格,但我想添加可以搜索我輸入的單詞的功能(console.readLine )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
namespace ReadFromExcelComponente
{
class Program
{
static void Main(string[] args)
{
//lese den excel
Excel.Application ExcelApp;
Excel.Workbook workbook;
Excel.Worksheet worksheet;
Excel.Range range;
int row = 0;
int column = 0;
ExcelApp = new Excel.Application();
workbook = ExcelApp.Workbooks.Open(@"D:\Map", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
worksheet = (Excel.Worksheet)workbook.Worksheets.get_Item(1);
range = worksheet.UsedRange;
List<String> myList = new List<string>();
for (row = 1; row <= range.Rows.Count; row )
{
for (column = 1; column <= range.Columns.Count; column )
{
Console.WriteLine(" Coulmn Number: " column "--> " (range.Cells[row, column] as Excel.Range).Value2);
myList.Add(range.Cells[column].ToString());
}
}
Console.WriteLine(myList[2]);
Console.WriteLine(range.Cells[2, 2].Value);
workbook.Close(true, null, null);
ExcelApp.Quit();
Console.ReadKey();
}
}
}
uj5u.com熱心網友回復:
嘗試這個:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
namespace ReadFromExcelComponente
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the word you want to search for:");
string SearchWord = Console.ReadLine();
//lese den excel
Excel.Application ExcelApp;
Excel.Workbook workbook;
Excel.Worksheet worksheet;
Excel.Range range;
int row = 0;
int column = 0;
ExcelApp = new Excel.Application();
workbook = ExcelApp.Workbooks.Open(@"D:\Map", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
worksheet = (Excel.Worksheet)workbook.Worksheets.get_Item(1);
range = worksheet.UsedRange;
for (row = 1; row <= range.Rows.Count; row )
{
for (column = 1; column <= range.Columns.Count; column )
{
if(range.Range(row, column).Value = SearchWord)
{
Console.WriteLine("Found word!");
break;
}
}
}
Console.WriteLine(myList[2]);
Console.WriteLine(range.Cells[2, 2].Value);
workbook.Close(true, null, null);
ExcelApp.Quit();
ExcelApp = null;
Console.ReadKey();
}
}
}
uj5u.com熱心網友回復:
當“您的代碼”啟動 Excel 應用程式并打開一個 Excel 檔案時……您的代碼將負責“釋放”它創建的 COM 物件……即……作業簿和 Excel 應用程式。
如果您不釋放這些物件,就像您當前的代碼一樣,那么這些物件將作為活動物件保留在記憶體中,并且基本上永遠不會被釋放。即使您的代碼關閉了檔案和 Excel 應用程式……您的代碼仍然必須明確“釋放”COM 物件。當然,如果您重新啟動機器甚至退出應用程式……那么這些物件可能/將被釋放。但是,這并不減損您作為編碼人員的責任,即釋放您的代碼創建的所有物件。
幸運的是,管理 Excel 資源并不困難。一種相當直接的方法是使用try/catch/finally陳述句來管理資源。該try部分是我們放置處理 Excel 檔案的所有代碼的地方。該catch部分用于顯然捕獲例外。這finally部分是我們放置代碼以“釋放”Excel COM 物件的地方。
使用這種方法,我們應該能夠更輕松地知道 COM 物件仍將被釋放......即使代碼在處理 Excel 檔案時崩潰。
接下來,我知道有時您必須使用InteropExcel 檔案,我經常使用它并且知道它可能很尷尬。更不用說這Interop是出了名的慢,尤其是在處理大型 Excel 檔案時。我的觀點是,如果您有選擇,我強烈推薦第三方 Excel 庫。有很多可供選擇,有些是免費的。
從我的肥皂盒里下來……
查看您的代碼,當代碼UsedRange從 Excel 作業表中獲取時,我沒有遵循一件事......
range = worksheet.UsedRange;
這range是一個基本的 Excel 范圍物件,其中包含作業表中所有“已使用”的單元格。到現在為止還挺好…
但是……后來我們看到了的行和列的雙重for回圈range。然后代碼將每個單元格的值放入 ( List<string>) 中myList。這將起作用,但似乎沒有必要。即使您想在所有單元格中搜索一個單詞,也無需將單元格值放入串列中……您已經在range變數中擁有了單元格!換句話說,不是將單詞添加到串列中,而是回圈range并搜索它。
當我說你有range作業時,這意味著Excel.Range物件。而且您已經知道您可以輕松地回圈遍歷該range物件,正如您的代碼所展示的那樣……但是……如果range很大,那么,您會注意到顯著的性能損失。底線是“回圈”通過Excel.Range'es 既昂貴又緩慢。
基本上我們想最小化任何回圈通過range. 一種可能的解決方案是將range轉換為兩 (2) 維Object陣列。然后,執行Object陣列中的所有回圈,這應該會更快,并且使代碼更易于閱讀。
因此,這行代碼……
range = worksheet.UsedRange;
可能最終看起來像……
range = worksheet.UsedRange;
object[,] worksheetValues = (object[,])range.Cells.Value;
現在,我們可以遍歷worksheetValues陣列而不是陣列range本身。
這“應該”替換mylist變數,因為它不需要……所有值都在worksheetValues陣列中。
現在我們有一個簡單的物件陣列來“搜索”……那么我們只需要一個搜索詞來查找。如果我們從用戶那里獲得搜索詞,那么我們可以創建一個方法,該方法接受該搜索詞和要搜索的陣列,并回傳搜索詞匹配的單元格的所有 X 和 Y 坐標。這種方法可能看起來像……
private static List<Point> FindWordInArray(string target, object[,] data) {
List<Point> foundIndexes = new List<Point>();
for (int row = 1; row <= data.GetLength(0); row ) {
for (int col = 1; col <= data.GetLength(1); col ) {
if (data[row, col] != null) {
if (data[row, col].ToString().Equals(target)) {
foundIndexes.Add(new Point { X = col, Y = row });
}
}
}
}
return foundIndexes;
}
public class Point {
public int X { get; set; }
public int Y { get; set; }
}
我知道Point該類可能不需要,只是為了簡化示例。
下面是上述內容的一個小而完整的示例。這包括設定try/catch/finally代碼以及釋放 COM 物件。
注意:Excel 從 1 開始陣列索引。因此物件陣列worksheetValues將從索引 1 開始。
Excel.Application ExcelApp = null;
Excel.Workbook workbook = null;
Excel.Worksheet worksheet= null;
Excel.Range range = null;
string searchWord = "";
try {
ExcelApp = new Excel.Application();
workbook = ExcelApp.Workbooks.Open(@"D:\Map", 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
worksheet = (Excel.Worksheet)workbook.Worksheets.get_Item(1);
range = worksheet.UsedRange;
object[,] worksheetValues = (object[,])range.Cells.Value;
Console.WriteLine("There are " worksheetValues.GetLength(0) " Rows and " worksheetValues.GetLength(1) " Columns");
for (int row = 1; row <= worksheetValues.GetLength(0); row ) {
Console.Write("Row " row ": ");
for (int col = 1; col <= worksheetValues.GetLength(1); col ) {
if (worksheetValues[row, col] != null) {
Console.Write(worksheetValues[row, col]);
}
else {
Console.Write(" - ");
}
}
Console.WriteLine();
}
Console.WriteLine("Type the word you want to search for...");
searchWord = Console.ReadLine().Trim();
List<Point> foundItems = FindWordInArray(searchWord, worksheetValues);
foreach (Point point in foundItems) {
Console.WriteLine("Found at Row: " point.Y " - Col: " point.X);
}
}
catch (Exception ex) {
Console.WriteLine("Excel error: " ex.Message);
}
finally {
if (range != null) {
Marshal.ReleaseComObject(range);
}
if (worksheet != null) {
Marshal.ReleaseComObject(worksheet);
}
if (workbook != null) {
workbook.Close(true, null, null);
Marshal.ReleaseComObject(workbook);
}
if (ExcelApp != null) {
ExcelApp.Quit();
Marshal.ReleaseComObject(ExcelApp);
}
}
我希望這是有道理的并有所幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/333935.html
上一篇:多個小數的2個數字的數學比較
