PHPExcel is a PHP library that allows you to read, write and manipulate Excel spreadsheets. One of the key features of PHPExcel is the ability to set the row height and width in Excel spreadsheets. In this tutorial, you will learn how to set the row height and width using PHPExcel.
How to set row height and width using phpexcel
- Setting Row Height
- Setting Column Width
Setting Row Height
To set the row height, you need to use the setRowHeight() method of the PHPExcel_Worksheet class. This method takes two arguments: the row index and the height in points.
Here is an example code snippet to set the row height of the first row to 30 points:
$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(30);
In this example, you first get the row dimension of the first row using getRowDimension(1) method. Then you set the height of the row using setRowHeight(30) method.
You can also set the height of multiple rows using a loop. Here is an example code snippet to set the height of first 10 rows to 30 points:
for($i=1;$i<=10;$i++){ $objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(30); }
In this example, you use a for loop to iterate through the first 10 rows and set the height of each row to 30 points.
Setting Column Width
To set the column width, you need to use the setColumnWidth() method of the PHPExcel_Worksheet class. This method takes two arguments: the column index and the width in characters.
Here is an example code snippet to set the column width of column A to 20 characters:
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
In this example, you first get the column dimension of column A using getColumnDimension(‘A’) method. Then you set the width of the column using setWidth(20) method.
You can also set the width of multiple columns using a loop. Here is an example code snippet to set the width of first 10 columns to 20 characters:
for($i='A';$i<='J';$i++){ $objPHPExcel->getActiveSheet()->getColumnDimension($i)->setWidth(20); }
In this example, you use a for loop to iterate through the first 10 columns and set the width of each column to 20 characters.
Conclusion
Setting row height and column width is a common requirement while working with Excel spreadsheets. PHPExcel provides easy-to-use methods to set the row height and column width of Excel spreadsheets. You can set the height and width of single as well as multiple rows and columns using simple loops. With this knowledge, you can create dynamic and customized Excel spreadsheets using PHPExcel.