نسخ الصفوف والأعمدة

مقدمة

في بعض الأحيان, قد تحتاج إلى نسخ الصفوف والأعمدة في ورقة العمل دون نسخ الورقة بأكملها. مع Aspose.Cells, من الممكن نسخ الصفوف والأعمدة داخل المصنف أو بين المصنفات. عند نسخ صف (أو عمود), يتم نسخ البيانات الموجودة فيه, بما في ذلك الصيغ - بالمراجع المحدثة - والقيم, التعليقات, التنسيق, الخلايا المخفية, الصور, وغيرها من الكائنات التوضيحية.

كيفية نسخ الصفوف والأعمدة باستخدام Microsoft Excel

  1. حدد الصف أو العمود الذي ترغب في نسخه.
  2. لنسخ الصفوف أو الأعمدة, انقر نسخ على شريط الأدوات قياسي, أو اضغط CTRL+C.
  3. حدد صفًا أو عمودًا أسفل أو إلى اليمين من المكان الذي تريد نسخ تحديدك.
  4. عند نسخ الصفوف أو الأعمدة, انقر الخلايا المنسوخة على قائمة إدراج.

كيفية لصق الصفوف والأعمدة باستخدام خيارات اللصق مع Microsoft Excel

  1. حدد الخلايا التي تحتوي على البيانات أو السمات الأخرى التي تريد نسخها.
  2. في علامة التبويب الرئيسية, انقر نسخ.
  3. انقر على الخلية الأولى في المنطقة التي ترغب في لصق ما نسخته.
  4. على علامة التبويب الرئيسية، انقر فوق السهم المجاور لـ لصق, ثم حدد لصق خاص.
  5. حدد الخيارات التي تريدها.

كيفية نسخ الصفوف والأعمدة باستخدام Aspose.Cells for .NET

كيفية نسخ صفوف فردية

توفر Aspose.Cells الطريقة CopyRow في الفئة Cells. تقوم هذه الطريقة بنسخ جميع أنواع البيانات بما في ذلك الصيغ والقيم والتعليقات وتنسيقات الخلايا والخلايا المخفية والصور وغيرها من العناصر الرسومية من الصف المصدر إلى الصف الوجهة.

تأخذ الطريقة CopyRow المعلمات التالية:

-كائن المصدر Cells.

  • فهرس الصف المصدر، و
  • فهرس الصف الوجهة.

استخدم هذه الطريقة لنسخ صف داخل ورقة العمل، أو إلى ورقة عمل أخرى. تعمل الطريقة CopyRow بنفس الطريقة كبرنامج Microsoft Excel. لذا، على سبيل المثال، لا تحتاج إلى ضبط ارتفاع الصف الوجهة بشكل صريح، تم نسخ تلك القيمة أيضًا.

المثال التالي يوضح كيفية نسخ صف في ورقة العمل. يستخدم قالب ملف Microsoft Excel وينسخ الصف الثاني (مع البيانات والتنسيق والتعليقات والصور وما إلى ذلك) وينسخه إلى الصف الثاني عشر في نفس ورقة العمل.

يمكنك تخطي الخطوة التي تحصل من خلالها على ارتفاع الصف المصدر باستخدام الطريقة Cells.GetRowHeight ومن ثم تحديد ارتفاع الصف الوجهة باستخدام الطريقة Cells.SetRowHeight حيث تعتني الطريقة CopyRow تلقائيًا بارتفاع الصف.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Open the existing excel file.
Workbook excelWorkbook1 = new Workbook(dataDir + "book1.xls");
// Get the first worksheet in the workbook.
Worksheet wsTemplate = excelWorkbook1.Worksheets[0];
// Copy the second row with data, formattings, images and drawing objects
// To the 16th row in the worksheet.
wsTemplate.Cells.CopyRow(wsTemplate.Cells, 1, 15);
// Save the excel file.
excelWorkbook1.Save(dataDir + "output.xls");

كيفية نسخ عدة صفوف

يمكنك أيضًا نسخ صفوف متعددة على وجهة وجديدة أثناء استخدام طريقة Cells.CopyRows التي تأخذ معلمة إضافية من النوع صحيح لتحديد عدد الصفوف المصدر التي سيتم نسخها.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create an instance of Workbook class by loading the existing spreadsheet
Workbook workbook = new Workbook(dataDir + "aspose-sample.xlsx");
// Get the cells collection of worksheet by name Rows
Cells cells = workbook.Worksheets["Rows"].Cells;
// Copy the first 3 rows to 7th row
cells.CopyRows(cells, 0, 6, 3);
// Save the result on disc
workbook.Save(dataDir + "output_out.xlsx");

كيفية نسخ الأعمدة

توفر Aspose.Cells الطريقة CopyColumn في الفئة Cells، تقوم هذه الطريقة بنسخ جميع أنواع البيانات بما في ذلك الصيغ - مع الإشارات المحدثة - والقيم والتعليقات وتنسيقات الخلايا والخلايا المخفية والصور وغيرها من العناصر الرسومية من العمود المصدر إلى العمود الوجهة.

تأخذ الطريقة CopyColumn المعلمات التالية:

-كائن المصدر Cells.

  • فهرس العمود المصدر، و
  • فهرس العمود الوجهة.

استخدم CopyColumn لنسخ عمود داخل صفحة أو إلى صفحة أخرى.

هذا المثال ينسخ عمودًا من ورقة العمل ويلصقه في ورقة عمل في دفتر عمل آخر.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create another Workbook.
Workbook excelWorkbook1 = new Workbook(dataDir + "book1.xls");
// Get the first worksheet in the book.
Worksheet ws1 = excelWorkbook1.Worksheets[0];
// Copy the first column from the first worksheet of the first workbook into
// The first worksheet of the second workbook.
ws1.Cells.CopyColumn(ws1.Cells, ws1.Cells.Columns[0].Index, ws1.Cells.Columns[2].Index);
// Autofit the column.
ws1.AutoFitColumn(2);
// Save the excel file.
excelWorkbook1.Save(dataDir + "output.xls");

كيفية نسخ عمود متعدد

على غرار الطريقة Cells.CopyRows، توفر واجهات برمجة التطبيقات لـ Aspose.Cells أيضًا الطريقة Cells.CopyColumns لنسخ عدة أعمدة مصدر إلى موقع جديد.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create an instance of Workbook class by loading the existing spreadsheet
Workbook workbook = new Workbook(dataDir + "aspose-sample.xlsx");
// Get the cells collection of worksheet by name Columns
Cells cells = workbook.Worksheets["Columns"].Cells;
// Copy the first 3 columns 7th column
cells.CopyColumns(cells, 0, 6, 3);
// Save the result on disc
workbook.Save(dataDir + "output_out.xlsx");

كيفية لصق الصفوف والأعمدة مع خيارات اللصق

توفر Aspose.Cells الآن PasteOptions أثناء استخدام الوظائف CopyRows و CopyColumns. يسمح بضبط خيار اللصق المناسب بشكل مماثل لبرنامج Excel.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Load sample excel file
Workbook wb = new Workbook(sourceDir + "sampleChangeChartDataSource.xlsx");
// Access the first sheet which contains chart
Worksheet source = wb.Worksheets[0];
// Add another sheet named DestSheet
Worksheet destination = wb.Worksheets.Add("DestSheet");
// Set CopyOptions.ReferToDestinationSheet to true
CopyOptions options = new CopyOptions();
options.ReferToDestinationSheet = true;
// Set PasteOptions
PasteOptions pasteOptions = new PasteOptions();
pasteOptions.PasteType = PasteType.Values;
pasteOptions.OnlyVisibleCells = true;
// Copy all the rows of source worksheet to destination worksheet which includes chart as well
// The chart data source will now refer to DestSheet
destination.Cells.CopyRows(source.Cells, 0, 0, source.Cells.MaxDisplayRange.RowCount, options, pasteOptions);
// Save workbook in xlsx format
wb.Save(outputDir + "outputChangeChartDataSource.xlsx", SaveFormat.Xlsx);