How to Find highest row & column in PHPExcel

How to Find highest row & column in PHPExcel

Get highest row & column in PHPexcel; In this tutorial, you will learn how to find the highest row and column in a PHPExcel worksheet.

How to Find highest row & column in PHPExcel

By using the getHighestDataRow() and getHighestDataColumn() methods, you can easily find the highest row and column in a worksheet.

  • Step 1: Load the Worksheet
  • Step 2: Find the Highest Row
  • Step 3: Find the Highest Column
  • Step 4: Convert Column Letter to Column Index

Step 1: Load the Worksheet

The first step is to load the worksheet that you want to work with. You can do this using the PHPExcel_IOFactory class. For example, if you have an Excel file named ‘example.xlsx’ in the same directory as your PHP script, you can load the worksheet using the following code:

require_once 'PHPExcel/IOFactory.php';

$inputFileName = 'example.xlsx';
$excelReader = PHPExcel_IOFactory::createReaderForFile($inputFileName);
$excelObj = $excelReader->load($inputFileName);
$worksheet = $excelObj->getActiveSheet();

This code will load the ‘example.xlsx’ file and store the active worksheet in the $worksheet variable.

Step 2: Find the Highest Row

To find the highest row in the worksheet, you can use the getHighestDataRow() method of the PHPExcel_Worksheet class. This method returns the index of the last row that contains data. For example, if the worksheet contains data up to row 10, the getHighestDataRow() method will return 10.

$highestRow = $worksheet->getHighestDataRow();
echo "The highest row is $highestRow";

This code will output the highest row in the worksheet.

Step 3: Find the Highest Column

To find the highest column in the worksheet, you can use the getHighestDataColumn() method of the PHPExcel_Worksheet class. This method returns the letter of the last column that contains data. For example, if the worksheet contains data up to column D, the getHighestDataColumn() method will return ‘D’.

$highestColumn = $worksheet->getHighestDataColumn();
echo "The highest column is $highestColumn";

This code will output the highest column in the worksheet.

Step 4: Convert Column Letter to Column Index

If you want to use the highest column index in a loop or any other operation, you need to convert the column letter to the column index. You can do this using the PHPExcel_Cell::columnIndexFromString() method. This method takes a column letter as its argument and returns the corresponding column index. For example, if the highest column letter is ‘D’, the columnIndexFromString(‘D’) method will return 3.

$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
echo "The highest column index is $highestColumnIndex";

This code will output the highest column index in the worksheet.

Conclusion

In this article, we have discussed how to find the highest row and column in a PHPExcel worksheet. By using the getHighestDataRow() and getHighestDataColumn() methods, you can easily find the highest row and column in a worksheet. Additionally, we have shown how to convert the column letter to the column index using the columnIndexFromString() method. With this information, you can perform various operations on the worksheet and work with the data more efficiently.

Recommended Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *