How to add multiple worksheets using phpexcel class.
$this->excel = new PHPExcel();
Take a look at the PHPExcel documentation
// Create a new worksheet called "My Data"
$myWorkSheet = new PHPExcel_Worksheet($objPHPExcel, 'My Data');
// Attach the "My Data" worksheet as the first worksheet in the PHPExcel object
$objPHPExcel->addSheet($myWorkSheet, 0);
Related
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);
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.
You can also try this way
//at the beginning of the code
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
$myWorkSheet = new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet($spreadsheet, 'Extra Tab');
$spreadsheet->addSheet($myWorkSheet, 0);
$sheetIndex = $spreadsheet->getIndex(
$spreadsheet->getSheetByName('Extra Tab')
);
$spreadsheet->setActiveSheetIndex($sheetIndex);
For more information here
i have two different Excel files(workbook), one have 2 sheets and another have 3, through PHP code using PHP Excel library , i managed to combine all these in one file and able to make one file with 5 sheets, all the data & table are fine , but there are no any pie charts & graphs( its blank).
How can i combine workbook along with charts & graphs.
include ("PHPExcel.php");
$inputFileType1 = 'Excel2007';
$inputFileName1 = 'a.xlsx';
$inputFileType2 = 'Excel2007';
$inputFileName2 = 'b.xlsx';
$outputFileType = 'Excel2007';
$outputFileName = 'c.xlsx';
// Load the first workbook (an xlsx file)
$objPHPExcelReader1 = PHPExcel_IOFactory::createReader($inputFileType1);
$objPHPExcelReader1->setIncludeCharts(TRUE);
$objPHPExcel1 = $objPHPExcelReader1->load($inputFileName1);
// Load the second workbook (an xls file)
$objPHPExcelReader2 = PHPExcel_IOFactory::createReader($inputFileType2);
$objPHPExcelReader2->setIncludeCharts(TRUE);
$objPHPExcel2 = $objPHPExcelReader2->load($inputFileName2);
// Merge the second workbook into the first
$objPHPExcel2->getActiveSheet()->setTitle('Unique worksheet name');
$objPHPExcel1->addExternalSheet($objPHPExcel2->getActiveSheet());
// Save the merged workbook under a new name (could save under the original
name)
// as an xls file
$objPHPExcelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel1,$outputFileType);
$objPHPExcelWriter->save($outputFileName);
$inputFileType1 = 'Excel2007';
$inputFileName1 = 'inputData1.xlsx';
$inputFileType2 = 'Excel5';
$inputFileName2 = 'inputData2.xls';
$outputFileType = 'Excel5';
$outputFileName = 'outputData.xls';
// Load the first workbook (an xlsx file)
$objPHPExcelReader1 = PHPExcel_IOFactory::createReader($inputFileType1);
$objPHPExcel1 = $objPHPExcelReader1->load($inputFileName1);
// Load the second workbook (an xls file)
$objPHPExcelReader2 = PHPExcel_IOFactory::createReader($inputFileType2);
$objPHPExcel2 = $objPHPExcelReader2->load($inputFileName2);
// Merge the second workbook into the first
$objPHPExcel2->getActiveSheet()->setTitle('Unique worksheet name');
$objPHPExcel1->addExternalSheet($objPHPExcel2->getActiveSheet());
// Save the merged workbook under a new name (could save under the original name)
// as an xls file
$objPHPExcelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel1,$outputFileType);
$objPHPExcelWriter->save($outputFileName);
Mission: I need add 2 .csv files to separate sheets
Problem: Second import removes second created sheet and puts information on first sheet
$inputFileType = 'CSV';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setDelimiter(';');
$objPHPExcel = $objReader->load('fail1.csv');
$objPHPExcel->getActiveSheet()->setTitle('laoseis');
//teine leht
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(1);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setDelimiter(';');
$objPHPExcel->setActiveSheetIndex(1);
$objPHPExcel = $objReader->load('fail2.csv');
$date = new DateTime($_GET['startDate']);
$objPHPExcel->getActiveSheet()->setTitle('Müük W'.$date->format("W").'');
How can I solve this problem?
PHPExcel doesn't load the second file to your current sheet in an existing PHPExcel object, it creates a new PHPExcel object and loads the file to that, so you're replacing the first completely when you try to load the second.
Load each csv to a separate PHPExcel instance, then copy the worksheet from the second instance to the first.
$objReader1 = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel1 = $objReader1->load('fail1.csv');
$objReader2 = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel2 = $objReader2->load('fail2.csv');
$objPHPExcel2->getActiveSheet()->setTitle('Worksheet 2');
$objPHPExcel1->addExternalSheet($objPHPExcel2->getActiveSheet());
Now $objPHPExcel1 has both worksheets, and you can save that
How do I load an Excel Template with PHPExcel and write to its cells and also insert images to cells dynamically?
You can read your excel template like this with PHPExcel:
$objPHPExcel = PHPExcel_IOFactory::load("./forms/english/cash.xlsx");
and you can write to cells like this:
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A2', "No")
->setCellValue('B2', "Name")
->setCellValue('C2', "Email")
->setCellValue('D2', "Phone")
->setCellValue('E2', "Address");
see the example, 30template.php in github site
https://github.com/PHPOffice/PHPExcel/blob/develop/Examples/30template.php
load template :
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load("templates/30template.xls");
see in the example write via
$objPHPExcel->getActiveSheet()->setCellValue()
to add image use PHPExcel_Worksheet_Drawing :
// Add an image to the worksheet
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('My Image');
$objDrawing->setDescription('The Image that I am inserting');
$objDrawing->setPath('./images/myImage.png');
$objDrawing->setCoordinates('B2');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
Now you don't need load templates. Try to use the PHP Excel templator:
https://github.com/alhimik1986/php-excel-templator
I have 2 xls, i want to plot this information into one HTML Page.
Note: For assumption i mentioned as xls. actual xls positions are already in the database table. i will just render these position and plot into HTML Page.
include_once("Classes/PHPExcel/IOFactory.php");
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
//first excel file
$objPHPExcel->getActiveSheet()
->setCellValue('c5','10');
$objPHPExcel->getActiveSheet()
->setCellValue('c6','20');
$objPHPExcel->getActiveSheet()
->setCellValue('c7','30');
$objPHPExcel->getActiveSheet()
->setCellValue('c8','40');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
//second excel file
$objPHPExcel->getActiveSheet()
->setCellValue('c5','50');
$objPHPExcel->getActiveSheet()
->setCellValue('c6','60');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->save('combinedexcelpage.html');
I'm not going to go through a long explanation of why this doesn't work, because it would take too long to explain; but there are a couple of solutions that you could take to achieve what you want:
Option #1
An Excel workbook comprises one or more worksheets, so you could create each "file" as a separate worksheet, rather than a separate file.
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// First excel worksheet, (created when you instantiate a new PHPExcel object)
$objPHPExcel->getActiveSheet()
->setCellValue('c5','10');
$objPHPExcel->getActiveSheet()
->setCellValue('c6','20');
$objPHPExcel->getActiveSheet()
->setCellValue('c7','30');
$objPHPExcel->getActiveSheet()
->setCellValue('c8','40');
// Second excel worksheet
// Add new sheet, which should also set it as the new "active" sheet
$objPHPExcel->createSheet()
$objPHPExcel->getActiveSheet()
->setCellValue('c5','50');
$objPHPExcel->getActiveSheet()
->setCellValue('c6','60');
By default, the HTML Writer will only write a single worksheet (the first), but you can set it to write all sheets:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->writeAllSheets();
$objWriter->save('combinedexcelpage.html');
Option #2
The HTML Writer save() method will always generate a file stream (whether a filesystem file or php://output), but rather than using save(), you can call individual methods in the class to generate a string containing the formatted worksheet data, and build your own output from those "blocks".
$data = '';
// First excel file
$objPHPExcel1 = new PHPExcel();
$objPHPExcel1->getActiveSheet()
->setCellValue('c5','10');
$objPHPExcel1->getActiveSheet()
->setCellValue('c6','20');
$objPHPExcel1->getActiveSheet()
->setCellValue('c7','30');
$objPHPExcel1->getActiveSheet()
->setCellValue('c8','40');
$objWriter1 = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter1->generateStyles(false);
$data .= $objWriter1->generateHTMLHeader();
$data .= $objWriter1->generateSheetData();
// Second excel file
$objPHPExcel2 = new PHPExcel();
$objPHPExcel2->getActiveSheet()
->setCellValue('c5','50');
$objPHPExcel2->getActiveSheet()
->setCellValue('c6','60');
$objWriter2 = PHPExcel_IOFactory::createWriter($objPHPExcel2, 'HTML');
$objWriter2->generateStyles(false);
$data .= $objWriter2->generateSheetData();
$data .= $objWriter2->generateHTMLFooter();
file_put_contents('combinedexcelpage.html', $data);
Both of these options are described in section 6.8 of the developer documentation