-
Notifications
You must be signed in to change notification settings - Fork 0
/
tojson.php
68 lines (58 loc) · 2.45 KB
/
tojson.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
// Handles uploading and transform to json.
ini_set("memory_limit", "256M");
require_once './Classes/PHPExcel/IOFactory.php';
if(!isset($_FILES['excelfile'])) {
echo json_encode((object) array('status' => 'error', 'code' => 'InvalidParameter', 'message' => 'The excelfile parameter not found'));
exit;
}
$inputFileName = $_FILES['excelfile']['tmp_name'];
/** 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);
if($inputFileType == 'CSV' || $inputFileType == 'csv') {
ini_set("auto_detect_line_endings", true);
}
/** Advise the Reader that we only want to load cell data **/
//$objReader->setReadDataOnly(true); // We can't do this, because we want to read format data...
/** Load $inputFileName to a PHPExcel Object **/
$objPHPExcel = $objReader->load($inputFileName);
$excelArray = array();
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$worksheetArray = array();
//array_push($worksheetArray, $worksheet->getTitle());
foreach ($worksheet->getRowIterator() as $row) {
$rowArray = array();
//array_push($excelArray, $row->getRowIndex());
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
foreach ($cellIterator as $cell) {
//if (!is_null($cell)) {
// array_push($excelArray, $cell->getCalculatedValue());
//}
try {
$dataType = $cell->getDataType();
// convert date...
$isDate = PHPExcel_Shared_Date::isDateTime($cell);
if($isDate) {
$dataType = 'date';
$calculatedValue = PHPExcel_Shared_Date::ExcelToPHPObject($cell->getValue())->format('Y-m-d');
} else {
$calculatedValue = $cell->getCalculatedValue();
}
if($dataType == 's') $dataType = 'string';
if($dataType == 'n') $dataType = 'number';
$cellObj = array("column" => $cell->getColumn(), "row" => $cell->getRow(), "dataType" => $dataType,
"value" => $cell->getValue(), "formattedValue" => $cell->getFormattedValue(), "calculatedValue" => $calculatedValue);
array_push($rowArray, $cellObj);
} catch(Exception $e) {
array_push($rowArray, "NA");
}
}
array_push($worksheetArray, $rowArray);
}
array_push($excelArray, $worksheetArray);
}
echo json_encode((object) array('status' => 'success', 'data' => $excelArray));
?>