I create in my php code a excel file with one tab and give it a name. The code what i use is this.:
// CREATE PHPSPREADSHEET OBJECT
require "../vendor/autoload.php";
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
// CREATE A NEW SPREADSHEET + POPULATE DATA
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('Batch');
But how do i create a second tab with another name?
[solved]
// CREATE A NEW SPREADSHEET + POPULATE DATA
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('Batch');
// Add some data
$spreadsheet->createSheet();
// Add some data
$spreadsheet->setActiveSheetIndex(1) ->setCellValue('A1', 'world!');
// Rename worksheet
$spreadsheet->getActiveSheet()->setTitle('URL Removed');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$spreadsheet->setActiveSheetIndex(0);
Adding a new Worksheet You can add a new worksheet to the workbook using the createSheet() method of the Spreadsheet object.
When you first instantiate the $objPHPExcel, it already has a single sheet (sheet 0); you're then adding a new sheet (which will become sheet 1), but setting active sheet to sheet $i (when $i is 0)... so you're renaming and populating the original worksheet created when you instantiated $objPHPExcel rather than the one ...
Use composer to install PhpSpreadsheet into your project. Or also download the documentation and samples if you plan to use them. A good way to get started is to run some of the samples. Don't forget to download them via --prefer-source composer flag.
You can add an extra tab like this:
$spreadsheet->createSheet();
// Zero based, so set the second tab as active sheet
$spreadsheet->setActiveSheetIndex(1)
$spreadsheet->getActiveSheet()->setTitle('Second tab');
If you like, you can read more here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With