Worksheet.AutoFitRow

AutoFitRow(int, int, int, int)

Autofits row height in a rectangle range.

public void AutoFitRow(int startRow, int endRow, int startColumn, int endColumn)
ParameterTypeDescription
startRowInt32Start row index.
endRowInt32End row index.
startColumnInt32Start column index.
endColumnInt32End column index.

Examples

namespace AsposeCellsExamples
{
    using Aspose.Cells;
    using System;

    public class WorksheetMethodAutoFitRowWithInt32Int32Int32Int32Demo
    {
        public static void Run()
        {
            // Create a new workbook
            Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];

            RowCollection rows = worksheet.Cells.Rows;
            rows[0].Height = 20;
            rows[1].Height = 20;
            // Add sample data to cells to demonstrate row autofit
            worksheet.Cells["A1"].PutValue("This is a long text that will demonstrate row autofit");
            worksheet.Cells["B1"].PutValue("Additional column with text");
            worksheet.Cells["A2"].PutValue("Shorter text");
            worksheet.Cells["B2"].PutValue("More text");
            
            try
            {
                Console.WriteLine($"Row 1 height before autofit: {worksheet.Cells.Rows[0].Height}");
                Console.WriteLine($"Row 2 height before autofit: {worksheet.Cells.Rows[1].Height}");

                // Call AutoFitRow with parameters: startRow, endRow, startColumn, endColumn
                worksheet.AutoFitRow(0, 1, 0, 1);
                
                Console.WriteLine("AutoFitRow method executed successfully with parameters (0, 1, 0, 1)");
                
                // Display the effect by showing row heights
                Console.WriteLine($"Row 1 height after autofit: {worksheet.Cells.Rows[0].Height}");
                Console.WriteLine($"Row 2 height after autofit: {worksheet.Cells.Rows[1].Height}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error executing AutoFitRow method: {ex.Message}");
            }
            
            // Save the result
            workbook.Save("AutoFitRowWithInt32Int32Int32Int32Demo.xlsx");
        }
    }
}

See Also


AutoFitRow(int)

Autofits the row height.

public void AutoFitRow(int rowIndex)
ParameterTypeDescription
rowIndexInt32Row index.

Remarks

AutoFitRow is an imprecise function.

Examples

using System;
using Aspose.Cells;

namespace AsposeCellsExamples
{
    public class WorksheetMethodAutoFitRowWithInt32Demo
    {
        public static void Run()
        {
            // Create a workbook
            Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];

            // Set long text with text wrapping in cell A1
            worksheet.Cells["A1"].Value = "LOOOOOOOOOOOOOOOOOONG TEEEEEEEEEEXT";
            Style style = worksheet.Cells["A1"].GetStyle();
            style.IsTextWrapped = true;
            worksheet.Cells["A1"].SetStyle(style);

            // Auto-fit the first row
            worksheet.AutoFitRow(0);

            // Save the workbook
            workbook.Save("AutoFitRowDemo.xlsx");
        }
    }
}

See Also


AutoFitRow(int, int, int)

Autofits the row height.

public void AutoFitRow(int rowIndex, int firstColumn, int lastColumn)
ParameterTypeDescription
rowIndexInt32Row index.
firstColumnInt32First column index.
lastColumnInt32Last column index.

Remarks

This method autofits a row based on content in a range of cells within the row.

Examples

using System;
using Aspose.Cells;

namespace AsposeCellsExamples
{
    public class WorksheetMethodAutoFitRowWithInt32Int32Int32Demo
    {
        public static void Run()
        {
            // Create a new workbook
            Workbook workbook = new Workbook();
            
            // Access first worksheet
            Worksheet worksheet = workbook.Worksheets[0];
            
            // Add sample data to cells that will affect row height
            worksheet.Cells["A2"].PutValue("This is a long text that will demonstrate AutoFitRow functionality");
            worksheet.Cells["B2"].PutValue("Additional text in another column");
            
            // AutoFit the row (row 2) between columns 0 and 1 (A to B)
            worksheet.AutoFitRow(1, 0, 1);
            
            // Save the workbook
            workbook.Save("AutoFitRowDemo.xlsx");
        }
    }
}

See Also


AutoFitRow(int, int, int, AutoFitterOptions)

Autofits the row height.

public void AutoFitRow(int rowIndex, int firstColumn, int lastColumn, AutoFitterOptions options)
ParameterTypeDescription
rowIndexInt32Row index.
firstColumnInt32First column index.
lastColumnInt32Last column index.
optionsAutoFitterOptionsThe auto fitter options

Remarks

This method autofits a row based on content in a range of cells within the row.

Examples

using System;
using Aspose.Cells;

namespace AsposeCellsExamples
{
    public class WorksheetMethodAutoFitRowWithInt32Int32Int32AutoFitterOptioDemo
    {
        public static void Run()
        {
            // Create a new workbook
            Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];

            // Add sample data to cells
            worksheet.Cells["A1"].PutValue("This is a test string for autofit");
            worksheet.Cells["B1"].PutValue("Another test string in column B");
            
            // Create AutoFitterOptions
            AutoFitterOptions options = new AutoFitterOptions
            {
                AutoFitMergedCells = true,
                IgnoreHidden = false,
                OnlyAuto = false
            };

            // Get original row height
            double originalHeight = worksheet.Cells.GetRowHeight(0);
            Console.WriteLine($"Original row height: {originalHeight}");

            // AutoFit the first row
            worksheet.AutoFitRow(0, 0, 2, options);

            // Get new row height
            double newHeight = worksheet.Cells.GetRowHeight(0);
            Console.WriteLine($"New row height after AutoFit: {newHeight}");

            // Save the workbook
            workbook.Save("AutoFitRowExample.xlsx");
        }
    }
}

See Also