第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

PHPExcel如何將excel文件導(dǎo)入MySQL數(shù)據(jù)庫(kù)?

請(qǐng)問(wèn)將excel導(dǎo)入mysql數(shù)據(jù)庫(kù)怎么導(dǎo)呢?/(ㄒoㄒ)/~~

而且只導(dǎo)入excel表的部分列,有些列的數(shù)據(jù)是不需要導(dǎo)入的,請(qǐng)問(wèn)怎么做?。看笊裾?qǐng)指教!

正在回答

3 回答

改為另外一個(gè)方法咯。

import.php

session_start();
header("Content-type:text/html;charset:utf-8");
//全局變量
$succ_result=0;
$error_result=0;
$file=$_FILES['filename'];
$max_size="2000000";?//最大文件限制(單位:byte)
$fname=$file['name'];
$ftype=strtolower(substr(strrchr($fname,'.'),1));
//文件格式
$uploadfile=$file['tmp_name'];
if($_SERVER['REQUEST_METHOD']=='POST'){
if(is_uploaded_file($uploadfile)){
?if($file['size']>$max_size){
echo?"Import?file?is?too?large";?
exit;
}
?if($ftype!='xls'){
echo?"Import?file?type?is?error";
?exit;???
}
}else{
echo?"The?file?is?not?empty!";
?exit;?
}?
}

?

$dir=dirname(__FILE__);//查找當(dāng)前腳本所在路徑
require?$dir."/db.php";//引入mysql操作類(lèi)文件
$db=new?db($link);//實(shí)例化db類(lèi)?連接數(shù)據(jù)庫(kù)
require_once?$dir."/PHPExcel/PHPExcel.php";//引入PHPExcel
require_once?$dir."/PHPExcel/PHPExcel/IOFactory.php";
require_once?$dir."/PHPExcel/PHPExcel/Reader/Excel5.php";
//stackoverflow?方法
$objReader?=?PHPExcel_IOFactory::createReader('Excel5');
$objReader->setReadDataOnly(true);
$objPHPExcel?=?$objReader->load($uploadfile);?
$objWorksheet?=?$objPHPExcel->getActiveSheet();
$highestRow?=?$objWorksheet->getHighestRow();?
$highestColumn?=?$objWorksheet->getHighestColumn();?
$highestColumnIndex?=?PHPExcel_Cell::columnIndexFromString($highestColumn);?
$rows?=?array();
for?($row?=?2;?$row?<=?$highestRow;?++$row)?{
for?($col?=?0;?$col?<=?$highestColumnIndex;?++$col)?{
$rows[$col]?=?mysql_real_escape_string($objWorksheet->getCellByColumnAndRow($col,?$row)->getValue());
}
//print_r($rows);
mysql_query("set?names?utf8");
mysql_query("INSERT?INTO?`user`(username,class,score)?
VALUES?('$rows[0]',$rows[1],$rows[2])")or?die("執(zhí)行錯(cuò)誤");
?$insert_num=mysql_affected_rows();
?if($insert_num>0){
$succ_result+=1;
}else{
$error_result+=1;
??}
}
echo?"插入成功".$succ_result."條數(shù)據(jù)?。。?lt;br>";
echo?"插入失敗".$error_result."條數(shù)據(jù)?。?!";


0 回復(fù) 有任何疑惑可以回復(fù)我~

昨天還能插入兩行,今天就只能插入一行,這是為什么呢/(ㄒoㄒ)/~~

0 回復(fù) 有任何疑惑可以回復(fù)我~

自己解決了,我寫(xiě)了一個(gè)import.php和import.html,其中的db和dbconfig請(qǐng)參考Veo老師《PHPHExcel的探索之旅課程》

import.html代碼

<!DOCTYPE?html>
<html><head><meta?http-equiv="Content-Type"?content="text/html";?charset="UTF-8">

<meta?name="viewport"?content="width=device-width,?initial-scale=1.0,?minimum-scale=1.0?,?maximum-scale=1.0,?user-scalable=0">
<title>Excel導(dǎo)入phpMyAdmin數(shù)據(jù)庫(kù)</title>
<form?name="frm1"?enctype="multipart/form-data"?action="import.php"?method="post">
???<input?name="filename"?type="file"?/><input?name="submit"?type="submit"?value="導(dǎo)入數(shù)據(jù)庫(kù)"?/>
</form>

import.php代碼

<?php
session_start();
header("Content-type:text/html;charset:utf-8");
//全局變量

	$succ_result=0;
	$error_result=0;
	$file=$_FILES['filename'];
	$max_size="2000000";?//最大文件限制(單位:byte)
	$fname=$file['name'];
	$ftype=strtolower(substr(strrchr($fname,'.'),1));
	?//文件格式
	?$uploadfile=$file['tmp_name'];
	?if($_SERVER['REQUEST_METHOD']=='POST'){
		?if(is_uploaded_file($uploadfile)){
			??if($file['size']>$max_size){
			?echo?"Import?file?is?too?large";?
			?exit;
			?}
			??if($ftype!='xls'){
			?echo?"Import?file?type?is?error";
			??exit;???
			?}
		?}else{
		?echo?"The?file?is?not?empty!";
		??exit;?
		?}?
	?}
	?
	$dir=dirname(__FILE__);//查找當(dāng)前腳本所在路徑
	require?$dir."/db.php";//引入mysql操作類(lèi)文件
	$db=new?db($phpexcel);//實(shí)例化db類(lèi)?連接數(shù)據(jù)庫(kù)
	
	require_once?$dir."/PHPExcel/PHPExcel.php";//引入PHPExcel
	require_once?$dir."/PHPExcel/PHPExcel/IOFactory.php";
	require_once?$dir."/PHPExcel/PHPExcel/Reader/Excel5.php";

	$objReader?=?PHPExcel_IOFactory::createReader('Excel5');//use?excel2007?for?2007?format?
	$objPHPExcel?=?$objReader->load($uploadfile);?
	$sheet?=?$objPHPExcel->getSheet(0);?
	$highestRow?=?$sheet->getHighestRow();?//?取得總行數(shù)?
	$highestColumn?=?$sheet->getHighestColumn();?//?取得總列數(shù)
	??$arr_result=array();
	??$strs=array();

	for($j=2;$j<=$highestRow;$j++)
	?{?
		unset($arr_result);
		unset($strs);
	?for($k='A';$k<=?$highestColumn;$k++)
		{?
		?//讀取單元格
	??$arr_result??.=?$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue().',';
		}
	?$strs=explode(",",$arr_result);
	?//echo?$strs[0];exit;
	?$sql="insert?into?`user`(username,score,class)?values?('$strs[0]',$strs[1],$strs[2])";
	?echo?$sql."<br/>";?
	?mysql_query("set?names?utf8");
	?$result=mysql_query($sql)?or?die("執(zhí)行錯(cuò)誤");

	?$insert_num=mysql_affected_rows();
	??if($insert_num>0){
			$succ_result+=1;
		}else{
			$error_result+=1;
	???}

	}

	echo?"插入成功".$succ_result."條數(shù)據(jù)!?。?lt;br>";
	echo?"插入失敗".$error_result."條數(shù)據(jù)?。?!";

其中的phpexcel類(lèi)庫(kù)的路徑根據(jù)自己的改,數(shù)據(jù)庫(kù)配置你另外寫(xiě)也可以。insert into 語(yǔ)句根據(jù)你自己的情況自己寫(xiě)。

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

PHPExcel如何將excel文件導(dǎo)入MySQL數(shù)據(jù)庫(kù)?

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)