نسخ الصفوف والأعمدة
مقدمة
في بعض الأحيان ، تحتاج إلى نسخ الصفوف والأعمدة في ورقة العمل دون نسخ ورقة العمل بأكملها. باستخدام Aspose.Cells ، يمكن نسخ الصفوف والأعمدة داخل أو بين المصنفات. عند نسخ صف (أو عمود) ، يتم أيضًا نسخ البيانات الموجودة فيه ، بما في ذلك الصيغ - مع المراجع المحدثة - والقيم والتعليقات والتنسيق والخلايا المخفية والصور والكائنات الرسومية الأخرى.
نسخ الصفوف والأعمدة باستخدام Microsoft Excel
- حدد الصف أو العمود الذي تريد نسخه.
- لنسخ الصفوف أو الأعمدة ، انقر فوقينسخ على المعيار شريط الأدوات ، أو اضغط علىكنترول+ ** ج **.
- حدد صفًا أو عمودًا أدناه أو على يمين المكان الذي تريد نسخ تحديدك إليه.
- عندما تقوم بنسخ صفوف أو أعمدة ، انقر فوقمنسوخة Cells على الإدراج قائمة.
لصق الصفوف والأعمدة باستخدام خيارات اللصق مع Microsoft Excel
- حدد الخلايا التي تحتوي على البيانات أو السمات الأخرى التي تريد نسخها.
- في علامة التبويب الصفحة الرئيسية ، انقر فوقينسخ.
- انقر فوق الخلية الأولى في المنطقة التي تريدهامعجون ما نسخته.
- في علامة التبويب الصفحة الرئيسية ، انقر فوق السهم الموجود بجانبمعجون ، ثم حددمعجون مميز.
- حدد ملفوالخيارات انت تريد.
باستخدام Aspose.Cells
نسخ صفوف واحدة
يوفر 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"); |
عند نسخ الصفوف ، من المهم ملاحظة الصور أو المخططات أو الكائنات الرسومية الأخرى ذات الصلة لأن هذا هو نفسه مع Microsoft Excel:
- إذا كان فهرس صف المصدر هو 5 ، يتم نسخ الصورة والرسم البياني وما إلى ذلك إذا كانت موجودة في الصفوف الثلاثة (مؤشر صف البداية هو 4 ومؤشر صف النهاية هو 6).
- لن تتم إزالة الصور والمخططات الموجودة وما إلى ذلك في صف الوجهة.
نسخ صفوف متعددة
يمكنك أيضًا نسخ صفوف متعددة إلى وجهة جديدة أثناء استخدام ملف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 طريقةCellsclass ، هذه الطريقة تنسخ جميع أنواع البيانات ، بما في ذلك الصيغ - مع المراجع المحدثة - والقيم والتعليقات وتنسيقات الخلايا والخلايا المخفية والصور وكائنات الرسم الأخرى من العمود المصدر إلى عمود الوجهة.
ال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 يوفر الآنخيارات لصق أثناء استخدام الوظائف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); |