首页 > 解决方案 > 在 C# 中使用 VSTO 将类列表绑定到 Excel 工作表

问题描述

我正在使用包含一些数据的类列表,我需要使用 VSTO 绑定到 c# 中的 excel 表。我在 VS2019 中添加了 VSTO 包。

标签: c#excelvsto

解决方案


您可以使用Worksheet.Range属性,该属性返回一个Range对象,该对象表示一个单元格或一系列单元格。范围对象表示一个单元格、一行、一列、包含一个或多个连续单元格块的单元格选择或 3-D 范围。

以下代码示例演示了使用 Range 属性访问单个单元格或多个单元格的不同方法:

private void CompareRangeUsage()
{                      
    Worksheet vstoWorksheet = Globals.Factory.GetVstoObject(
        this.Application.ActiveWorkbook.Worksheets[1]);
    // The following line of code specifies a single cell.
    vstoWorksheet.Range["A1"].Value2 = "Range 1";

    // The following line of code specifies multiple cells.
    vstoWorksheet.Range["A3", "B4"].Value2 = "Range 2";

    // The following line of code uses an Excel.Range for 
    // the second parameter of the Range property.
    Excel.Range range1 = vstoWorksheet.Range["C8"];
    vstoWorksheet.Range["A6", range1].Value2 = "Range 3";
}

推荐阅读