I have this code
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$LastRow = $objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
$row = $LastRow + 1;
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$row, $_POST['name']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$row, $_POST['email'] );
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$row, $_POST['tel']);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$row, $_POST['adresss']);
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('files\Clientes.xlsx');
The problem in here is the always create a new .xlsx file or erase the others rows that are in Excel File, i need Add a new row...
Any ideas ?
$objPHPExcel = $objReader->load("FILE.xlsx");
$objWorksheet = $objPHPExcel->getActiveSheet();
Just open the file
In your current code you are creating a new PHPExcel() object each time. You need to load the existing, then insert a new row. See here for how: Adding a new row with PHPExcel?
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 have this file
https://docs.google.com/spreadsheets/d/1-U3zAECz0TNF-yLzNdzfDyuMzRVAcJrc6L8wni1lDhc/edit?usp=sharing
I am copy this file
and try to get data from this sheet.
$tempLoc = $file;
Yii::import('ext.phpexcel.XPHPExcel');
$objPHPExcel = XPHPExcel::createPHPExcel();
$inputFileType = \PHPExcel_IOFactory::identify($tempLoc);
$objReader = \PHPExcel_IOFactory::createReader($inputFileType);
$ExcelData = $objReader->load($tempLoc);
$sheet = $ExcelData->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
but when i try to get data from this sheet it will display like
image
and you can see in sheet header is different from this array,can someone please help me to slove it,
i tried with
1.$objReader->setInputEncoding('');
2.$objReader->setInputEncoding('UTF-16LE');
I want to automatically generate new excel file if i run this code:
//file.php
$arrtweet[] = array(....);
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load("file/ABCDay-1.xlsx");
$objSheet = $objPHPExcel->getActiveSheet();
$objSheet->setTitle('Data ABC Day-1');
$objSheet->getCell('A1')->setValue('User_ID');
$objSheet->getCell('C1')->setValue('Content');
$objSheet->getCell('D1')->setValue('Timestamp');
$objPHPExcel->getActiveSheet()->fromArray($arrtweet, NULL, 'A2');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,"Excel2007");
$objWriter->save("file/ABCDay-1.xlsx");
What I want is:
PHP will automatically generate new excel file if I run that codes for day-2,day-3...,day-N
. Is that possible? Can you help me? thank you so much :')
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();
How to display the names of the sheets of xls file using PHPexcel?
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0); ???
$objPHPExcel->getSheetNames();
will return a list of all worksheet names
$objWorksheet->getTitle();
will return the name of the worksheet in $objWorksheet
EDIT
How to access a sheet by name/title:
$objWorksheet = $objPHPExcel->getSheetByName('Worksheet1');
its return all sheet in an array
$this->load->library('PHPExcel/Classes/PHPExcel');
$excelReader = PHPExcel_IOFactory::createReaderForFile($fileTempName);
$excelObj = $excelReader->load($fileTempName);
//$worksheet = $excelObj->getSheet(0);
$worksheet = $excelObj->getSheetNames();
echo "<pre>";
print_r($worksheet);
die;