简体   繁体   English

如何在mysql中导入Excel文件?

[英]How can I import Excel file in mysql?

I have a code in which I can import .CSV file in my mysql. 我有一个可以在mysql中导入.CSV文件的代码。 But I can not import Excel file. 但是我无法导入Excel文件。 Some garbage values insert in my database while I import Excel file. 导入Excel文件时,一些垃圾值插入到数据库中。

<?php 
if(isset($_POST["Import"]))
{
//First we need to make a connection with the database
include("connection.php");
echo $filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
//$sql_data = "SELECT * FROM prod_list_1 ";
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
//print_r($emapData);
//exit();
$sql = "INSERT into city(city_id,city_name) values ('$emapData[0]','$emapData[1]')";
mysql_query($sql);
}
fclose($file);
echo 'CSV File has been successfully Imported';
header('Location: index.php');
}
else
echo 'Invalid File:Please Upload CSV File';
}
?>
<form enctype="multipart/form-data" method="post" role="form">
<div class="form-group">
<label for="exampleInputFile">File Upload</label>
<input type="file" name="file" id="file" size="150">
<p class="help-block">Only Excel/CSV File Import.</p>
</div>
<button type="submit" class="btn btn-default" name="Import" value="Import">Upload</button>
</form>

Looks like you are trying to read an excel file using some code that is able to parse only csv files, you can't, the format is completely different. 看起来您正在尝试使用一些仅能解析csv文件的代码来读取excel文件,但是不能,格式是完全不同的。

Check for filetype and use a library like PHPExcel if the user sends you an excel spreadsheet. 如果用户向您发送了excel电子表格,请检查文件类型并使用类似PHPExcel的库。

You can use EasyXLS Excel library to import Excel file and than to insert data into MySQL database: 您可以使用EasyXLS Excel库导入Excel文件,然后将数据插入MySQL数据库:

$workbook = new COM("EasyXLS.ExcelDocument");
$rows = $workbook->easy_ReadXLSActiveSheet_AsList($filename);

For more details see this link about importing Excel file to MySQL using EasyXLS . 有关更多详细信息,请参见以下有关使用EasyXLS将Excel文件导入MySQL的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM