PHPExcel - Combine two file in one - php

I have two xlsx files: first.xlsx and second.xlsx, I would combine this two files in one and color the second part of the last file appended(second.xlsx). What can I do?

Open/load both files as two separate PHPExcel objects, and use the addExternalSheet() method to move sheets from the second PHPExcel object to the first, then colour to taste and save the first.
$objPHPExcel1 = PHPExcel_IOFactory::load("MergeBook1.xlsx");
$objPHPExcel2 = PHPExcel_IOFactory::load("MergeBook2.xlsx");
foreach($objPHPExcel2->getSheetNames() as $sheetName) {
$sheet = $objPHPExcel2->getSheetByName($sheetName);
$sheet->setTitle($sheet->getTitle() . ' copied');
$objPHPExcel1->addExternalSheet($sheet);
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel1, 'Excel2007');
$objWriter->save('mergedBooks.xlsx');
The use of addExternalSheet() ensures that all styling, merges, etc as well as cell data is transferred over from the second workbook to the first
You can do whatever additional styling and colouring you want after the merge loop, and before saving
EDIT
If you simply want to copy data from one workbook to another, then something like:
$objPHPExcel1 = PHPExcel_IOFactory::load("MergeBook1.xlsx");
$objPHPExcel2 = PHPExcel_IOFactory::load("MergeBook2.xlsx");
$objPHPExcel1->getActiveSheet()->fromArray(
$objPHPExcel2->getActiveSheet->toArray(),
null,
'A' . ($objPHPExcel1->getActiveSheet()->getHighestRow() + 1)
);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel1, 'Excel2007');
$objWriter->save('mergedBooks.xlsx');

Related

php excel trying to add sheets with data dynamically in one workbook

I am trying to write the data to the multiple sheets in one excel file using phpExcel. I am getting data as array and i am looping it in that i am creating and writing the data .Problem is that it is creating the multiple sheets but it is writing all the data in one sheet. Below is my code:
function getcsv($data, $cirval, $opval)
{
global $objPHPExcel;
$myWorkSheet = new PHPExcel_Worksheet($objPHPExcel, $cirval);
$objPHPExcel->addSheet($myWorkSheet);
$objPHPExcel->setActiveSheetIndex(1);
$sheet = $objPHPExcel->getActiveSheet();
$sheet->setCellValue('A1', 'Denomination');
$sheet->setCellValue('B1', 'Validity');
$sheet->setCellValue('C1', 'Data Plan');
$rowcounter = 1;
foreach($data as $v1){
$rowcounter++;
$sheet->setCellValue('A' . $rowcounter, $v1['Denomination']);
$sheet->setCellValue('B' . $rowcounter, $v1['Validity']);
$sheet->setCellValue('C' . $rowcounter, $v1['Data Plan']);
}
}
Remove the line
$objPHPExcel->setActiveSheetIndex(1);
When you add a new worksheet using addSheet(), then PHPExcel automatically makes that the current active sheet.... you're explicitly overriding that behaviour and resetting the active sheet so that it is always sheet id #1, so it will always write everything to sheet id #1 no matter how many new sheets you add

PHPExcel: Combine 2 Excel files into 1 into the same worksheet

