300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > php读取excel存入mysql数据库_PHP将Excel内容导入mysql数据库

php读取excel存入mysql数据库_PHP将Excel内容导入mysql数据库

时间:2023-10-14 04:41:10

相关推荐

php读取excel存入mysql数据库_PHP将Excel内容导入mysql数据库

PHP将Excel内容导入mysql数据库

注:本文技术含量较低,是通过别人写的一个类来完成的,所以不要用金砖砸我。小生我怕怕!!!

1、首先需要一个读取excel的类(在附件中)

2、附件中有3个文件在excel目录中有2文件是读取excel的文件,excel.php是我的一个测试文件

2、excel.php文件测试代码如下:

include'conn.php'; // 数据库连接文件,引用你自己的连接数据库文件

if($_POST[submit]){

if($_POST['upfile']==""){

echo "";

}else{

require_once 'Excel/reader.php';//注意这里的引用位置$data = new Spreadsheet_Excel_Reader();

$data->setOutputEncoding('gbk'); //指定编码很重要$data->read($_POST['upfile']);

error_reporting(E_ALL ^ E_NOTICE);

mysql_query('set names "gbk"');//指定编码很重要for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {

$sql = "INSERT INTO tuhaoinfo VALUES('".//开始读取excel文件的内容并插入数据库

$data->sheets[0]['cells'][$i][1]."','".

$data->sheets[0]['cells'][$i][2]."','". sheets[0]['cells'][$i][3]."','".

$data->sheets[0]['cells'][$i][4]."','".

$data->sheets[0]['cells'][$i][5]."','".

$data->sheets[0]['cells'][$i][6]."','".

$data->sheets[0]['cells'][$i][7]."','".

$data->sheets[0]['cells'][$i][8]."','".

$data->sheets[0]['cells'][$i][9]."')";

$zh=mysql_query($sql);

}

if($zh){

echo "";

}else{//判断添加是否成功echo "";

}

}

}

?>

function checkFileType(str){

var pos = str.lastIndexOf(".");

var lastname = str.substring(pos,str.length);

var resultName=lastname.toLowerCase();

if ('.xls'!=resultName.toString()){alert('只能选择xls文件,您上传的文件类型为'+lastname+',请重新选择);

imp.submit.disabled=true;

}else{

imp.submit.disabled=false;

}

}

4、数据库内容根据自己的实际情况来定,在附件中有一个我自己的数据库字段表

特别注意:前端和后台数据库的字符编码一定要一致。

//exp.php 用于导出

<?php include_once 'conn.php'; $savename = date("YmjHis"); mysql_query("Set Names 'gbk'"); $file_type = "vnd.ms-excel"; $file_ending = "xls"; header("Content-Type: application/$file_type;charset=big5"); header("Content-Disposition: attachment; filename=".$savename.".$file_ending"); header("Pragma: no-cache"); $sql = "Select * from zhbyz_ WHERE riq>='$_GET[ac]' and riq<='$_GET[bc]' ORDER BY `id` DESC"; $result=mysql_query($sql); #print_r($sql); echo("$title\n"); $sep = "\t"; for ($i = 0; $i < mysql_num_fields($result); $i++) { echo mysql_field_name($result,$i) . "\t"; } print("\n"); $i = 0; while($row = mysql_fetch_row($result)) { $schema_insert = ""; for($j=0; $j

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。