我尝试编写一个可以从Microsoft Excel中读取并提取值并插入数据库的脚本。我得到了如何执行此操作的概念,如下所示。我知道如何选择并插入数据库。但我不知道如何逐行或特定列从excel中提取值。
Excel文件(示例):
COLUMN A COLUMN B COLUMN C COLUMN D COLUMN E
T3C27811L-01AA L130-27800030P0W11 927700492001 DONE 1/12/14
T3C27811L-02AA L130-27800030P1W11 927700492301 DONE 1/12/14
T3C30811L-01AA L130-30800030P0W11 927700492101 DONE 1/12/14
T3C27811L-02AB L130-27800030P0W21 927700492201 DONE 1/12/14
代码:
**Code Here** - Loop through the excel file - line by line
**Code Here** - Extract the Column A and set it as variable - $ColumnA
#Use the $ColumnA to select from database
my $selectStatement = "select filename from mysqltable " .
"where ColumnA = '$ColumnA'";
#If the $ColumnA not exist
if(!IfExist($selectStatement)) {
**Code Here** - Extract the ColumnB,ColumnC,ColumnD and set it as variable - $ColumnB,$ColumnC,$ColumnD
#Insert the $ColumnA,B,C,D into the database
$sqlStatement = "insert into mysqltable" .
"(ColumnA, ColumnB, ColumnC , ColumnD) " .
"values ('$ColumnA','$ColumnB','$ColumnC','$ColumnD')";
}
$sth = $myDbh->prepare($sqlStatement);
预期产出:
能够遍历excel文件并检索特定值,以便我可以选择或将记录插入数据库。
任何相关的帖子/信息/答案分享?谢谢!