How to Find highest row & column in PHPExcel

To find the highest row and column in PHPExcel, you can use the following methods:

1. getHighestRow() – This method returns the highest row number of the active worksheet.

2. getHighestColumn() – This method returns the highest column letter of the active worksheet.

3. getHighestDataColumn() – This method returns the highest column letter of the active worksheet that contains data.

4. getHighestDataRow() – This method returns the highest row number of the active worksheet that contains data.
[ad_1]

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

[ad_2]

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00