نسخ الصفوف والأعمدة
مقدمة
في بعض الأحيان ، تحتاج إلى نسخ الصفوف والأعمدة في ورقة العمل دون نسخ ورقة العمل بأكملها. باستخدام Aspose.Cells ، يمكن نسخ الصفوف والأعمدة داخل أو بين المصنفات.
عند نسخ صف (أو عمود) ، يتم أيضًا نسخ البيانات الموجودة فيه ، بما في ذلك الصيغ - مع المراجع المحدثة - والقيم والتعليقات والتنسيق والخلايا المخفية والصور والكائنات الرسومية الأخرى.
نسخ الصفوف والأعمدة باستخدام Microsoft Excel
- حدد الصف أو العمود الذي تريد نسخه.
- لنسخ الصفوف أو الأعمدة ، انقر فوقينسخ على المعيار شريط الأدوات ، أو اضغط علىكنترول+ ** ج **.
- حدد صفًا أو عمودًا أدناه أو على يمين المكان الذي تريد نسخ تحديدك إليه.
- عندما تقوم بنسخ صفوف أو أعمدة ، انقر فوقمنسوخة Cells على الإدراج قائمة.
نسخ صف واحد
يوفر Aspose.Cells ملفcopyRow طريقة الCellsصف دراسي. تنسخ هذه الطريقة جميع أنواع البيانات بما في ذلك الصيغ والقيم والتعليقات وتنسيقات الخلايا والخلايا المخفية والصور والكائنات الرسومية الأخرى من صف المصدر إلى صف الوجهة.
الcopyRow تأخذ الطريقة المعلمات التالية:
- المصدرCellsموضوع،
- فهرس صف المصدر و
- فهرس صف الوجهة.
استخدم هذه الطريقة لنسخ صف داخل ورقة أو إلى ورقة أخرى. الcopyRow تعمل بطريقة مشابهة لـ Microsoft Excel. لذلك ، على سبيل المثال ، لا تحتاج إلى تعيين ارتفاع صف الوجهة بشكل صريح ، حيث يتم نسخ هذه القيمة أيضًا.
يوضح المثال التالي كيفية نسخ صف في ورقة عمل. يستخدم قالب Microsoft ملف Excel ويقوم بنسخ الصف الثاني (كاملاً بالبيانات والتنسيق والتعليقات والصور وما إلى ذلك) ولصقه في الصف الثاني عشر في نفس ورقة العمل.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(CopyingRows.class) + "rows_cloumns/"; | |
// Create a new Workbook. | |
Workbook excelWorkbook = new Workbook(dataDir + "book1.xls"); | |
// Get the first worksheet in the workbook. | |
Worksheet wsTemplate = excelWorkbook.getWorksheets().get(0); | |
// Copy the second row with data, formating, images and drawing objects to the 12th row in the worksheet. | |
wsTemplate.getCells().copyRow(wsTemplate.getCells(), 2, 10); | |
// Save the excel file. | |
excelWorkbook.save(dataDir + "CopyingRows_out.xls"); | |
// Print message | |
System.out.println("Row and Column copied successfully."); |
يتم إنشاء الإخراج التالي عند تنفيذ الكود أدناه.
يتم نسخ الصف بأعلى درجات الدقة والدقة
عند نسخ الصفوف ، من المهم ملاحظة الصور أو المخططات أو الكائنات الرسومية الأخرى ذات الصلة لأن هذا هو نفسه مع Microsoft Excel:
- إذا كان فهرس صف المصدر هو 5 ، يتم نسخ الصورة والرسم البياني وما إلى ذلك إذا كانت موجودة في الصفوف الثلاثة (مؤشر صف البداية هو 4 ومؤشر صف النهاية هو 6).
- لن تتم إزالة الصور والمخططات الموجودة وما إلى ذلك في صف الوجهة.
نسخ صفوف متعددة
يمكنك أيضًا نسخ صفوف متعددة إلى وجهة جديدة أثناء استخدام ملفCells.copyRows الطريقة التي تأخذ معلمة إضافية من النوع الصحيح لتحديد عدد صفوف المصدر المراد نسخها.
يوجد أدناه لقطة لجدول بيانات الإدخال الذي يحتوي على 3 صفوف من البيانات بينما ينسخ مقتطف الشفرة المقدم أدناه جميع الصفوف الثلاثة إلى موقع جديد بدءًا من الصف السابع.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(CopyingMultipleRows.class); | |
// 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.getWorksheets().get("Rows").getCells(); | |
// Copy the first 3 rows to 7th row | |
cells.copyRows(cells, 0, 6, 3); | |
// Save the result on disc | |
workbook.save(dataDir + "output.xlsx"); |
فيما يلي عرض جدول البيانات الناتج بعد تنفيذ مقتطف الشفرة أعلاه.
نسخ عمود واحد
يوفر Aspose.Cells ملفنسخ العمود طريقة الCellsclass ، هذه الطريقة تنسخ جميع أنواع البيانات ، بما في ذلك الصيغ - مع المراجع المحدثة - والقيم والتعليقات وتنسيقات الخلايا والخلايا المخفية والصور وكائنات الرسم الأخرى من العمود المصدر إلى عمود الوجهة.
النسخ العمود تأخذ الطريقة المعلمات التالية:
- المصدرCellsموضوع،
- فهرس عمود المصدر و
- فهرس عمود الوجهة.
استخدم النسخ العمود طريقة لنسخ عمود داخل ورقة أو إلى ورقة أخرى.
يقوم هذا المثال بنسخ عمود من ورقة عمل ولصقه في ورقة عمل في مصنف آخر.
يتم نسخ عمود من مصنف إلى آخر
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(CopyingColumns.class) + "rows_cloumns/"; | |
// Create a new Workbook. | |
Workbook excelWorkbook = new Workbook(dataDir + "book1.xls"); | |
// Get the first worksheet in the workbook. | |
Worksheet wsTemplate = excelWorkbook.getWorksheets().get(0); | |
// Copy the first column from the first worksheet of the first workbook into the first worksheet of the second workbook. | |
wsTemplate.getCells().copyColumn(wsTemplate.getCells(), 1, 4); | |
// Save the excel file. | |
excelWorkbook.save(dataDir + "CopyingColumns_out.xls"); | |
// Print message | |
System.out.println("Row and Column copied successfully."); |
نسخ أعمدة متعددة
مشابه لCells.copyRows ، فإن واجهات برمجة التطبيقات Aspose.Cells توفر أيضًا امتدادCells.copyColumns لنسخ أعمدة مصدر متعددة إلى موقع جديد.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(CopyingMultipleColumns.class); | |
// 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.getWorksheets().get("Columns").getCells(); | |
// Copy the first 3 columns 7th column | |
cells.copyColumns(cells, 0, 6, 3); | |
// Save the result on disc | |
workbook.save(dataDir + "output.xlsx"); |
إليك كيفية ظهور جداول البيانات المصدر والنتيجة في Excel.
لصق الصفوف / الأعمدة بخيارات اللصق
Aspose.Cells يوفر الآنخيارات لصق أثناء استخدام الوظائفCopyRows وCopyColumns. يسمح بتعيين خيارات لصق مناسبة مشابهة لبرنامج Excel.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Load some excel file | |
Workbook wb = new Workbook("book1.xlsx"); | |
// Access the first sheet which contains chart | |
Worksheet source = wb.getWorksheets().get(0); | |
// Add another sheet named DestSheet | |
Worksheet destination = wb.getWorksheets().add("DestSheet"); | |
// Set CopyOptions.ReferToDestinationSheet to true | |
CopyOptions options = new CopyOptions(); | |
options.setReferToDestinationSheet(true); | |
// Set PasteOptions | |
PasteOptions pasteOptions = new PasteOptions(); | |
pasteOptions.setPasteType(PasteType.VALUES); | |
pasteOptions.setOnlyVisibleCells(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.getCells().copyRows(source.getCells(), 0, 0, source.getCells().getMaxDisplayRange().getRowCount(), options, pasteOptions); | |
// Save workbook in xlsx format | |
wb.save("destination.xlsx", SaveFormat.XLSX); |