I want to delete sheets from an Excel2005/Excel5 file using PHP. I am using PHPExcel-1.7.9. I am using the following code to delete Excel2007 files as follows
$exceltype="Excel2007";
$excel = PHPExcel_IOFactory::createReader($exceltype);
$excel = $excel->load("ABC.xlsx");
$count = $excel->getSheetCount();
for($i = 0; $i < $count; $i++)
{
$excel->removeSheetByIndex(0);
}
When I use it for Excel5, I get an error
Fatal error: Call to undefined method PHPExcel_Reader_Excel5::getSheetCount()
Potential Issue #1
Use a different variable name for the Reader and for the object that you're loading from the Reader
$exceltype="Excel2007";
$excelReader = PHPExcel_IOFactory::createReader($exceltype);
$excel = $excelReader->load("ABC.xlsx");
Potential Issue #2
It's always sensible to let PHPExcel identify the filetype for you rather than trusting to the file extension
$excel = PHPExcel_IOFactory::load("ABC.xlsx");
Documentation
Related
I want to split slides of one pptx file into seperated pptx files, containing one slide each. The content/text is copied but the layout & styling is not copied. Here is the code.
Can anyone please help ?
<?php
use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\IOFactory;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Alignment;
use PhpOffice\PhpPresentation\Slide\SlideLayout;
$objReader = \PhpOffice\PhpPresentation\IOFactory::createReader('PowerPoint2007');
$objPHPPowerPoint = $objReader->load('a.pptx');
$totalSlides = $objPHPPowerPoint->getSlideCount();
$oMasterSlide = $objPHPPowerPoint->getAllMasterSlides()[0];
$documentProperties = $objPHPPowerPoint->getDocumentProperties();
for ( $count = 0; $count < $totalSlides; $count++ ) {
$objPHPPresentation = new PhpPresentation();
$slide = $objPHPPowerPoint->getSlide( $count );
$background = $slide->getBackground();
$newSlide = $objPHPPresentation->addSlide( $slide );
$newSlide->setBackground ( $background );
$objPHPPresentation->setAllMasterSlides( $oMasterSlide );
$objPHPPresentation->removeSlideByIndex(0);
$oWriterPPTX = \PhpOffice\PhpPresentation\IOFactory::createWriter($objPHPPresentation, 'PowerPoint2007');
$oWriterPPTX->save($count.'.pptx');
}
I don't think it's an issue with your code - more an issue with the underlying libraries - as mentioned here: PhpPresentation imagecreatefromstring(): Data is not in a recognized format - PHP7.2
It ran a test to see if it was something I could replicate - and I was able to. The key difference in my test was in one presentation I had a simple background, and in the other it was a gradient.
This slide caused problems:
But this one was copied over fine:
With the more complex background I got errors like:
PHP Warning: imagecreatefromstring(): Data is not in a recognized format
My code is even less complicated than yours, I just clone the original slideshow and remove all except a single slide before saving it:
for ( $count = 0; $count < $totalSlides; $count++ ) {
$copyVersion = clone $objPHPPowerPoint;
foreach ($copyVersion->getAllSlides() as $index => $slide) {
if ($index !== $count) {
$copyVersion->removeSlideByIndex($index);
}
}
$oWriterPPTX = \PhpOffice\PhpPresentation\IOFactory::createWriter($copyVersion, 'PowerPoint2007');
$oWriterPPTX->save($count.'.pptx');
}
Sorry if this doesn't exactly solve your problem, but hopefully it can help identify why it's happening. The other answer I linked to has more information about finding unsupported images types in your slides.
You can try using Aspose.Slides Cloud SDK for PHP to split a presentation into separate slides and save them to many formats. You can evaluate this REST-based API making 150 free API calls per month for API learning and presentation processing. The following code example shows you how to split a presentation and save slides to PPTX format using Aspose.Slides Cloud:
use Aspose\Slides\Cloud\Sdk\Api\Configuration;
use Aspose\Slides\Cloud\Sdk\Api\SlidesApi;
use Aspose\Slides\Cloud\Sdk\Model;
$configuration = new Configuration();
$configuration->setAppSid("my_client_id");
$configuration->setAppKey("my_client_key");
$slidesApi = new SlidesApi(null, $configuration);
$filePath = "example.pptx";
// Upload the file to the default storage.
$fileStream = fopen($filePath, 'r');
$slidesApi->uploadFile($filePath, $fileStream);
// Split the file and save the slides in PPTX format in the same folder.
$response = $slidesApi->split($filePath, null, Model\SlideExportFormat::PPTX);
// Download files of the slides.
foreach($response->getSlides() as $slide) {
$slideFilePath = pathinfo($slide->getHref())["basename"];
$slideFile = $slidesApi->downloadFile($slideFilePath);
echo $slideFile->getRealPath(), "\r\n";
}
Sometimes it is necessary to split a presentation without using any code. In this case, you can use Online PowerPoint Splitter.
I work as a Support Developer at Aspose.
I need to write in a .xlsx file about 111.100 rows, using fromArray() but I have a strange error
I use phpspreadsheet library
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$columnLetter = 'A';
foreach ($columnNames as $columnName) {
// Allow to access AA column if needed and more
$sheet->setCellValue($columnLetter.'1', $columnName);
$columnLetter++;
}
$i = 2; // Beginning row for active sheet
$columnLetter = 'A';
foreach ($columnValues as $columnValue) {
$sheet->fromArray(array_values($columnValue), NULL, $columnLetter.$i);
$i++;
$columnLetter++;
}
// Create your Office 2007 Excel (XLSX Format)
$writer = new Xlsx($spreadsheet);
// In this case, we want to write the file in the public directory
// e.g /var/www/project/public/my_first_excel_symfony4.xlsx
$excelFilepath = $directory . '/'.$filename.'.xlsx';
// Create the file
$writer->save($excelFilepath);
And I get the exception :
message: "Invalid cell coordinate AAAA18272"
#code: 0
#file: "./vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Coordinate.php"
Can you help me please ?
Excel pages are limited. The limit is huge but still limited. This is a correct filter so you can't write if there is no space for it.
Anyway you shouldnt use excel pages for such a big amount of data, you can try fragmenting it into smaller pieces, but databases should be the way to manipulate such amount of information
I am using php spreadsheet excel reader to read a excel file and do some manipulation.
It was working fine and suddenly started to return value 'General' in case of integer columns
Sample rows from excel:
Code:
$dataFields = array("alias"=>"alias","store_quantity"=>"store_quantity","godown_quantity"=>"godown_quantity","name"=>"name");
$mandatoryFeilds = array("alias","store_quantity","godown_quantity","name");
$fieldsPos = array();
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP1251');
$data->read($_FILES['upload-file']['tmp_name']);
for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++)
{
if ( isset($data->sheets[0]['cells'][1][$j]) == true )
{
$fieldsPos[$data->sheets[0]['cells'][1][$j]] = $j;
if(array_search($data->sheets[0]['cells'][1][$j],$mandatoryFeilds)!==false)
{
unset($mandatoryFeilds[array_search($data->sheets[0]['cells'][1][$j],$mandatoryFeilds)]);
}
}
}
if(count($mandatoryFeilds)>0)
{
die('Some of the mandatory columns are missing in excel');
}
for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++)
{
//printing the output here to check values received from excel which are some anonymous values getting returned
var_dump($data->sheets[0]['cells'][$i]);
}
Output:
array("alias"=>"General","name"=>"Item A","store_quantity"=>"General","godown_quantity"=>"General")
Can anyone help me on this? Any help will be seriously appreciated
The issue happened because i updated my OS from Ubuntu 14 to Ubuntu 16 which updated the Libre Office version from 4.2 to 5
The excel file in Libre 5 is getting read in above discussed manner. So it has to do with Libre version rather than anything from Spreadsheet reader.
I had the same problems. Turned out that those cells was blocked. That was why Spreadsheet reader couldn't read correctly.
The issue was getting on Libre Office 5 as far as I have checked.
Not getting on Windows when editing the Excel sheet.
I handled the issue by reading the raw field of cellsinfo for integer cases:
$data = new JPhpExcelReader($filename);
$id = $data->sheets[0]['cellsInfo'][$i][1]['raw'];
I am done with basic import to database. I have explained in the following example where I got stuck up ...
For example,
I have an xls file named project.xls and it has 6 sheets. I have populated all 6 sheet names in dropdown. If I select sheet2 and click button, it should import sheet2 data into db and sheet3 so on.
How can I do this ...? please help me...
You can access to differents sheets of a file with PHPExcelReader this way:
$filename = "path/to/filename.xls";
$reader = new Spreadsheet_Excel_Reader(); // Your PHPEXCELREDER Class
$reader->setOutputEncoding('UTF8');
// Read XLS File
$reader->read($filename);
$sheet = 2; // Your sheet
// Then walk trough your wanted sheet:
for ($i = 2; $i <= $reader->sheets[$sheet]['numRows']; $i++) {
// Do something
}
I read here http://www.daniweb.com/code/snippet217293.html# it is possible.
What should be activated in PHP.Ini or elsewhere to make this work ? Are there any examples with Excel ?
If you are running php on windows it will be installed by default. There are a few options you can set though. I don't believe they're needed:
http://www.php.net/manual/en/com.configuration.php
As for an example working with excel? Here's a little snippet I found for pulling out a value from an excel spreadsheet:
<?PHP
$filename = "c:/spreadhseet/test.xls";
$sheet1 = 1;
$sheet2 = "sheet2";
$excel_app = new COM("Excel.application") or Die ("Did not connect");
print "Application name: {$excel_app->Application->value}\n" ;
print "Loaded version: {$excel_app->Application->version}\n";
$Workbook = $excel_app->Workbooks->Open("$filename") or Die("Did not open $filename $Workbook");
$Worksheet = $Workbook->Worksheets($sheet1);
$Worksheet->activate;
$excel_cell = $Worksheet->Range("C4");
$excel_cell->activate;
$excel_result = $excel_cell->value;
print "$excel_result\n";
$Worksheet = $Workbook->Worksheets($sheet2);
$Worksheet->activate;
$excel_cell = $Worksheet->Range("C4");
$excel_cell->activate;
$excel_result = $excel_cell->value;
print "$excel_result\n";
#To close all instances of excel:
$Workbook->Close;
unset($Worksheet);
unset($Workbook);
$excel_app->Workbooks->Close();
$excel_app->Quit();
unset($excel_app);
?>
what's your target? Import excel into a DB? You can use PHPExcelReader or dbTube.org
for this task. Here you are.
PHP is platform independend. I think it is not a good idea to break this....in
a long term of view.
greeting
NotALinuxMan