PHPExcel Writer create blank file - php

i try to create new excel file using phpexcel writer
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
include 'Classes/PHPExcel/IOFactory.php';
include 'Classes/PHPExcel/Writer/Excel2007.php';
$inputFileName ='mak.xlsx';
try
{
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader -> load($inputFileName);
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('MyExcel.xlsx');
}
catch(Exception $e)
{
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
?>
i first load exiting excel file then pass the file data into PHPExcel_Writer_Excel2007($objPHPExcel). new file is create as a blank file

Try this:
$sheet = $objPHPExcel->setActiveSheetIndex(0);
$sheet->setCellValue('A1','TEST');
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
In your example $objPHPExcel is empty so that's why you get empty document

Related

how to delete image/ images from excel file using PHPExcel?

Can anybody help to solve this issue, i am new for PHPExcel Libraries,
I want to delete image which is on excel file using PHPExcel.
I did some code here but i am getting error of
Notice: main(): ArrayIterator::next(): Array was modified outside object and internal position
<?php
require_once '../Classes/PHPExcel/IOFactory.php';
require_once '../Classes/PHPExcel.php';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load(files/test.xlsx);
$objPHPExcel->setActiveSheetIndex(1);
$objPHPExcel = $objPHPExcel->getActiveSheet(1)->getDrawingCollection();
foreach ($objPHPExcel as $key => $drawing){
$objPHPExcel->offsetUnset($key);
}
$objReader = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objReader->save('excel-files/new_test.xlsx');
?>

Excel PHP - PHPExcel error

I'm trying to read excel file. but it's showing me error. I tried with this.
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
// Set the Excel file name and path
$inputFileName = 'uploads/aaa.xlsx'; // this is 2007 new format.
// Read your Excel workbook
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
But this error is showing...
Fatal error: Class 'PHPExcel' not found in F:\xampp\htdocs\preme\PHPExcel\Reader\Excel2007.php on line 351
I autoload the "phpoffice/phpexcel" with composer. Make sure you get the same to your directory. If you do not use composer then point to the right directory. Just for demonstration I pointed to the folder down below
<?php
//require_once 'vendor/autoload.php';
require_once 'vendor\phpoffice\phpexcel\Classes\PHPExcel\IOFactory.php';
$inputFileName = 'uploads/aaa.xlsx';
// Read your Excel workbook
try {
$excelReader = PHPExcel_IOFactory::createReaderForFile($inputFileName);
$excelReader->setReadDataOnly();
$excelReader->setLoadAllSheets();
$excelObj = $excelReader->load($inputFileName);
//var_dump($excelObj);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}

ms excel file doesn't load in PHPExcel

ms excel file doesn't load in PHPExcel but when i create excelsheets using libreoffice in linux it's load and upload in database.
$temp=$_FILES['file']['tmp_name'];
$destination = "/var/www/html/recovery";
move_uploaded_file($temp,$destination."/"."recovery"."."."xlsx");
chmod("/var/www/html/recovery/recovery.xlsx", 0777);
$inputFileName ='recovery.xlsx';
try
{
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel =$objReader->load($inputFileName);
}
catch(Exception $e)
{
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
$allDataInSheet = $objPHPExcel->getActiveSheet()- >toArray(null,true,true,true);
$arrayCount = count($allDataInSheet);
what i do for upload all type of excel file in database using PHPExcel.

reading xls and xlsx file using PHPExcel not working

when i reading xls or xlsx using PHPExcel it leads to a browser message The connection was reset
but when i read csv file it works fine.
my code is
/* checking extension*/
if($ext=="xls")
{
$objReader = new PHPExcel_Reader_Excel5();
}
else if($ext=="csv")
$objReader = new PHPExcel_Reader_CSV();
else
$objReader = new PHPExcel_Reader_Excel2007();
/* ........end checking .............. */
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($name);

PHPExcel converting multiple sheet of xlsx to csv

im using this few lines to convert an xlsx, which contain 4sheets to convert to .csv . but it only convert the first sheet of the xlsx file. how can i make it to convert every sheets in the xlsx. heres the code,
error_reporting(E_ALL);
date_default_timezone_set($this->vendor_timezone);
/** PHPExcel_IOFactory */
require_once sfConfig::get('sf_root_dir').'/lib/PHPExcel/IOFactory.php';
$file=sfConfig::get("sf_upload_dir").DIRECTORY_SEPARATOR."temp".DIRECTORY_SEPARATOR."1344500254_MyExcel.xlsx";
// Check prerequisites
//print sfConfig::get("sf_upload_dir").DIRECTORY_SEPARATOR."temp".DIRECTORY_SEPARATOR; exit;
if (!file_exists($file)) {
exit($file."Please run 06largescale.php first.\n");
}
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($file);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->save(str_replace('.xlsx', '.csv',$file));
return "success";
Please check the tutorial here which has step by step code to achieve this
<?php
require_once 'PHPExcel/PHPExcel/IOFactory.php';
$excel = PHPExcel_IOFactory::load("test123.xlsx");
$writer = PHPExcel_IOFactory::createWriter($excel, 'CSV');
$writer->setDelimiter(";");
$writer->setEnclosure("");
$writer->save("test123.csv");
?>
To convert all sheets you must iterate through all worksheets and set it as the active sheet and then write that to a file.
$inFile = 'fileWithMultipleWorksheet.xlsx';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($inFile);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$index = 0;
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$objPHPExcel->setActiveSheetIndex($index);
// write out each worksheet to it's name with CSV extension
$outFile = str_replace(array("-"," "), "_", $worksheet->getTitle()) .".csv";
$objWriter->setSheetIndex($index);
$objWriter->save($outFile);
$index++;
}
Write each sheet in turn to a different file, and then concatenate those files into one: PHPExcel does not provide an option to write multiple sheets to a single CSV file.

Categories