Am working with large excel file I have removed limits is there a way I can read through my cells much quicker. I have 6000 rows and reducing the tmp folder in my wamp folder.
set_time_limit(0);
ini_set('memory_limit', '-1');
include'../Classes/PHPExcel.php';
include'../Classes/PHPExcel/IOFactory.php';
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
$cacheSettings = array( ' memoryCacheSize ' =>'8MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
$objReader = new PHPExcel_Reader_Excel2007();
$objPHPExcel = $objReader->load('Book1.xlsx');
$highestRowEM = $objPHPExcel->getActiveSheet()->getHighestRow();
echo $highestRowEM;
for($i=0;$i<$highestRowEM;$i++){
$varval=$objPHPExcel->getActiveSheet()->getCell('A'.$i)->getValue();
if($varval==""){
break;
}
}
echo $i;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('Book1.xlsx');
What to clean up workbooks with worksheets with , in them
`include'../Classes/PHPExcel.php';
include'../Classes/PHPExcel/IOFactory.php';
set_time_limit(0);
ini_set('memory_limit', '-1');
$xlsxfiles=$_SESSION['file'];
echo $xlsxfiles;
echo "<br>";
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = PHPExcel_IOFactory::load('../upload/'.$xlsxfiles);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
////Validation
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$highestRow = $worksheet-> getHighestDataRow();
$columnhighest=$worksheet->getHighestDataColumn();
$columnhighestval=array_search($columnhighest, $alphabet);
echo 'Worksheet - ' , $worksheet->getTitle()." Number of rows: ".$highestRow."<br>";
for($cl=0;$cl<$highestRow+1;$cl++){
for ($ga=1;$ga<$columnhighestval;$ga++){
$letters=strtoupper($alphabet[$ga]);
$clean=$worksheet->getCell($letters.$cl)->getValue();
$cleandone=str_replace(','," ",$clean);
$worksheet->setCellValue($letters.$cl,$cleandone);
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
}
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
$objWriter->setOffice2003Compatibility(true);
$objWriter->save('../upload/'.$xlsxfiles);
echo "Done";
Tip 1:
replace
for($i=0;$i<$highestRowEM;$i++){
$varval=$objPHPExcel->getActiveSheet()->getCell('A'.$i)->getValue();
with
$objSheet = $objPHPExcel->getActiveSheet();
for($i=0;$i<$highestRowEM;$i++){
$varval = $objSheet->getCell('A'.$i)->getValue();
Then you're not calling $objPHPExcel->getActiveSheet() in every iteration of the loop.
Then tell us what you're actually trying to do with the worksheet data, and we may be able to help speed it up a bit more
EDIT
Don't set the cell value unless you have to;
Don't instantiate the Writer until you need it, and definitely not in the loop;
Instead of using $letters and $alphabet, use the routines built-into PHPExcel, or take advantage of PHP's Perl-style character incrementor;
Don't do maths in your loop comparisons
////Validation
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$highestRow = $worksheet->getHighestDataRow();
$columnhighest=$worksheet->getHighestDataColumn();
$columnhighest++;
echo 'Worksheet - ' , $worksheet->getTitle()." Number of rows: ".$highestRow."<br>";
for($cl = 0; $cl <= $highestRow; $cl++){
for ($ga='A'; $ga !== $columnhighest; $ga++){
$clean=$worksheet->getCell($ga.$cl)->getValue();
$cleandone=str_replace(','," ",$clean);
if($clean != $cleandone) {
$worksheet->setCellValue($ga.$cl, $cleandone);
}
}
}
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
Related
I use phpexcel to read a table,but sometimes it can't get value which is not empty.
import('ORG.PHPExcel');
import('ORG.PHPExcel.IOFactory');
import('ORG.PHPExcel.Reader.Excel5');
import('PHPExcel.Reader.Excel2007');
$objReader = new PHPExcel_Reader_Excel5();
$objReader->setReadDataOnly(true);
//use excel2007
$objPHPExcel = new PHPExcel();
$objPHPExcel = $objReader->load($uploadfile);
$sheet = $objPHPExcel->getSheet(1);
$highestRow = $sheet->getHighestRow();
var_dump($highestRow);
$highestColumn = $sheet->getHighestColumn();
var_dump($highestColumn);
$count=0;
$zzdw = M('zzdw');
for($j=4;$j<=$highestRow;$j++)
{
$count++;
$flag=0;
$b= (string)$objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();
if($b==''){
echo('<font color="red"><strong>'.$count.'</strong></font>');
var_dump($b);
exit;
}
}
result
like this, row and col have value,but can't get cell's value.
some files don't have this problem,some have.
file format error?I use ".xls".
I am building a script that takes every xls file from the /uploads folder and converts it to CSV.
But, I am having this error:
Fatal error:
Uncaught Error: Call to undefined function convertXLStoCSV() in C:\xampp\htdocs\Technocripa-php\scandir.php:46 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Technocripa-php\scandir.php on line 46
Here is the code:
$dir = "uploads/*";
foreach(glob($dir) as $file)
{
if(!is_dir($file)) { echo basename($file)."\n";}
//--------------------------
$nameAsString = (string)$file;
require_once('Classes/PHPExcel.php');
$inputfilename = $nameAsString;
$outputfilename = 'convert.csv';
//Usage:
convertXLStoCSV($inputfilename, $outputfilename);
function convertXLStoCSV($infile, $outfile)
{
$fileType = PHPExcel_IOFactory::identify($infile);
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($infile);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->save($outfile);
}
}
I think the error comes from wrong variable usage, but I really can't find a way to fix this. All I want to is store the name of te file in the uploads/ folder in a variable and use it throughout my script.
Here's the program without the LOOP, it works without any errors. Maybe it helps to understand better.
require_once('Classes/PHPExcel.php');
$inputfilename = 'test2.xls';
$outputfilename = 'convert.csv';
//Usage:
convertXLStoCSV($inputfilename, $outputfilename);
function convertXLStoCSV($infile, $outfile)
{
$fileType = PHPExcel_IOFactory::identify($infile);
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($infile);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->save($outfile);
}
Create the function outside of foreach and call it inside like below:
function convertXLStoCSV($infile, $outfile)
{
$fileType = PHPExcel_IOFactory::identify($infile);
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($infile);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->save($outfile);
}
$dir = "uploads/*";
foreach(glob($dir) as $file)
{
if(!is_dir($file)) { echo basename($file)."\n";}
//--------------------------
$nameAsString = (string)$file;
require_once('Classes/PHPExcel.php');
$inputfilename = $nameAsString;
$outputfilename = 'convert.csv';
//Usage:
convertXLStoCSV($inputfilename, $outputfilename);
}
function convertXLStoCSV($infile, $outfile)
{
$fileType = PHPExcel_IOFactory::identify($infile);
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($infile);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->save($outfile);
}
convertXLStoCSV($inputfilename, $outputfilename);
declare the function before using it
I have this test script that is acting just like my production script except production script pulls from a SQL db. I want to set the cell placement using a variable based off the count of an array. In the script below, (which you can copy and run as an example) I want to set the cell number based off how many of the same car that is in an array. So for an example there are 7 BMW's listed in the array. So I want the Comments: data copied to cell A12 using the code $num=$no+5; $cell='A'.$num;
The problem I am having is it does place it on that line but it also places it 7 more times about it. If there are 4 cars of that type it would place it on the right line but also place it 3 times above it. I just want it to place it once at the desired location. Any help would be great. Here is the code:
<?PHP
require_once 'Classes/PHPExcel.php';
include 'Classes/PHPExcel/Writer/Excel2007.php';
$dataArray= array();
$cars=array("Versa","Volt","Volt","Volt","Volt","Volkswagen","Bentley","Benz","BMW","BMW","BMW","BMW","BMW","BMW","BMW","Cobra","Cord","Daewoo","Datsun","Dodge","Dodge","Dixi");
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
while (list($var, $val) = each($cars)) {
if ($val!=$id){
$dataArray = array();
$objWorksheet = new PHPExcel_Worksheet($objPHPExcel);
$objPHPExcel->addSheet($objWorksheet);
$objWorksheet->setTitle(''. $val);
$row_array[$val] = $val;
array_push($dataArray,$row_array);
$no = count($dataArray);
$num=$no+5;
$cell='A'.$num;
$objWorksheet->setCellValue('A1' , $no);
$objWorksheet->setCellValue('A2' , $val);
$objWorksheet->setCellValue($cell , 'Comments:');
$id = $val;
}
else {
$row_array[$val] = $val;
$count=count($cars);
$count2=count($loc);
array_push($dataArray,$row_array);
$no = count($dataArray);
$num=$no+5;
$cell='A'.$num;
$objWorksheet->setCellValue('A2' , $no);
$objWorksheet->setCellValue('A3' , $val);
$objWorksheet->setCellValue($cell , 'Comments:');
$id = $val;
}
}
// Save Excel 2007 file
#echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');
// It will be called file.xls
header('Content-Disposition: attachment; filename="cars.xlsx"');
$objWriter->save('php://output');
Exit;
?>
This fixes the problem.
<?PHP
require_once 'Excel/PHPExcel.php';//path for my config, rewrite for yours
//include 'Classes/PHPExcel/Writer/Excel2007.php'; // not needed, lazy loader job
$cars=array("Versa","Volt","Volt","Volt","Volt","Volkswagen","Bentley","Benz","BMW","BMW","BMW","BMW","BMW","BMW","BMW","Cobra","Cord","Daewoo","Datsun","Dodge","Dodge","Dixi");
$objPHPExcel = new PHPExcel();
$objWorksheet=$objPHPExcel->setActiveSheetIndex(0);
$id='';
$countRows=0;
while (list($var, $val) = each($cars)) {
if ($val!=$id && $id!=''){
$objWorksheet->setTitle($id);
$num=$countRows+5;
$cell='A'.$num;
$objWorksheet->setCellValue($cell , 'Comments:');
$objWorksheet = new PHPExcel_Worksheet($objPHPExcel);
$objPHPExcel->addSheet($objWorksheet);
$id = $val;
$countRows=0;
}//end if
if($id=='') $id=$val;//the first car
$no = ++$countRows;
$objWorksheet->setCellValue('A'.$no , $no);
$objWorksheet->setCellValue('B'.$no , $id);//Versa, Volt, ...
}
if($countRows>0 && $id!=''){// the last car - if $id=='' the workbook is empty
$objWorksheet->setTitle($id);
$num=$countRows+5;
$cell='A'.$num;
$objWorksheet->setCellValue($cell , 'Comments:');
}//end if
// Save Excel 2007 file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
// We'll be outputting an excel file
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // note : use correct mime type (not xls for xlsx)
// It will be called cars.xlsx
header('Content-Disposition: attachment; filename="cars.xlsx"');
$objWriter->save('php://output');
Exit;
?>
iam trying to read an Excel File with PHP Reader, i print the information with an echo of the Highest Row and the Highest Colum, but what i get is the last formatted Column&Row and what i need is just the last cell where data was inserted. Here is my code:
require_once '..\..\..\Common\PHPExcel_1.7.9_doc\Classes\PHPExcel\IOFactory.php';
**code code code**
ReadExcelFile();
function ReadExcelFile(){
$inputFileType = 'Excel2007';
$inputFileName = '../ExcelFiles/test.xlsx';
$sheetname = 'Tabelle1';
try {
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
$objReader->setLoadSheetsOnly($sheetname);
$objPHPExcel = $objReader->load($inputFileName);
}
catch(Exception $e) {
die('Error loading Excel file '.$e->getMessage());
}
$sheet = $objPHPExcel->getSheet(0);
echo $highestRow = $sheet->getHighestRow();
echo $highestColumn = $sheet->getHighestColumn();
}
Iam reading a very extense Macro that is modified once in a while externally (thats why i need to read just only to the last cell where has data) What i did was just a small copy of this excel file and the output was just
"254J"
But my last cell with data is about "210J". Anyone knows how to do it?
$highestRow = $sheet->getHighestDataRow();
$highestColumn = $sheet->getHighestDataColumn();
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.