I have 2 excel files birds.xlsx and bees.xlsx both of which have the same number of columns and same type of column header. I've seen how PHPExcel does wonders with excel files but is there some way to combine 2 separate files into the same worksheet and saving it as a new file? The analogy that comes to mind is something like the SQL UNION command.
Something like:
// Load both spreadsheet files
$objPHPExcel1 = PHPExcel_IOFactory::load("birds.xlsx");
$objPHPExcel2 = PHPExcel_IOFactory::load("bees.xlsx");
// Find the last cell in the second spreadsheet
$findEndDataRow = $objPHPExcel2->getActiveSheet->getHighestRow();
$findEndDataColumn = $objPHPExcel2->getActiveSheet->getHighestColumn();
$findEndData = $findEndDataColumn . $findEndDataRow;
// Read all the data from second spreadsheet to a normal PHP array
// skipping the headers in row 1
$beeData = $objPHPExcel2->getActiveSheet->rangeToArray('A2:' . $findEndData);
// Identify the row in the first spreadsheet where we want to start
// adding merged bee data without overwriting any bird data
$appendStartRow = $objPHPExcel1->getActiveSheet->getHighestRow() + 1;
// Add bee data from the PHP array into the bird data
$objPHPExcel1->getActiveSheet->fromArray($beeData, null, 'A' . $appendStartRow);
// Save the spreadsheet with the merged data
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel1, 'Excel2007');
$objWriter->save(str_replace('animals.xlsx');

PHPExcel Mulitple excel file in one HTML page

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

phpexcel not writing data to excel file using setCellValueByColumnAndRow

$objPHPExcel = PHPExcel_IOFactory::load("untitled.xls");
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) //loop through the 3 worksheets of KindleEditionEISBN
{
$sheet_name = $worksheet->getTitle();
if ($worksheet->getTitle = 'Records')
{
$highestColumm = $objPHPExcel->setActiveSheetIndex(0)->getHighestColumn();
$highestRow = $objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($highestColumm, $highestRow, $node_10);
$objWriter2007 = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter2007->save('php://output');
The above is my code, I run it and the if loop works because I am getting the output but it does not do anything in my "untitled.xls" file
This code won't write anything to untitled.xls because you're saving to php://output (the screen) so the content of the file should be displayed on screen unless you're also sending headers to tell a browser to treat the output as an Excel file; and you're using the Excel2007 Writer to generate the output as an OfficeOpenXML format xlsx file rather than as a BIFF format xls file.
So are you seeing the output on screen?

php_excel07- How to make grow the height of cell based on cell data(xls)

In my application i need to export to xls file in a predefined format.
so I just integrated php_excel2007. I am using one template with a predefined format.
The problem here is the cell data may change dynamically. if data is much bigger than cell height then data is collapsing.
So is ther anyway to increase the height of cell based on content of the cell(in XLX not xlsx)?
The only way is as described in this answer to your previous question on this topic: setting the row height to autofit, and the cell alignment to wrap. This should work the same way, whether you use the Excel5 Writer or the Excel2007 Writer.
$objPHPExcel = new PHPExcel();
// Set some long string values in some cells
$objPHPExcel->getActiveSheet()->getCell('A1')->setValue("This is a large block of text,\ncomprising several lines,\nthat will be set to autofit.");
$objPHPExcel->getActiveSheet()->getCell('A2')->setValue("This is a large block of text that will NOT be set to autofit.");
$objPHPExcel->getActiveSheet()->getCell('B1')->setValue("This is another large block of text without any line breaks, that will be set to autofit.");
$objPHPExcel->getActiveSheet()->getCell('A3')->setValue("This is another large block of text,\ncomprising several lines,\nthat will be set to autofit.");
$objPHPExcel->getActiveSheet()->getCell('A4')->setValue("This is another large block of text without any line breaks, that will be set to autofit but not wrap.");
// Fix the column width to a reasonable size
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
// Set text wrap for cells A1 and B1. This forces the text to wrap, but doesn't adjust the height of the row
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setWrapText(true);
// Set rows 1, 3 and 4 to autofit the height to the size of text
$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
$objPHPExcel->getActiveSheet()->getRowDimension(3)->setRowHeight(-1);
$objPHPExcel->getActiveSheet()->getRowDimension(4)->setRowHeight(-1);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('testAutoHeight.xls');
EDIT
Added comments to the code sample to explain what each called method actually does
setRowHeight to -1 for make auto height (based on cell data)
// auto-size on row 1
$objWorksheet->getRowDimension('1')->setRowHeight(-1);
<?php
error_reporting(E_ALL);
require_once ROOT.'/PHPExcel.php';
function down($details)
{
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load(ROOT."/templates/project_report1.xls");
// Set properties
$p_i=5;$alpa="B";$row_no=5;$mil="";$d_cel="B";
$objPHPExcel->setActiveSheetIndex()
->setCellValue('K1', $details['report_details']['cur_time']);
$objPHPExcel->setActiveSheetIndex()
->setCellValue('C2', 'REPORT START DATE:'.$details['report_details']['start_date'])
->setCellValue('H2', $details['report_details']['details'])
->setCellValue('C3', 'SHOWING:'.$details['report_details']['showing']);
foreach($details as $p_name=>$date){
//thisis to display date at the top
foreach($date as $p1=>$m_name1){
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($d_cel.'4', $p1);$d_cel++;//to display date in the top
}
break;
}
foreach($details as $p_name=>$date){
if($p_name=="report_details")break;
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$p_i, $p_name);
$objPHPExcel->getActiveSheet(0)->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('A5'), 'A'.$p_i );
$objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.$p_i)->getAlignment()->setWrapText(true);
foreach($date as $p=>$m_name){
$mil=$tic=$st=" ";
foreach($m_name as $name=>$val){
if($name=="milestone")
foreach($val as $in_det=>$det){
if($det && isset($det['start_date']))
$mil.=$det['name']."\n".$det['start_date']."\n";
else
$mil.=$det['name'];
}
if($name=="ticket")
foreach($val as $in_det=>$det){
if($det)
$tic.=$det['name']." ".$det['start_date']."\n";
}
if($name=="task")
foreach($val as $in_det=>$det){
if($det)
$st.=$det['name']." ".$det['start_date']."\n";
}
}
$summary=$mil.$tic.$st;
$objPHPExcel->getActiveSheet(0)->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('B5'), $alpa.$p_i );
$objPHPExcel->getActiveSheet(0)->getRowDimension($p_i)->setRowHeight();
$objPHPExcel->getActiveSheet(0)->getStyle('A'.$p_i.':'.'M'.$p_i)->getAlignment()->setWrapText(true);
$objPHPExcel->setActiveSheetIndex(0)->getStyle($alpa.$p_i)->getFont()->setSize(5);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($alpa.$p_i, $summary);
$alpa++;
}
$alpa="B";
$p_i++;
}
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle('Report');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="project_report.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
?>

Categories