vba 从 Excel API C# 获取单元格值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14344442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:11:14  来源:igfitidea点击:

Getting Cell Values from Excel API C#

c#excelapiexcel-vbavba

提问by Ramie

I have a ribbon that i made for Excel along with a cs File to perform as the core of the add-in. When a button is clicked on the CustomRibbon I want to parse the data values for one of the rows.

我有一个我为 Excel 制作的功能区和一个 cs 文件,用作加载项的核心。当在 CustomRibbon 上单击按钮时,我想解析其中一行的数据值。

    private void button1_Click(object sender, RibbonControlEventArgs e)
    {
        Console.WriteLine(Globals.ThisAddIn.Application.Cells[0, 0]);

    }

But that gives me an error

但这给了我一个错误

Exception from HRESULT: 0x800A03EC

HRESULT 异常:0x800A03EC

All i want to do is to be able to parse the data from cells on my worksheet and write data to the worksheet. Even on the startup of my program when i do:

我想要做的就是能够解析工作表上单元格中的数据并将数据写入工作表。即使在我的程序启动时,我也这样做:

        sheet.Cells[0,0]= "hello";

It gives me an error as well.

它也给我一个错误。

回答by B L

The "lowest" applicable index for referring to a cell in an Excel sheet is [1,1], which corresponds to Column A, Cell 1. I see you're trying to refer to 0,0 which is out of the bounds of the table.

用于引用 Excel 工作表中单元格的“最低”适用索引是 [1,1],它对应于 A 列,单元格 1。我看到您正在尝试引用超出范围的 0,0桌子。

Though C# typically utilizes zero based table indexing, it appears the programmers of Excel Interop adhered to a different methodology. That or they wanted to keep their conventions uniform, since the first row in an Excel table starts with 1, and not 0. I'm not an Excel buff, but that's my best guess.

尽管 C# 通常使用基于零的表索引,但 Excel Interop 的程序员似乎遵循了不同的方法。那或者他们想保持他们的约定一致,因为 Excel 表中的第一行以 1 开始,而不是 0。我不是 Excel 爱好者,但这是我最好的猜测。

Edit: Per Siddharth's request, here's an example that copies the contents of a DataGridView control to an Excel sheet. Note that this just demonstrates basic functionality, and isn't a complete example or necessarily best practice:

编辑:根据 Siddharth 的要求,这是一个将 DataGridView 控件的内容复制到 Excel 工作表的示例。请注意,这只是演示基本功能,而不是完整的示例或必要的最佳实践:

#using Microsoft.Office.Interop;

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;

xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(1);
xlWorkSheet = Excel.Worksheet xlWorkBook.ActiveSheet;

// Notice the index starting at [1,1] (row,column)
for (int i=1; i<DataGridView.Columns.Count+1; i++)
    xlWorkSheet.Cells[1, i] = DataGridView.Columns[i - 1].HeaderText;

for each(DataGridViewRow row in DataGridView.Rows)
{
    for each (DataGridViewColumn column in DataGridView.Columns)
    {
        xlWorkSheet.Cells[row.Index+2,column.Index+1] = DataGridView.Rows[row.Index].Cells[column.Index].Value;
    }
}

Notice the disparities between the indexing. This example first copies the headers from the DataGridView, then offsets the position in the Excel sheet to copy the rest of the data since the column headers don't count as index-able cells in the DataGrid. This would probably also work for DataTables.

注意索引之间的差异。此示例首先从 DataGridView 复制标题,然后偏移 Excel 工作表中的位置以复制其余数据,因为列标题不计为 DataGrid 中可索引的单元格。这可能也适用于数据表。

回答by Porkbutts

Try using the Value2field of the cell.

尝试使用单元格的Value2字段。

sheet.Cells[1,1].Value2 = "hello";

Also in this line

也在这一行

Console.WriteLine(Globals.ThisAddIn.Application.Cells[1, 1]);

The "Cells" field is a property of a worksheet. You can't use it on the Excel application; you need to first create or open a Workbookand then create or use an existing Worksheet.

“单元格”字段是工作表的一个属性。您不能在 Excel 应用程序上使用它;您需要先创建或打开Workbook,然后创建或使用现有的Worksheet

EDIT: Forgot that Excel ranges are 1-index and not 0-index.

编辑:忘记 Excel 范围是 1-index 而不是 0-index。