我正在使用 php 和 bootstrap 开发一个 web 应用程序。上传 excel 表是我的应用程序的要求之一。现在我部分实现了它(从 excel 表中提取数据),接下来我想上传这些数据进入数据库表但失败了。这是我的代码
$file = fopen($filename, "r");
$row=1;
while (($Data = fgetcsv($file, 10000, ",")) !== FALSE)
{
echo $Data[0];
}
fclose($file);
这是 $Data[0]
的输出
我想用这些字段将数据存储在数据库表中。我该如何实现?
最佳答案
这是您应该如何操作的示例代码。详情可见here . Excel 阅读器库将为您完成大部分工作
ini_set("display_errors",1);
require_once 'excel_reader2.php';
$data = new Spreadsheet_Excel_Reader("example.xls");
echo "Total Sheets in this xls file: ".count($data->sheets)."<br /><br />";
$html="<table border='1'>";
for($i=0;$i<count($data->sheets);$i++) // Loop to get all sheets in a file.
{
if(count($data->sheets[$i][cells])>0) // checking sheet not empty
{
echo "Sheet $i:<br /><br />Total rows in sheet $i ".count($data->sheets[$i][cells])."<br />";
for($j=1;$j<=count($data->sheets[$i][cells]);$j++) // loop used to get each row of the sheet
{
$html.="<tr>";
for($k=1;$k<=count($data->sheets[$i][cells][$j]);$k++) // This loop is created to get data in a table format.
{
$html.="<td>";
$html.=$data->sheets[$i][cells][$j][$k];
$html.="</td>";
}
$html.="</tr>";
}
}
}
$html.="</table>";
echo $html;
echo "<br />Data Inserted in dababase";
?>
关于php - 使用php将excel表上传到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27031434/