[英]copying of specific range of excel cells from one worksheet to another worksheet
[英]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.