Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c中访问Excel工作表单元格#_C#_Excel - Fatal编程技术网

C# 如何在c中访问Excel工作表单元格#

C# 如何在c中访问Excel工作表单元格#,c#,excel,C#,Excel,我正在使用C#中的Microsoft.Office.Interop.Excelnamespace访问Excel工作表 我在访问工作表中的特定单元格时遇到了一些问题 这就是我到目前为止所做的: using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Windows; using System.Windows.Forms; using System.Windows

我正在使用C#中的
Microsoft.Office.Interop.Excel
namespace访问Excel工作表

我在访问工作表中的特定单元格时遇到了一些问题

这就是我到目前为止所做的:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input.Manipulations;
using Application = Microsoft.Office.Interop.Excel.Application;




namespace excelProgram
{
    class Program
    {
        [STAThreadAttribute]
        public static void Main(string[] args)
        {
            Program p = new Program();
            p.openWorkSheet();
            Console.ReadKey(true);
        }


        private void openWorkSheet()
        {
            var xl= new Application();
            xl.Visible = true;
            xl.Workbooks.Open(@"C:\Documents and Settings\user\Desktop\Book1.xls");

        }


    }
}
我创建了一个
Application()
对象,它可以在我的电脑上打开一个Excel工作簿。这很好,但是我不知道如何访问工作簿中工作表中的特定单元格,例如单元格B1


有人能帮忙吗?

先参考特定的工作表,然后参考特定的范围,例如这样

(示例使用对Excel 2007 PIA的引用:C:\Windows\assembly\GAC\Microsoft.Office.Interop.Excel\12.0.0.0\uuu 71e9bce111e9429c\Microsoft.Office.Interop.Excel.dll)


xl.Workbooks.Open(…)命令是否返回工作簿?@Gwny是的,它打开实际的Excel工作簿。例如,您可以尝试通过Range:Range aRange=Workbook.get_Range(“C1”、“C7”)访问它;除了我必须显式地将工作表类型转换为
(工作表工作表=(工作表)工作簿之外,这是有效的否则会出现编译器错误。
using Microsoft.Office.Interop.Excel;
using Application = Microsoft.Office.Interop.Excel.Application;
using Excel = Microsoft.Office.Interop.Excel;

Application excelApplication = new Excel.Application
{
    Visible = true,
    ScreenUpdating = true
};

_Workbook workbook = excelApplication.Workbooks.Open(@"C:\Temp\Book1.xlsx");
_Worksheet sheet = workbook.Worksheets[1];
Range range = sheet.Range["B1"];
range.Formula = "Test";

excelApplication.Quit();