【问题标题】:How do I get and compare the contents of a cell in an Excel spreadsheet by name?如何按名称获取和比较 Excel 电子表格中单元格的内容?
【发布时间】:2014-12-04 00:57:21
【问题描述】:

我正在尝试将一个单元格与一个字符串进行比较,如果它相等则替换它。但是当我尝试执行下面的代码时,会出现 0x800A03EC 错误。

                int cont = 0;
                string cell;
                do
                {
                    cont++;
                    cell = rCol.ToUpper() + cont.ToString(); // = "D1"
                    string cellData = ((Excel.Range)sheet.Cells[cell]).Value2.ToString();

                    if (cellData == from)
                    {
                        sheet.Cells[cell] = to;
                    }
                } while (sheet.Cells[cell] == null);

我该怎么做?

【问题讨论】:

  • 您是否尝试将Cells 更改为Range
  • 我试过了,没用

标签: c# excel office-interop


【解决方案1】:

如果您知道要检查的单元格,例如 A1,您可以这样做:

using System;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            // create app
            var excelApp = new Excel.Application();
            // open workbook
            var workbook = excelApp.Workbooks.Open(
                @"C:\Users\Home\Documents\Book1.xlsx",
                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Type.Missing, Type.Missing);
            // open sheet
            var sheet = (Excel.Worksheet)workbook.Sheets[1];

            // create some variables
            var from = "Pete";
            var to = "Dave";
            // compare cell A1 [1,1] with 'from'
            if (string.Equals(sheet.Cells[1,1].Value, from))
            {
                sheet.Cells[1, 1].Value = to;
            }

            // save the workbook
            workbook.Save();
            // close the workbook and release resources
            workbook.Close(true, workbook.Path);
            Marshal.ReleaseComObject(workbook);
            workbook = null;
        }
    }
}

【讨论】:

    【解决方案2】:

    试试这个得到一个简单的范围:

    int row = 1;
    string col = "D";        
    string text = sheet.get_Range(col + row.ToString()).Value;
    

    0x800A03EC 错误是 Excel 返回的值,表示 NAME_NOT_FOUND(请参阅this SA question)。看起来您传递的是 Excel 找不到的参数,可能是因为您传递的是字符串 ("D1"),而不是两个整数参数 (4,1)。

    如果不查看更多代码,就不可能知道从哪里获得 rCol.ToUpper() 值。但是,如果您尝试通过一系列列和行来检查相等条件(这就是您正在尝试的样子),您将很快遇到如何使用大写字母增加列值的讨厌问题(试试看吧;没什么好玩的!)。

    我最近在 VB 中做的一个解决方案是使用本机 Excel 函数索引,它使用数值来获取特定单元格。您需要转换 Excel.WorksheetFunction 类型的对象才能使用该函数。但后来我发现有比使用 Excel 函数更简单的解决方案:

    using System.Runtime.InteropServices;
    using Microsoft.Office.Interop.Excel;
    
    namespace exceltest
    {
    class Program
    {
    
        static void Main(string[] args)
        {
            Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook workbook = xl.Workbooks.Open(@"C:\test.xlsx");
            Microsoft.Office.Interop.Excel.Worksheet sheet = workbook.Sheets[1];
            xl.Visible = true;
    
            //use this if you want to use native Excel functions (such as index)
            Microsoft.Office.Interop.Excel.WorksheetFunction wsFunc = xl.WorksheetFunction;
    
            int maxNum = 100; // set maximum number of rows/columns to search
    
            string from = "blah";
            string to = "blum";
    
            //this is pretty slow, since it has to interact with 10,000 cells in Excel
            // just one example of how to access and set cell values           
            for (int col = 1; col <= maxNum; col++)
            {
                for (int row = 1; row <= maxNum; row ++)
                {
                    Range cell = (Range)sheet.Cells[row, col];
                    if ((string)cell.Value == from) //cast to string to avoid null reference exceptions
                    {
                        cell.Value = to;
                    }
                }
            }
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-08
      • 2017-07-21
      • 2017-06-09
      • 1970-01-01
      • 2016-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多