احتواء تلقائي للصفوف والأعمدة
تركيب تلقائي
يوفر Aspose.Cells أدفتر العملفئة تمثل ملف Excel Microsoft. الدفتر العملفئة تحتوي علىأوراق عملمجموعة تسمح بالوصول إلى كل ورقة عمل في ملف Excel. يتم تمثيل ورقة العمل بواسطةورقة عمل صف دراسي. الورقة عمل توفر class مجموعة واسعة من الخصائص والأساليب لإدارة ورقة العمل. تبحث هذه المقالة في استخدامورقة عملفئة للاحتواء التلقائي للصفوف أو الأعمدة.
صف الاحتواء التلقائي - بسيط
تتمثل الطريقة الأكثر مباشرة في تغيير حجم عرض الصف وارتفاعه تلقائيًا في استدعاءورقة عمل صف دراسيAutoFitRow طريقة. الAutoFitRowتأخذ الطريقة فهرس الصف (للصف الذي سيتم تغيير حجمه) كمعامل.
// 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); | |
string InputPath = dataDir + "Book1.xlsx"; | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(InputPath, FileMode.Open); | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Auto-fitting the 3rd row of the worksheet | |
worksheet.AutoFitRow(1); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xlsx"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
صف الاحتواء التلقائي في نطاق Cells
يتكون الصف من عدة أعمدة. يسمح Aspose.Cells للمطورين بملاءمة صف تلقائيًا استنادًا إلى المحتوى في نطاق من الخلايا داخل الصف عن طريق استدعاء نسخة محملة بشكل زائد منAutoFitRowطريقة. يأخذ المعلمات التالية:
- فهرس الصف، فهرس الصف الذي سيتم تركيبه تلقائيًا.
- فهرس العمود الأول، فهرس العمود الأول للصف.
- فهرس العمود الأخير، فهرس العمود الأخير للصف.
الAutoFitRowيتحقق الأسلوب من محتويات جميع الأعمدة في الصف ثم يقوم تلقائيًا بملاءمة الصف.
// 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); | |
string InputPath = dataDir + "Book1.xlsx"; | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(InputPath, FileMode.Open); | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Auto-fitting the 3rd row of the worksheet | |
worksheet.AutoFitRow(1, 0, 5); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xlsx"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
عمود الاحتواء التلقائي في نطاق Cells
يتكون العمود من عدة صفوف. من الممكن أن يتم احتواء عمود تلقائيًا استنادًا إلى المحتوى الموجود في نطاق من الخلايا في العمود عن طريق استدعاء نسخة محملة بشكل زائد منAutoFitColumnالطريقة التي تأخذ المعلمات التالية:
- فهرس العمود، فهرس العمود الذي على وشك أن يتم تركيبه تلقائيًا.
- فهرس الصف الأول، فهرس الصف الأول للعمود.
- فهرس الصف الأخير، فهرس الصف الأخير للعمود.
الAutoFitColumnيتحقق الأسلوب من محتويات جميع الصفوف في العمود ثم يقوم تلقائيًا بملاءمة العمود.
// 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); | |
string InputPath = dataDir + "Book1.xlsx"; | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(InputPath, FileMode.Open); | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Auto-fitting the Column of the worksheet | |
worksheet.AutoFitColumn(4, 4, 6); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xlsx"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
صفوف AutoFit للدمج Cells
مع Aspose.Cells ، من الممكن احتواء الصفوف تلقائيًا حتى للخلايا التي تم دمجها باستخدامAutoFitterOptions API. AutoFitterOptionsفئة تقدمAutoFitMergedCellsType الخاصية التي يمكن استخدامها للاحتواء التلقائي للصفوف للخلايا المدمجة.AutoFitMergedCellsTypeيقبلAutoFitMergedCellsType معدود يضم الأعضاء التالية أسماؤهم.
- بلا: تجاهل الخلايا المدمجة.
- FirstLine: يوسع ارتفاع الصف الأول فقط.
- LastLine: يوسع ارتفاع الصف الأخير فقط.
- EveryLine: يوسع فقط ارتفاع كل صف.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Instantiate a new Workbook | |
Workbook wb = new Workbook(); | |
// Get the first (default) worksheet | |
Worksheet _worksheet = wb.Worksheets[0]; | |
// Create a range A1:B1 | |
Range range = _worksheet.Cells.CreateRange(0, 0, 1, 2); | |
// Merge the cells | |
range.Merge(); | |
// Insert value to the merged cell A1 | |
_worksheet.Cells[0, 0].Value = "A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end"; | |
// Create a style object | |
Aspose.Cells.Style style = _worksheet.Cells[0, 0].GetStyle(); | |
// Set wrapping text on | |
style.IsTextWrapped = true; | |
// Apply the style to the cell | |
_worksheet.Cells[0, 0].SetStyle(style); | |
// Create an object for AutoFitterOptions | |
AutoFitterOptions options = new AutoFitterOptions(); | |
// Set auto-fit for merged cells | |
options.AutoFitMergedCellsType = AutoFitMergedCellsType.EachLine; | |
// Autofit rows in the sheet(including the merged cells) | |
_worksheet.AutoFitRows(options); | |
// Save the Excel file | |
wb.Save(outputDir + "AutofitRowsforMergedCells.xlsx"); |
يمكنك أيضًا محاولة استخدام الإصدارات المحملة بشكل زائد منAutoFitRows & الأعمدة التلقائية الطرق التي تقبل نطاقًا من الصفوف / الأعمدة ومثيلًا لـAutoFitterOptions لملاءمة الصفوف / الأعمدة المحددة تلقائيًا مع ما تريدهAutoFitterOptionsوفقاً لذلك.
تواقيع الطرق المذكورة هي كما يلي:
- AutoFitRows (int startRow، int endRow،AutoFitterOptionsوالخيارات)
- AutoFitColumns (int firstColumn ، int lastColumn ،AutoFitterOptionsوالخيارات)