读取excel数据并以下面的格式插入

Excel数据表中的数据如下所示。

在这里输入图像说明

我必须读取数据(我会尝试CSV格式,请另外build议)。 使用PHP读取数据…

创build两个表,希望如下所示。

1。

在这里输入图像说明

2。

在这里输入图像说明

  1. 我期待着像这样的ExtJs树。

在这里输入图像说明

我在步骤1中遇到了问题。我应该如何读取我的excel表格,csv文件,以便数据库更新如表1

这里有一个解决scheme,虽然我没有像你一样维护相同的Data_Id(你似乎增加深度和节点的id,但我只是使用行号)。

 <?php $data = <<<EOT Eatables,, ,Fruits, ,,Apple ,,Mango ,Vegetables, ,,Tomatoes ,,Potatoes EOT; $mysqli = new mysqli("localhost", "username", "password", "test"); $dataInsertStatement = $mysqli->prepare("INSERT INTO Data (Data_Id, Data_Value) VALUES (?, ?)"); $treeInsertStatement = $mysqli->prepare("INSERT INTO Tree (parent_id, child_id) VALUES (?, ?)"); $lines = explode("\n", $data); $path = array(); foreach ($lines as $rowNum => $line) { // convert line of csv to array $row = str_getcsv($line); // loop through the row's fields and find the one that's not empty foreach ($row as $column => $value) { if (!empty($value)) { // use row number + 1 as $Data_Id $Data_Id = $rowNum + 1; // track our depth in the tree $path[$column] = $Data_Id; // insert the data $dataInsertStatement->bind_param('is', $Data_Id, $value); $dataInsertStatement->execute(); // check if this node has a parent if (isset($path[$column - 1])) { // connect this node to its parent in the tree $treeInsertStatement->bind_param('ii', $path[$column - 1], $Data_Id); $treeInsertStatement->execute(); } continue; } } } $mysqli->close(); 

就像是

 lastvalues=array(); // To hold the last value on each level data=array(); foreach($lines as $i=>$line){ elems=explode(',',$line); foreach($elems as $n=>$e){ if($e>''){ $data[$i]=$e; lastvalues[$n]=$i; if($n){ // makes no sense for 0th level $tree[]=array('parent'=>$lastvalues[$n-1],child=>$i); } } } } 

应该给你的数据结构。 您可以使用SQL插入而不是$ data和$ tree数组。