Home >Backend Development >PHP Tutorial >PHP generates excel files and outputs them to the browser_PHP tutorial
php tutorial generates excel files and outputs them to the browser
This article introduces php spreadsheet_excel_writer and how to generate excel files.
The first step is to install spreadsheet_excel_writer. Since this package uses the ole package, you may need to install it
Execute the following command to perform the update: update pear.php.net ole - 0.5 spreadsheet_excel_writer - 0.9.1
Look at an example, myfile.xls is the file name (including path), and the workbook contains a table with a student list
require_once 'spreadsheet/excel/writer.php';
// creating workbook
$workbook = new spreadsheet_excel_writer('myfile.xls');
// adding worksheet
$worksheet =& $workbook->addworksheet('students');
// data input
$worksheet->write(0, 0, 'name');
$worksheet->write(0, 1, 'grade');
$worksheet->write(1, 0, 'ivancho');
$worksheet->write(1, 1, 7);
$worksheet->write(2, 0, 'mariika');
$worksheet->write(2, 1, 7);
$worksheet->write(3, 0, 'stoyancho');
$worksheet->write(3, 1, 8);
// saving file
$workbook->close();
?>
Let’s take a look at a method to export data to users for storage.
require_once 'spreadsheet/excel/writer.php';
// creating workbook
$workbook = new spreadsheet_excel_writer();
// sending headers to browser
$workbook->send('students.xls');
// adding worksheet
$worksheet =& $workbook->addworksheet('students');
// data input
$worksheet->write(0, 0, 'name');
$worksheet->write(0, 1, 'grade');
$worksheet->write(1, 0, 'ivancho');
$worksheet->write(1, 1, 7);
$worksheet->write(2, 0, 'mariika');
$worksheet->write(2, 1, 7);
$worksheet->write(3, 0, 'stoyancho');
$worksheet->write(3, 1, 8);
// sending the file
$workbook->close();