phpexcel excel数据插入问题

时间:2012-04-28 20:48:59

标签: php mysql database phpexcel

我有一个excel文件,其中包含15列和248行。我想将此excel文件数据插入数据库。为此,我选择PHPExcel脚本。使用此脚本,我已成功将14列数据插入数据库表。但是15个列名称'Description'包含在html标签或每个单元格中的代码中。当我尝试上传其他14列时,只插入92条记录而不是248.没有15列其他成功插入248行。我正在分享我的代码我告诉我哪里有问题。感谢

>include($HTTP_SERVER_VARS["DOCUMENT_ROOT"]."/my_session.php");<br>
>require_once($HTTP_SERVER_VARS["DOCUMENT_ROOT"]."/includes/Classes/PHPExcel/IOFactory.php");

>$filetype = $_FILES["file"]["type"];<br>
$filename = $_FILES["file"]["name"];<br>
$filetmp = $_FILES["file"]["tmp_name"];<br>
$Ext = strrchr($filename,".");<br>
//set_time_limit(3000);

>$objReader = new PHPExcel_Reader_CSV();<br>
$objPHPExcel = $objReader->load("ebay.csv");<br>

>foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {<br>
    $worksheetTitle     = $worksheet->getTitle();<br>
    $highestRow         = $worksheet->getHighestRow(); <br>
    $highestColumn      = $worksheet->getHighestColumn(); <br>
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);<br>
    $nrColumns = ord($highestColumn) - 64; <br>
>for ($row=2; $row<=$highestRow; $row++) {<br>
        $item_name = $worksheet->getCellByColumnAndRow(3, $row)->getValue();<br>
        $condition = $worksheet->getCellByColumnAndRow(4, $row)->getValue();<br>
        $Category = $worksheet->getCellByColumnAndRow(7, $row)->getValue();<br>
        $Quantity = $worksheet->getCellByColumnAndRow(11, $row)->getValue();<br>
        $StartPrice = $worksheet->getCellByColumnAndRow(14, $row)->getValue();<br>
        $BuyItNowPrice = $worksheet->getCellByColumnAndRow(15, $row)->getValue();<br>
        $Duration = "365";<br>
        $ItemID = $worksheet->getCellByColumnAndRow(29, $row)->getValue();<br>
        $objPHPExcel->getActiveSheet()->getCell('A1')->getValue();<br>
        $PostageServices1 = $worksheet->getCellByColumnAndRow(95, $row)->getValue();<br>
        $PostageCost1 = $worksheet->getCellByColumnAndRow(96, $row)->getValue();<br>
        $PostageServices2 = $worksheet->getCellByColumnAndRow(101, $row)->getValue();<br>
        $PostageCost2 = $worksheet->getCellByColumnAndRow(102, $row)->getValue();<br>
        $PostageServices3 = $worksheet->getCellByColumnAndRow(106, $row)->getValue();<br>
        $PostageCost3 = $worksheet->getCellByColumnAndRow(107, $row)->getValue();<br>
        $DispatchTimeMax = $worksheet->getCellByColumnAndRow(122, $row)->getValue();<br>
        $ReturnsAcceptedOption = $worksheet->getCellByColumnAndRow(195, $row)->getValue();<br>
        $AdditionalDetails = $worksheet->getCellByColumnAndRow(202, $row)->getValue();<br>
        $item_description = stripslashes($worksheet->getCellByColumnAndRow(30, $row)->getValue());<br>

>insertInto("INSERT INTO mytable (`item_name`,`condition`,`Category`,`Quantity`,`StartPrice`,`BuyItNowPrice`,`Duration`,`item_code`,`PostageServices1`,`PostageCost1`,`PostageServices2`,`PostageCost2`,`PostageServices3`,`PostageCost3`,`DispatchTimeMax`,`ReturnsAcceptedOption`,`AdditionalDetails`) VALUES ('$item_name','$condition','$Category','$Quantity','$StartPrice','$BuyItNowPrice','$Duration','$ItemID','$PostageServices1','$PostageCost1','$PostageServices2','$PostageCost2','$PostageServices3','$PostageCost3','$DispatchTimeMax','$ReturnsAcceptedOption','$AdditionalDetails')");
}
}<br>

描述单元格包含html元素,即table,p,b等等...每个excel单元格都很重,因此我需要适当的方法来处理html。在phpexcel中是否有任何函数或方法可以处理这个html单元格数据并从excel中获取并从头到尾插入到数据库中?如果是,请至少给我一个例子。

1 个答案:

答案 0 :(得分:0)

如果您确定Description只是一个包含html标记的文本字符串,那么您应该将其转义(或使用预准备语句)。这是因为字符串可以包含引号字符,并且是标准做法每当在数据库中使用文本字符串时,与PHPExcel无关。