繁体   English   中英

从特定关键字下方复制特定范围的excel单元格

[英]Copying specific range of excel cells from below a specific keyword

我想编写一个 C# 程序来复制特定关键字下方的特定范围的单元格。 该代码将识别 Excel 中的关键字,然后复制关键字下方所有单元格的值以复制到另一个范围。

我正在使用 Aspose。 我试图编写代码来查找关键字,并且可以成功返回关键字所在的单元格。我试图弄清楚如何做是将任何关键字下方的范围复制到另一个范围中。 我可以成功地将一个范围复制到另一个范围,但不能从特定关键字下方进行。

    Cells cellsOne = worksheet.Cells;
    FindOptions findOptions = new FindOptions();
    findOptions.LookAtType = LookAtType.StartWith;
    Cell cell = cellsOne.Find("Accounting", null, findOptions);
    //Printing the name of the cell found after searching worksheet
    Console.WriteLine("Name of the cell containing String: " + cell.Name);
    //if cell is found/value is returned
    if (cell.Name.Contains("Accounting"))
    {
         //return cell value ?
         //copy all below values (will need the cell keyword is in to do that)
         //paste below values into specific columns
         //doing it manually
         Aspose.Cells.Range range1 = cellsOne.CreateRange("A2:A10"); 
         Aspose.Cells.Range range2 = cellsOne.CreateRange("B28:B34"); 
         range1.Copy(range2);
    }

我访问过 Aspose 网站,但正在努力复制特定关键字下方的范围。 谢谢你。

我不知道如何在 C# 中执行此操作,但我建议您找到具有该值的单元格,然后在下面的“范围”语句中,您可以从之前找到的那个单元格开始。

例如,Cell cell = cellsOne.Find("Accounting", null, findOptions).Row;

例如,您得到“320”,然后您的下一行转到该 320。使用您的代码:

  Aspose.Cells.Range range1 = cellsOne.CreateRange("A2:A10"); 
  Aspose.Cells.Range range2 = cellsOne.CreateRange("B" & cell & ":B34"); 
  range1.Copy(range2);`

这就是我在 VBA 中所做的。

希望能帮助到你!

稍微检查一下您的代码段,我认为您的目标范围是“A2:A10”。 您的代码需要一些调整。 请参阅带有注释的更新(完整)示例代码以完成您的任务以供参考。 我使用 CellsHelper 静态类动态评估了源范围(在搜索的关键字下方)。
例如

示例代码:

Workbook workbook = new Workbook("e:\\test2\\Book1.xlsx");
Worksheet worksheet = workbook.Worksheets[0];
Cells cellsOne = worksheet.Cells;
FindOptions findOptions = new FindOptions();
findOptions.LookAtType = LookAtType.StartWith;
Cell cell = cellsOne.Find("Accounting", null, findOptions);
//Printing the name of the cell found after searching worksheet
Console.WriteLine("Name of the cell containing String: " + cell.Name);
//if cell is found/value is returned
if (cell != null)
{
   //I thought this is your destination range 
   Aspose.Cells.Range range1 = cellsOne.CreateRange("A2:A10");

   //Evaluate the the next cell after (found) cell's row and column indices.
   int startRow = cell.Row +1; //we add "1" to get the next in the same column
   int startCol = cell.Column;
   string startCell = CellsHelper.CellIndexToName(startRow, startCol);
   //Set and evaluate your end cell for the range.
   string endCell = CellsHelper.CellIndexToName(startRow + 6, startCol);
   //Create your dynamic source range based on your startCell and endCell values
   Aspose.Cells.Range range2 = cellsOne.CreateRange(startCell, endCell);
   
   //Copy the source range to destination range
   range1.Copy(range2);

}

workbook.Save("e:\\test2\\out1.xlsx");

希望这个对你有帮助。

您还可以查看有关 复制范围的文档以供进一步参考。

PS。 我在 Aspose 担任支持开发人员/传播者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM