gpt4 book ai didi

c# - 如何将值设置为二维 Excel 范围?

转载 作者:行者123 更新时间:2023-12-04 22:07:45 25 4
gpt4 key购买 nike

我需要从特定格式的测试用例列表中构建一个 excel 表,以便将其上传到服务器。
我很难在文件中填充“预期”和“实际”的二维范围。
我使用相同的方法来填充标题,它是一个一维数组,以及步骤(它是二维的)。

流程是:

  • 取消对 TestCase 范围的资助(一些标题 + 步骤)。假设:第一次迭代的 A1 到 E14。
  • 删除标题的 testCase 范围内的子(本地)范围(例如:A1 到 C1)。
  • 在标题的 testCase 范围内删除另一个子(本地)范围(在我的情况下:D1 到 E14)。
  • 使用测试用例值(标题和步骤)填充两个子范围。
  • 重复使用相同的本地范围(步骤 2-3)取消下一个电子表格范围(在我的情况下为 A14 到 E28),然后填充它们,依此类推...

  • 源值是一个字典,它表示测试用例的步骤(键 = 预期和值 = 实际)。

    这是我使用的代码:
    public class TestCase
    {
    public Dictionary<string, string> steps;
    }

    Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
    Workbooks workBooks = excelApp.Workbooks;
    Workbook workbook = workBooks.Add(XlWBATemplate.xlWBATWorksheet);
    Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
    excelApp.Visible = true;

    foreach (TestCase testCase in TestCaseList.Items.GetList())
    {
    Range worksheetRange = GetRangeForTestCase(worksheet);
    //The step above is equivalent to:
    // Range worksheetRange = worksheet.get_Range("A1", "E14");
    //for the first foreach iteration.

    Range stepsRange = worksheetRange.get_Range("D1", "E14");
    //This is a local range within the worksheetRange, not the worksheet,
    //so it is always have to be between D1 to E14 for a 14th steps test case.
    //Anyway, it should work at least for the 1st iteration.

    //for test evaluation only. All cells between D1 to E14 are filled with "ccc"
    test.Value = "ccc";

    //This list of pairs which are converted to Array[,] is about to be converted to a two dimensional array
    list = new List<object>();
    foreach (KeyValuePair<string, string> item in testCase.Steps)
    {
    //Here I build the inside Array[,]
    object[] tempArgs = {item.Key, item.Value};
    list.Add(tempArgs);
    }
    object[] args = { Type.Missing, list.ToArray() };

    test.GetType().InvokeMember("Value", System.Reflection.BindingFlags.SetProperty, null, test, args);

    //Now, all the "ccc" within the Excel worksheet are disapeared, so the Range is correct, but the value of args[] are not set!!
    }

    运行此代码的实际结果是范围已定义(可能正确)但其值设置为 null,
    虽然 - 我可以在运行时看到 args 数组中的正确值。
    我还尝试设置更大的范围并使用 range.Value = "Pake value"填充它,并看到在运行我的代码之后,正确的步骤范围变为空白!
    所以,范围是正确的,数组中填充了我的值,InvokeMember 方法被正确调用:)
    但是,所有值都设置为 null..

    帮助...

    最佳答案

    可以通过以下方式之一设置一个单元格或 1dim 数组:

    Range range = SetRange();//Let's say range is set between A1 to D1
    object[] args = {1, 2, 3, 4 };

    //Directly
    range.Value = args;

    //By Reflection
    range.GetType().InvokeMember("Value", System.Reflection.BindingFlags.SetProperty, null, range, args);

    无法直接设置 2dim 数组,因此必须使用反射流来设置值矩阵。该矩阵必须在集合之前构建,如下所示:
    Range range = SetRange();//Let's say range is set between A1 to C5
    int rows = 5;
    int columns = 3;
    object[,] data = new object[rows, columns];
    for (int i = 0; i < rows; i++)
    {
    for (int j = 0; j < columns; j++)
    {
    //Here I build the inside Array[,]
    string uniqueValue = (i + j).ToString();
    data[i, j] = "Insert your string value here, e.g: " + uniqueValue;
    }
    }
    object[] args = { data };
    range.GetType().InvokeMember("Value", System.Reflection.BindingFlags.SetProperty, null, range, args);

    关于c# - 如何将值设置为二维 Excel 范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17062599/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com