// include PHPExcel library here
$objReader = new PHPExcel_Reader_Excel5();
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load( "file.xls" );
// get excel file rows as array
$rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
Question is: $rowIterator array always have same sequence of elements (rows), as they are in excel file? it is guaranteed?
Related
When using the PHPExcel IOFactory library, I use the example methods to fetch the active sheet.
But it only returns the first 49 Columns and my Columns go up to 70 Columns.
For some reason it stops at column AW (maybe because that is a date column?)
Some code:
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . 'PHPExcel/Classes/');
/** PHPExcel_IOFactory */
include 'PHPExcel/Classes/PHPExcel/IOFactory.php';
$inputFileName = $current;
$inputFileType = 'Excel2007';
$sheetname = 'Sheet1';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$sheetnames = $objReader->listWorksheetNames($inputFileName);
$objReader->setLoadSheetsOnly($sheetnames[0]);
$objPHPExcel = $objReader->load($inputFileName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
Screenshot of my XLS file :
Screenshot of my Debugger:
Ok seems like my XLS file is corrupted. If I try and run an Excel Open and Repair... and I extract all the values, then it only extracts the values up to column AW
Some More Info: The xlsm file was generated using VB code to fill all the data after column AW. So there must be something extra that prevents PHPExcell from reading those fields
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
I created a PHPWriter through PHPExcel and was able to write to my excel file. now after writing to my excel file i want to retrieve the value from cell C9 and display it to screen. What am i doing wrong ? . How can i display the value of C9 ! Thanks
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcelReader = $objReader->load('test.xlsx');
$objPHPExcelReader->setActiveSheetIndex(1);
$objPHPExcel->getActiveSheet()->getCell('C9')->getValue();
$objReader = new PHPExcel_Reader_Excel2007($objPHPExcel);
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