在PHP中擅长json

问题描述 投票:1回答:2

我只是在学习编码,我还不知道。

<!doctype>
<html>
<head>
</head>
<body>
<?php
require_once "Classes/PHPExcel.php";
		$tmpfname = "tabula1.xlsx";
		$excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
		$excelObj = $excelReader->load($tmpfname);
		$worksheet = $excelObj->getSheet(0);//
		$lastRow = $worksheet->getHighestRow();
		
		echo "<table>";
		for ($row = 1; $row <= $lastRow; $row++) {
			 echo "<tr><td>";
			 echo $worksheet->getCell('A'.$row)->getValue();
			 echo "</td><td>";
			 echo $worksheet->getCell('B'.$row)->getValue();
			 echo "</td><tr>";
		}
		echo "</table>";	
		

</body>
</html>

这是我的代码。我需要从excel表中输出json中的数据。我一直在youtube和谷歌搜索解决方案,但到目前为止我找不到任何理解。我该怎么写,我该怎么办?此外,我从这个论坛得到了这个代码,我编辑它,所以它适合我。如果有人能帮我解决这个问题,我将非常感激<3

php json excel
2个回答
5
投票

尝试这样的事情,使用你的循环来创建一个数组,并使用json_encode将它转换为json字符串:

<!doctype>
<html>
<head>
</head>
<body>
<?php
require_once "Classes/PHPExcel.php";
        $tmpfname = "tabula1.xlsx";
        $excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
        $excelObj = $excelReader->load($tmpfname);
        $worksheet = $excelObj->getSheet(0);//
        $lastRow = $worksheet->getHighestRow();

        $data = [];
        for ($row = 1; $row <= $lastRow; $row++) {
             $data[] = [
                'A' => $worksheet->getCell('A'.$row)->getValue(),
                'B' => $worksheet->getCell('B'.$row)->getValue()
             ];
        }

echo json_encode($data);

</body>
</html>

0
投票

只有逻辑没有经过测试

 $result=array();
    for ($row = 1; $row <= $lastRow; $row++) 
    {           
    array_push($result,array($worksheet->getCell('A'.$row)->getValue(),$worksheet->getCell('B'.$row)->getValue()));
        }

    echo json_encode($result);
© www.soinside.com 2019 - 2024. All rights reserved.