PHPSpreadsheet · Google Sheets API · Excel
Spreadsheets, driven by code.
Hands-on PHP tutorials for working with Excel and Google Sheets — read and
write .xlsx, convert files to JSON, stream downloads in the
browser, and insert images, formulas, and styling. Every guide ships with
code you can copy, run, and adapt.
Latest complete code
<?php require 'vendor/autoload.php'; use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Alignment; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); $worksheet->setTitle('Survey'); $worksheet->fromArray([ ['Rep', 'Answered on time', 'Escalated twice', 'Left a comment', 'Notes'], ['Ivy', 'Yes', 'No', 'Yes', 'steady'], ['Marco', 'Yes', 'Yes', 'No', 'watch'], ['Dan', 'No', 'No', 'Yes', 'steady'], ], null, 'A1'); // Long headings over narrow columns: rotate them 45 degrees. $worksheet->getStyle('B1:D1')->getAlignment() ->setTextRotation(45) ->setVertical(Alignment::VERTICAL_BOTTOM); // The 255 special case: letters stacked one under the next, not rotated. $worksheet->getStyle('E1')->getAlignment()->setTextRotation(255); // Rotated text needs the room; Excel will not grow the row for you here. $worksheet->getRowDimension(1)->setRowHeight(90); foreach (range('B', 'D') as $column) { $worksheet->getColumnDimension($column)->setWidth(6); } (new Xlsx($spreadsheet))->save('survey.xlsx'); echo "Wrote survey.xlsx\n\n"; printf("%-6s %-14s %s\n", 'Cell', 'Set to', 'Stored as'); foreach (['B1' => 45, 'C1' => 45, 'D1' => 45, 'E1' => 255] as $cell => $set) { printf("%-6s %-14s %d\n", $cell, $set, $worksheet->getStyle($cell)->getAlignment()->getTextRotation()); } echo "\nRow 1 height: " . $worksheet->getRowDimension(1)->getRowHeight() . "\n"; // Anything outside -90..90 (except 255) is rejected, not clamped. try { $worksheet->getStyle('A1')->getAlignment()->setTextRotation(120); } catch (SpreadsheetException $e) { echo "\nsetTextRotation(120): " . $e->getMessage() . "\n"; } $reloaded = IOFactory::load('survey.xlsx')->getActiveSheet(); echo "\nAfter a save and reload:\n"; printf(" B1 %d\n E1 %d\n", $reloaded->getStyle('B1')->getAlignment()->getTextRotation(), $reloaded->getStyle('E1')->getAlignment()->getTextRotation() );
The full script from Rotate Text In A Cell In Excel Files In PHP Using PHPSpreadSheet — copy, run, adapt.
Key methods
IOFactory::load()
PhpSpreadsheet
getActiveSheet()
PhpSpreadsheet
fromArray()
PhpSpreadsheet
getCalculatedValue()
PhpSpreadsheet
save('php://output')
PhpSpreadsheet
spreadsheets_values->get()
Google Sheets
spreadsheets_values->update()
Google Sheets
spreadsheets->create()
Google Sheets
json_encode()
PHP
header()
PHP
Latest articles
Rotate Text In A Cell In Excel Files In PHP Using PHPSpreadSheet
August 8, 2026
Add Data Bars, Color Scales And Icon Sets In Excel Files In PHP Using PHPSpreadSheet
August 7, 2026
Sort Rows Before Writing An Excel File In PHP Using PHPSpreadSheet
August 7, 2026
Prevent Formula Injection In Excel Exports In PHP Using PHPSpreadSheet
August 7, 2026
Create An Excel Table In PHP Using PHPSpreadSheet
August 6, 2026
Use VLOOKUP In Excel Files In PHP Using PHPSpreadSheet
August 6, 2026
Read Excel Dates Back Into PHP Using PHPSpreadSheet
August 6, 2026
Convert An Excel File To XML In PHP Using PHPSpreadSheet
August 5, 2026
Save One Spreadsheet As XLSX, XLS And ODS In PHP Using PHPSpreadSheet
August 4, 2026
Browse by technique