How to Set Column Width PHPExcel

To set the column width in PHPExcel, you can use the setWidth() method.

Example:

$objPHPExcel->getActiveSheet()->getColumnDimension(‘A’)->setWidth(20);
[ad_1]

To set the width of cells/columns. In this tutorial, you will learn how to set the width of cells/columns or multiple columns/cells in PHPExcel.

How to Set width of spreadsheet cell /Column using PHPExcel

By following these simple steps, you can set width of spreadsheet cell /column in PHPexcel:

  • Step 1: Create a new PHPExcel object
  • Step 2: Create a worksheet
  • Step 3: Set the width of a cell
  • Step 4: Set the width of multiple cells
  • Step 5: Save the worksheet

Step 1: Create a new PHPExcel object

To begin, you need to create a new PHPExcel object. This can be done using the following code:

require_once 'path/to/PHPExcel.php';

$objPHPExcel = new PHPExcel();

This code includes the PHPExcel library and creates a new PHPExcel object.

Step 2: Create a worksheet

Next, you need to create a worksheet within the PHPExcel object. To do this, you can use the following code:

$worksheet = $objPHPExcel->getActiveSheet();

This code creates a new worksheet and assigns it to the $worksheet variable.

Step 3: Set the width of a cell

Now that you have a worksheet, you can set the width of a cell. To set the width of a cell, you need to use the getColumnDimension() method of the worksheet object. Here’s an example:

$cell = 'A1';
$worksheet->getColumnDimension(substr($cell, 0, 1))->setWidth(15);

This code sets the width of column A to 15 characters.

Step 4: Set the width of multiple cells

You can also set the width of multiple cells at once. To do this, you can use a loop to iterate over an array of cell addresses and set their widths. Here’s an example:

$cells = array('A1', 'B1', 'C1');
foreach ($cells as $cell) {
    $worksheet->getColumnDimension(substr($cell, 0, 1))->setWidth(15);
}

This code sets the width of columns A, B, and C to 15 characters.

Step 5: Save the worksheet

Finally, you need to save the worksheet to a file. To do this, you can use the following code:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('example.xlsx');

This code creates a new PHPExcel_IOFactory object, sets its file type to Excel2007, and saves the PHPExcel object to a file named “example.xlsx”.

Conclusion

In this tutorial, we have shown you how to set the width of cells in PHPExcel. By following these simple steps, you can customize the layout of your spreadsheets and make your data more visually appealing. With PHPExcel’s powerful functionality, you can create complex spreadsheets with ease and manipulate them programmatically to suit your needs.

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.

Contact
Jassweb, Rai Chak, Punjab, India. 143518