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);
Related
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
I am using PHPExcel to read excel file in php,it is working fine with xlsx but when try to read xls file,It shows an error
Fatal error: Call to undefined method PHPExcel_Reader_CSV::setReadDataOnly() in /var/www/....
Here is my code
$file_path='/var/www/html/site/sample.xls';
$inputFileType = PHPExcel_IOFactory::identify( $file_path);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
/** Load $inputFileName to a PHPExcel Object **/
$objPHPExcel = $objReader->load( $file_path);
$total_sheets=$objPHPExcel->getSheetCount();
$allSheetName=$objPHPExcel->getSheetNames();
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
for ($row = 1; $row <= $highestRow;++$row)
{
for ($col = 0; $col <$highestColumnIndex;++$col)
{
$value=$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
$arraydata[$row-1][$col]=trim($value);
}
}
The file that you're loading is identified as a CSV file, even though it has an .xls extension... it is quite a common practise for some developers to save files formatted as csv or with html markup with a .xls extension, but that doesn't make them BIFF-format .xls files.
The setReadDataOnly() method isn't available for the CSV Reader, because a CSV file cannot contain anything other than data.
The most recent versions of PHPExcel provide a stub for setReadDataOnly() in the CSV Reader to prevent an error in this situation, and I'd certainly recommend that you upgrade to the latest code; but if you can't do that, then the simplest fix for you is simply to wrap the call to setReadDataOnly() in an if test:
$inputFileType = PHPExcel_IOFactory::identify( $file_path);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
if ($inputFileType !== 'CSV') {
$objReader->setReadDataOnly(true);
}
For binary Excel files (xls) you may have better luck using the (old) PHP-ExcelReader. It's the most reliable one I've found so far.
I am using PHPExcel to read its content based on no.of rows present.
To find no.of rows I am using following function.
$objSheet->getHighestRow();
It is working fine for .xlsx files.
But it is NOT working in .xls.
Fatal error: Call to a member function getHighestRow() on a non-object
So how i can get no.of rows of excel ?
PHP Code:
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
/** PHPExcel_IOFactory */
include 'www/PHPExcelReader/Classes/PHPExcel/IOFactory.php';
include 'www/PHPExcelReader/Classes/PHPExcel/Classes/PHPExcel.php';
try {
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_sqlite3;
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setReadDataOnly(true);
$objReader->setLoadSheetsOnly( array("Sheet1") );
$objPHPExcel = $objReader->load($inputFileName);
$objSheet = $objPHPExcel->getActiveSheet();
$no_of_rows=$objSheet->getHighestRow();
//echo "no_of_rows=$no_of_rows";
I want both xls & .xlsx to be supported... Please guide me.
I solved this problem myself.
It is due excel version incompatibility.
Here is the code to resolve this issue.
/** Identify the type of $inputFileName **/
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
/** Create a new Reader of the type that has been identified **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
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.
I need to aggregate 4 CSV files into a single, Excel workbook using PHPExcel.
Working on a single CSV file and a mono-spreadsheet workbook all works fine.
Using more than one CSV, I'm unable to get each CSV file into a seperate sheet.
How can I achieve this using PHPExcel?
There is an example of this in the Documentation/Examples/Readers directory in the SVN repository for PHPEXcel: It's Example #13
include 'PHPExcel/IOFactory.php';
$inputFileType = 'CSV';
$inputFileNames = array('./example1.csv','./example2.csv','./example3.csv','./example4.csv');
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$inputFileName = array_shift($inputFileNames);
$objPHPExcel = $objReader->load($inputFileName);
$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));
foreach($inputFileNames as $sheet => $inputFileName) {
$objReader->setSheetIndex($sheet+1);
$objReader->loadIntoExisting($inputFileName,$objPHPExcel);
$objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME));
}
$loadedSheetNames = $objPHPExcel->getSheetNames();
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$objPHPExcel->setActiveSheetIndexByName($loadedSheetName);
echo $loadedSheetName,PHP_EOL;
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData);
echo PHP_EOL;
}
You didn't specify exactly where the problem is...
For multiple worksheet excel file proceed as following:
Load CSV file
Create new worksheet
Write CSV to new worksheet
Go to 1
From docs:
If you need to create more worksheets in the workbook, here is how:
$objWorksheet1 = $objPHPExcel->createSheet();
$objWorksheet1->setTitle('Another sheet');
To set active worksheet:
$objWorksheet1->setActiveSheetIndex($index);