我如何找到一个关键字的范围内的excel在c#

本文关键字:关键字 范围内 excel 一个 何找 | 更新日期: 2023-09-27 18:03:33

我有一个excel文件,我想从A1到A27搜索,找出哪个单元格有关键字'currency'。

我该怎么做?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using hExcel = Microsoft.Office.Interop.Excel;
namespace excel004
{
   class Program
   {
        static void Main(string[] args)
        {
            string xls_path = "c:''sample.xls";
            hExcel.Application xlsApp = new hExcel.ApplicationClass();
            hExcel.Workbook xlsWorkbook = xlsApp.Workbooks.Open(
                xls_path,   //file name
                0,          //UpdateLinks
                true,       //read-only
                5,          //Format
                "",         //password
                "",         //write password,
                true,       //IgnoreReadyOnblyReconnected
                Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, //Origin
                "'t",       //Delimiter
                false,      //Editable
                false,      //Notify
                0,          //Converter
                true,       //AddToMru,
                1,          //Local
                0           //CorruptLoad
            );
            hExcel.Worksheet xlsWorksheet = (hExcel.Worksheet)xlsWorkbook.Worksheets.get_Item(1);

            // Output Worksheet properties
            hExcel.Range xlsRange = xlsWorksheet.UsedRange;
            int columns = xlsRange.Columns.Count;
            int rows = xlsRange.Cells.Rows.Count;
            Console.Write("[info]Worksheet columns: "+columns+"'n");
            Console.Write("[info]worksheet rows: " + rows + "'n");
            // Find Keyword: Currency
            //(1)set range
            // I stopped here, coz I don't how to search from A1 ~ A27 with keyword:currency
              Console.Write(xlsWorksheet.get_Range("A1", "A27").Value2.ToString()); //get nothing 
           // my excel file is Row:100 * Column:20.
           // if the way have to look up all the cells,
           // i will give up to ask this question, 
           // I think it should be a way just look up one column that's A1 to A27.
           // [Resolved 1:] Answer from Francesco Baruchelli
            hExcel.Range titleRange = xlsWorksheet.get_Range("A1","A27");  //it works
           // [Resolved 2:] it works, but not good, coz it get too many unused value
            hExcel.Range titleRange = (hExcel.Range)xlsWorksheet.Columns.get_Item(1,Type.Missing);  
           // why this is wrong? 
           hExcel.Range titleRange = (hExcel.Range)xlsWorksheet.Columns.get_Item(1,27); 
            Console.ReadLine();
        }
    }
}

我如何找到一个关键字的范围内的excel在c#

您可以通过以下方式获得您正在寻找的范围:

xlsWorksheet.Range("A1:A27")

,则可以使用如下所示的Find方法:

http://msdn.microsoft.com/en-us/library/e4x1k99a (v = vs.80) . aspx

在您的代码中,可以这样应用(如果您正在查找的字符串只出现一次):

Excel.Range firstFind = null; 
currentFind = titleRange.Find("apples", missing,
    Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, 
    Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false,
    missing, missing); 
if (currentFind != null)
    String cellAddress = currentFind.get_Address(false, false, Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1, false, System.Reflection.Missing.Value);