$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?
Related
I'm using PHPExcel which I've used before many times. The problem I have now is when reading Excel2007 files (.xlsx - format). What I'm doing is simply looping the the .xlsx file and creating an array by row/column and then print_r()-ing the results after the read operation to make sure the data output is good before importing it into a MySQL database.
Now when reading the Excel2007 .xlsx file (6MB) the output fails, but whats interesting is if I save the file as the older format .xls (1992-2004 - Excel5) the file becomes larger (16MB) but outputs correctly. This made me think originally it wasn't a memory problem since the older larger .xls file (16MB) ran with no problems and was almost 3x the size of the .xlsx file (6MB).
For test purposes I then copied 25 rows of the 30,000 in the .xlsx (6MB) file and created a new Excel2007 .xlsx and ran the import against the smaller 25 row data-set and it output correctly. This then led me think that it is a memory problem however related specifically to the .xlsx format...
I'm running the server on Amazon Web Services and have C4.Xlarge (16-core, 30GB RAM) so I should have plenty of resources to run this operation.
Question: Why does my output fail when reading a smaller .xlsx file vs a larger .xls file, but then succeed with a smaller .xlsx (25-row) file?
//PHP Function
function parse_xls($file){
ini_set('memory_limit','-1');
$type = PHPExcel_IOFactory::identify($file);
$reader = PHPExcel_IOFactory::createReader($type);
$reader->setReadDataOnly(true);
$xls = $reader->load($file);
$sheet = $xls->getActiveSheet();
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
for($row=2; $row <= ($highestRow+2); $row++){
$import[$row] = [];
for($col=0; $col < $highestColumnIndex; $col++){
$result = $sheet->getCellByColumnAndRow($col, $row)->getValue();
array_push($import[$row],$result);
}
}
print_r($import);
die();
}
for big files i use chunkReadFilter
$iChunkSize=1000;
for($iStartRow = $row_start; $iStartRow <= $totalRows; $iStartRow += $iChunkSize) {
$objReader = $oExcel->SetCreateReader();
$oChunkFilter = new chunkReadFilter();
$objReader->setReadFilter($oChunkFilter);
$oChunkFilter->setRows($iStartRow,$iChunkSize);
$objReader->setReadFilter($oChunkFilter);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($files['path']);
$objPHPExcel->setActiveSheetIndex($iList);
$sFromCell = 'A'.$iStartRow;
$aData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,false,$sFromCell);
// free memory
unset($objPHPExcel);
unset($objReader);
unset($oChunkFilter);
// parse data
foreach ($aData as $sKey => $aValue) {
...
}
// real data rows
if (count($aData) < $iChunkSize) {
unset($aData);
break;
}
unset($aData);
}
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');
When I try to set the value of an cell with a formula, e.g.: setCellValue('C1', '=A1+B1'), the generated file don't have the calculated value for the cells.
I have the following script:
<?php
require_once '../vendor/autoload.php';
date_default_timezone_set('America/Sao_Paulo');
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 5)
->setCellValue('B1', 10)
->setCellValue('C1', '=A1+B1');
echo $objPHPExcel->getActiveSheet()->getCell('C1')->getCalculatedValue() . PHP_EOL;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('a.xlsx');
When I open a.xlsx with libreoffice, the cell C1 shows the string "0". The odd thing is that when I click the cell it actually shows "=A1+B1", but not the final result.
How do I get PHPExcel to work with formulas properly?
I had the same problem with LibreOffice 5.0.5.2.
Changing the following option in LibreOffice Calc worked for me:
Go to Tools/Options.../LibreOffice Calc/Formula
In section "Recalculation on file load", "Excel 2007 and newer", change the value to "Always recalculate"
Found on
https://ask.libreoffice.org/en/question/12165/calc-auto-recalc-does-not-work/
I am using PHPExcel for editing existing excel sheets.
I had already set up a Data Validation method in my excel sheet as below.
When i edit this excel using PHPExcel, this Excel Specific Data Validation vanishes.
Can anyone help me to overcome this issue. I need to edit the excel without changing its functionality.
My PHP code:
//load existing template..
$objPHPExcel = PHPExcel_IOFactory::load('www/PHPExcelReader/Excel_Uploads/sample.xls');
// Set document properties
$objPHPExcel->getProperties()->setCreator("Logic Item")
->setLastModifiedBy("Logic")
->setTitle("List");
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('AZ1', $combination);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$file_name='sample.xls';
$objWriter->save($file_name);
echo "Excel is ready to download now...";
From spreadsheet files that I've just created with data validation on cell A1 applying values from a list in $G1:$G4 and the following code:
$inputFileType = 'Excel5';
$inputFileName = './15datavalidationUnpopulated.xls';
$outputFileType = 'Excel5';
$outputFileName = './15datavalidationPopulated.xls';
$objPHPExcelReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objPHPExcelReader->load($inputFileName);
$objPHPExcel->getActiveSheet()->setCellValue('G1', "India")
->setCellValue('G2', "America")
->setCellValue('G3', "China")
->setCellValue('G4', "Russia");
$objPHPExcelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,$outputFileType);
$objPHPExcel = $objPHPExcelWriter->save($outputFileName);
works with both .xls and .xlsx files, showing India, America, China and Russia in the data validation dropdown list for cell A1
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