I am trying to parse excel files with help of PHPExcel library, here is my function for parsing tables
function readXLS($inputFileName) {
require_once(LIB_PATH.'PHPExcel.php');
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($inputFileName);
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$arr_data = array();
for ($row = 1; $row <= $highestRow; ++$row) {
for ($col = 0; $col <= $highestColumnIndex; ++$col) {
$value=$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
$arr_data[$row-1][$col]=$value;
}
}
return $arr_data;
}
When i am parsing *.xlsx format all works good, but when i uploading for parsing *.xls format i have an error Call to a member function getNamespaces() on a non-object in <b>Z:\home\mysyte\www\trunk\UI\lib\PHPExcel\Reader\Excel2003XML.php
Related
I use PHPspreadsheet to covert a xlsx file to a csv file.
In column "I" I have dates and times like [26-04-2019 07:57:35] how can I change this to [2019-04-26 07:57:35]?
I want to change this for uploading to MYSQL.
use \PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use \PhpOffice\PhpSpreadsheet\Writer\Csv;
$xls_file = "$file";
$reader = new Xlsx();
$spreadsheet = $reader->load($xls_file);
$loadedSheetNames = $spreadsheet->getSheetNames();
$writer = new Csv($spreadsheet);
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$writer->setSheetIndex($sheetIndex);
$writer->save($loadedSheetName.'.csv');
}
You will need to loop through each row of each sheet and change column I to the format needed for MySQL.
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$writer->setSheetIndex($sheetIndex);
$worksheet = $spreadsheet->getActiveSheet();
$highestRow = $worksheet->getHighestRow(); // e.g. 10
for ($row = 1; $row <= $highestRow; ++$row) {
$datenow = $worksheet->getCellByColumnAndRow(9,$row)->getValue();
$worksheet->setCellValue('I' . $row,date('Y-m-d h:i:s',strtotime($datenow)));
} // end of for loop through the sheet
$writer->save($loadedSheetName.'.csv');
}
I have the code that can save the uploaded csv file in the database. But the problem is that it can only save 100-200 rows out of more than 1000 rows. I have an idea to save rows batch by batch instead of a whole but i don't know how to code it.
require_once 'PHPexcel/PHPExcel.php';
$objPHPExcel = PHPExcel_IOFactory::load($file);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
for ($row = 2; $row <= $highestRow; ++ $row) {
$val=array();
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val[] = $cell->getValue();
}
}
}
I am trying to parse an Excel file. I am able to get the contents of individual cells of the excel. I am unable to add the data I got from the cell into an array.
function parseExcel($inputFileName){
$mh = new MainHandler;
// 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());
}
$Arr = array();
// Get worksheet dimensions
$sheet = $objPHPExcel->getActiveSheet();
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
for ($row = 1; $row <= $highestRow; $row++){
$myArray = array();
for($col = 'A' ; $col <= $highestColumn ; $col++){
$a = $sheet->getCell($col.$row);
// echo "\n".$a;
$myArray[] = $a;
}
// $Arr[] = $myArray;
echo json_encode($myArray);
}
// echo json_encode($Arr);
}
The output i get is
[{},{},{},{},{}][{},{},{},{},{}][{},{},{},{},{}]
What should be done to get the populated array as output?
What would be the most efficient way of checking data contained in arrays
I want to validate in the following ways:
if cell a and b are a string
if cell c and d are numbers
if cells are empty
Then how would it be best to catch errors and display them to the user?
I'm using codeigniter and the PHPExcel library
an example of where I'm currently at
public function read_excel()
{
$this->load->library('excel');
$inputFileName = 'test.xlsx';
/** Identify the type of $inputFileName **/
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
/** Advise the Reader that we only want to load cell data **/
$objReader->setReadDataOnly(true);
/** Load $inputFileName to a PHPExcel Object **/
$objPHPExcel = $objReader->load($inputFileName);
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
// Loop through each row of the worksheet
for ($row = 2; $row <= $highestRow; $row++){ // Read a row of data into an array
$rowData = $sheet->rangeToArray('A'.$row.':'.$highestColumn.$row,NULL,TRUE,FALSE);
$cella = strtoupper($rowData[0][0]);
$cellb = strtoupper($rowData[0][1]);
$cellc = $rowData[0][2];
$celld = $rowData[0][3];
}
}
To Answer my own question i eventually went with the below
if (!isset($rowData[0][0],$rowData[0][1],$rowData[0][2])) {
echo "error cells a-c are required.";
}
for ($i=0; $i <$columns ; $i++) {
switch ($rowData[0][$i]) {
case (is_numeric($rowData[0][$i])):
filter_var($rowData[0][$i],FILTER_SANITIZE_NUMBER_INT);
echo $rowData[0][$i];
echo "</br>";
break;
case (is_string($rowData[0][$i])):
$rowData[0][$i] = preg_replace("/[^A-Za-z0-9\-]/"," ",$rowData[0][$i]);
break;
}
}
I'm fairly new to PHPExcel and was wondering if you would be able to assist me with the code below.
Exporting with PHPExcel works perfectly, but when I am importing a xls or xlsx document, it stores the content of the sheet (which is what i want) but also inserts almost 200 blank entries into the Database, from a sheet with 4 rows of data.
I've searched the internet for quite a while now, but cannot seem to find the solution to this.
See my code below:
$storedir = "../uploads/". $_FILES['file']['name'];
$store = move_uploaded_file($_FILES['file']['tmp_name'], $storedir);
$filename = $_FILES['file']['name'];
$srow = $_POST['srow'];
if ($store) {
$objPHPExcel = PHPExcel_IOFactory::load($storedir);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;
for ($row = $srow; $row <= $highestRow-2; ++ $row) {
$val=array();
for ($col = 1; $col < $highestColumnIndex; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val[] = $cell->getValue();
}
$savetodb = mysql_query("INSERT INTO `students` (`gender`, `name`, `surname`) VALUES ('".$val[1]."','".$val[2]."','".$val[3]."')") or die (mysql_error());
}
}
}
I've not added my Connection codes and includes for PHPExcel classes and IOFactories, although they are in the code.
Any assistance would be greatly appreciated.