php - 使用php将excel表上传到mysql

标签 php mysql excel

我正在使用 php 和 bootstrap 开发一个 web 应用程序。上传 excel 表是我的应用程序的要求之一。现在我部分实现了它(从 excel 表中提取数据),接下来我想上传这些数据进入数据库表但失败了。这是我的代码

$file = fopen($filename, "r");
$row=1;

while (($Data = fgetcsv($file, 10000, ",")) !== FALSE)
{
    echo $Data[0];
}
fclose($file);

这是 $Data[0] 的输出

enter image description here

我想用这些字段将数据存储在数据库表中。我该如何实现?

最佳答案

这是您应该如何操作的示例代码。详情可见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/

相关文章:

php - Laravel : failing to add foreign key 中的 SQLSTATE[HY000] 错误

php - 在 codeigniter 中发布后,多选不保留值

mysql - Sql 按组选择所有对话

excel - 将小时、分钟、秒文本列聚合到时间 - Excel

java - 创建 LSL 脚本以将对象添加到 list 中

php - jQuery - 根据视口(viewport)中的部分显示按钮

mysql查询多次返回同一行

sql - mysql递归?

excel - 如何提交登录凭据?

jquery - table2excel 插件和 IE 11