简体   繁体   English

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

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

I am wanting to write a C# program to copy a specific range of cells below a specific keyword.我想编写一个 C# 程序来复制特定关键字下方的特定范围的单元格。 The code will identify a keyword in Excel and then copy the values of all the cells below the keyword to copy into another range.该代码将识别 Excel 中的关键字,然后复制关键字下方所有单元格的值以复制到另一个范围。

I am using Aspose.我正在使用 Aspose。 I attempted to write code to find the keyword and can successfully return the cell the keyword is located in. What I am trying to figure out how to do is copy the range specifically below any keyword into another range.我试图编写代码来查找关键字,并且可以成功返回关键字所在的单元格。我试图弄清楚如何做是将任何关键字下方的范围复制到另一个范围中。 I can successfully copy one range to another range but cannot do it from below a specific keyword.我可以成功地将一个范围复制到另一个范围,但不能从特定关键字下方进行。

    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);
    }

I have visited Aspose website but am struggling to copy a range BELOW a specific keyword.我访问过 Aspose 网站,但正在努力复制特定关键字下方的范围。 Thank you.谢谢你。

I don't know how to do it in C#, but I would advise you to find the cell which has the value, then in your "Range" statement below you could start at that cell you found before.我不知道如何在 C# 中执行此操作,但我建议您找到具有该值的单元格,然后在下面的“范围”语句中,您可以从之前找到的那个单元格开始。

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

You get for example "320", then your next line goes to that 320. Using your code:例如,您得到“320”,然后您的下一行转到该 320。使用您的代码:

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

That's what I would do in VBA.这就是我在 VBA 中所做的。

Hope it helps!希望能帮助到你!

Checking your code segment a bit, I thought your destination range is "A2:A10".稍微检查一下您的代码段,我认为您的目标范围是“A2:A10”。 Your code needs some tweaks.您的代码需要一些调整。 See the updated (complete) sample code with comments to accomplish your task for your reference.请参阅带有注释的更新(完整)示例代码以完成您的任务以供参考。 I evaluated the source range (below the searched keyword) dynamically using CellsHelper static class.我使用 CellsHelper 静态类动态评估了源范围(在搜索的关键字下方)。
eg例如

Sample code:示例代码:

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");

Hope, this helps a bit.希望这个对你有帮助。

You may also see the document on copying ranges for your further reference.您还可以查看有关 复制范围的文档以供进一步参考。

PS. PS。 I am working as Support developer/ Evangelist at Aspose.我在 Aspose 担任支持开发人员/传播者。

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

相关问题 将特定范围的 Excel 单元格从一个工作表复制到另一个工作表 - copying of specific range of excel cells from one worksheet to another worksheet 使用 VSTO 将一系列单元格从 excel 复制到 powerpoint - Copying a range of cells from excel into powerpoint using VSTO 如何使用C#将一个Excel工作簿中特定范围的单元格复制到另一工作簿中 - how to copy cells from a specific range from one excel workbook to another workbook using c# 循环浏览Excel工作簿,从每个工作簿复制一定范围的单元格并粘贴到主工作簿 - Looping through Excel workbooks, copying a range of cells from each and pasting to a master workbook 如何从DataGridView在特定的Excel单元格中插入值 - How to Insert Values in Specific Excel Cells From DataGridView 在Excel中从范围中删除单元格 - Remove cells from range in Excel 如何在Excel中仅读取具有特定范围内的值的单元格-使用VSTO C# - How to only read cells with values with in a specific range in Excel - using VSTO C# 如何将数据添加到列中特定范围的单元格 - how to add data to a specific range of cells in column 将单元格从Excel复制到Word时如何保持单元格的格式 - How to maintain formatting of cells when copying cells from Excel to Word Excel Interop:从特定工作簿获取命名表(范围) - Excel Interop: Get named table (Range) from specific workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM