HelpBot Assistant

How can I help you?

How to get the column width and row height in pixels?

17 Oct 20254 minutes to read

In Essential® XlsIO, you can obtain column widths and row heights in pixels by using the GetColumnWidthInPixels and GetRowHeightInPixels methods. The below code snippet demonstrates this.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    FileStream inputStream = new FileStream("InputTemplate.xlsx", FileMode.Open, FileAccess.Read);
    IWorkbook workbook = application.Workbooks.Open(inputStream);
    IWorksheet worksheet = workbook.Worksheets[0];

    var range = worksheet.UsedRange["A1"];  
    
    //Get the Column width in pixels
    var width = worksheet.GetColumnWidthInPixels(range.Column);

    //Get the Row height in pixels
    var height = worksheet.GetRowHeightInPixels(range.Row);

    #region Save
    //Saving the workbook
    FileStream outputStream = new FileStream("RowsandColumns.xlsx", FileMode.Create, FileAccess.Write);
    workbook.SaveAs(outputStream);
    #endregion

    //Dispose streams
    outputStream.Dispose();
    inputStream.Dispose();
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx");
    IWorksheet worksheet = workbook.Worksheets[0];                
    var range = worksheet.UsedRange["A1"];

    //Get the Column width in pixels
    var width = worksheet.GetColumnWidthInPixels(range.Column);

    //Get the Row height in pixels
    var height = worksheet.GetRowHeightInPixels(range.Row);

    workbook.SaveAs("RowsandColumns.xlsx");
}
Using excelEngine As ExcelEngine = New ExcelEngine()
     Dim application As IApplication = excelEngine.Excel
     application.DefaultVersion = ExcelVersion.Excel2013
     Dim workbook As IWorkbook = application.Workbooks.Open("InputTemplate.xlsx", ExcelOpenType.Automatic)
     Dim worksheet As IWorksheet = workbook.Worksheets(0)

     Dim range = worksheet.UsedRange("A1")
     ' Column width in pixels
     Dim width As Integer = worksheet.GetColumnWidthInPixels(range.Column)
     ' Row height in pixels
     Dim height As Integer = worksheet.GetRowHeightInPixels(range.Row)

     workbook.SaveAs("GridLineColor.xlsx")
 End Using

See Also