Create String from object PHPExcel - php

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);

Related

Phpspreadsheet Converted File should have additional new Column containing same value

Following is the code in which I am using the PhpSpreadsheet library to convert xlsx file to csv. I am trying to achieve the ability that the converted file should have one extra column called ClientID which should contain the same value for all the number of rows present in that file. Please let me know is it possible to do? Any help would be highly appreciated.
use \PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use \PhpOffice\PhpSpreadsheet\Writer\Csv;
$reader = new Xlsx();
$uploaded_xls_file = $_FILES['file']['tmp_name'];
$spreadsheet = $reader->load($uploaded_xls_file);
$loadedSheetNames = $spreadsheet->getSheetNames();
$writer = new Csv($spreadsheet);
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$writer->setSheetIndex($sheetIndex);
$writer->setPreCalculateFormulas(false);
$savingpath = $loadedSheetName.'.csv';
$writer->save($savingpath);
//$writer->save($loadedSheetName.'.csv');
}

PHPExcel getRowIterator() and sequence of rows

// 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?

PHPExcell getActiveSheet() not getting all the Columns

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

PHPExcel problem with importing 2 .csv to separate sheets

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

PHPExcel removes Data Validation Option of Excel sheet after editing existing Excel Sheet

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

Categories