1、首先創(chuàng)建要插入100萬數(shù)據(jù)的表格,隨機(jī)產(chǎn)生數(shù)字。
10年積累的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有臨漳免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
2、其次創(chuàng)建存儲過程,并向表中插入數(shù)據(jù),調(diào)用存儲過程進(jìn)行插入數(shù)據(jù),插入300萬條數(shù)據(jù),且分幾次插入。
3、最后導(dǎo)出數(shù)據(jù)到csv,使用函數(shù)selectxxxintooutfile,其中outfile后面的路徑可以是Windows或macOS或者Linux。
php導(dǎo)入CSV文件:
$line_number = 0;
$handle = fopen("1.xls","r");
while ($data = fgetcsv ($handle, 100000, ",")) {
if($line_number == 0){
$line_number++;
continue;
}
//z這樣就可以去掉表頭的那一行
for ($i = 0; $i count($data); $i++) {
$ziduan = $data[$i];
}
}
2.php導(dǎo)出CSV文件
header( "Cache-Control: public" );
header( "Pragma: public" );
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=txxx.csv");
header('Content-Type:APPLICATION/OCTET-STREAM');
ob_start();
$header_str = ?iconv("utf-8",'gbk',"信息id,標(biāo)題,名稱,電話,QQ,Email,內(nèi)容,時間\n");
$file_str="";
$mysqli= new mysqli('localhost','root','','test');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$sql='select * ?from messages';
$mysqli-query("set names utf8 ;");
$result=$mysqli-query($sql);
if($result){
while ($row = mysqli_fetch_assoc($result)){
$file_str.= $row['id'].','.$row['title'].','.$row['name'].','."'{$row['telephone']}'".','.$row['qq'].','.$row['email'].','.str_ireplace(',',',',$row['content']).','.$row['retime']."\n";
}
}else{
echo "nonono!!!";
}
$file_str= ?iconv("utf-8",'gbk',$file_str);
ob_end_clean();
echo $header_str;
echo $file_str;
?
思路:
讀取csv文件,每讀取一行數(shù)據(jù),就插入數(shù)據(jù)庫
示例
文件夾結(jié)構(gòu)
/
file.csv????//csv大文件,這里只模擬三行數(shù)據(jù),不考慮運(yùn)行效率(PS:csv文件格式很簡單,文件一般較小,解析很快,運(yùn)行效率的瓶頸主要在寫入數(shù)據(jù)庫操作)
index.php????//php文件
file.csv
singi,20
lily,19
daming,23
index.php
/**
*?讀取csv文件,每讀取一行數(shù)據(jù),就插入數(shù)據(jù)庫
*/
//獲取數(shù)據(jù)庫實(shí)例
$dsn?=?'mysql:dbname=test;host=127.0.0.1';
$user?=?'root';
$password?=?'';
try?{
$db?=?new?PDO($dsn,?$user,?$password);
}?catch?(PDOException?$e)?{
echo?'Connection?failed:?'?.?$e-getMessage();
}
//讀取file.csv文件
if?(($handle?=?fopen("file.csv",?"r"))?!==?FALSE)?{
while?(($row?=?fgetcsv($handle,?1000,?","))?!==?FALSE)?{
//寫入數(shù)據(jù)庫
$sth?=?$db-prepare('insert?into?test?set?name=:name,age=:age');
$sth-bindParam(':name',$row[0],PDO::PARAM_STR,255);
$sth-bindParam(':age',$row[1],PDO::PARAM_INT);
$sth-execute();
}
fclose($handle);
}
數(shù)據(jù)表
CREATE?TABLE?`test`?(
`id`?INT(10)?UNSIGNED?NOT?NULL?AUTO_INCREMENT,
`name`?VARCHAR(255)?NULL?DEFAULT?''?COLLATE?'utf8mb4_bin',
`age`?INT(10)?NULL?DEFAULT?'0',
PRIMARY?KEY?(`id`)
)
COLLATE='utf8mb4_bin'
ENGINE=InnoDB;
運(yùn)行結(jié)束后,數(shù)據(jù)庫中會插入csv中的三行數(shù)據(jù)
(一)phpexcel文件導(dǎo)出:
步驟1,引入文件require APPPATH.'/libraries/PHPExcel.PHP';
步驟2, 實(shí)例化PHPEXCEL對象 $objPHPExcel=new PHPExcel();
步驟3, 設(shè)置表頭 $column = array('A','B','C'); $line = array('詞語','頻次','詞性');
//填充表頭信息
for($i = 0;$i count($tableheader_all);$i++) {
$objPHPExcel-getActiveSheet()-setCellValue("$letter[$i]1","$tableheader[$i]");
//上一行中"$letter[$i]1"表示第“1”行第“$i”列
}
步驟4, 填充數(shù)據(jù)
for ($i = 2;$i = count($data) + 1;$i++) {//因?yàn)閮?nèi)容是從第二行開始的,所以i=2表示從第二行開始填充數(shù)據(jù)
$j = 0;
foreach ($tmp as $key=$val ){
$objPHPExcel-getActiveSheet()-setCellValue("$letter[$j]$i",$data[$i-2][$val]); //數(shù)據(jù)是從第一條開始但是i的初值是2所以想從第一條開始應(yīng)該是$data[$i-2]開始
$j++;
}
}
步驟5,寫進(jìn)excel中并輸出
$write = new PHPExcel_Writer_Excel5($objPHPExcel);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download")
header('Content-Disposition:attachment;filename="數(shù)據(jù)導(dǎo)出_詞云圖.xls"');
header("Content-Transfer-Encoding:binary");
$write-save('php://output');
(一)CSV文件導(dǎo)出:
csv文件導(dǎo)出最令人頭疼的問題就是編碼問題,現(xiàn)在分享一下我的經(jīng)驗(yàn)
首先看服務(wù)器是Linux的還是windows的 如果是windows的服務(wù)器那么編碼問題只能借助于mb_convert_encoding()或者是iconv兩個函數(shù)相互轉(zhuǎn)換中文編碼
如果服務(wù)器是linux的 那么很簡單 utf-8: setlocale(LC_ALL, ‘en_US.UTF-8′); 簡體中文:setlocale(LC_ALL, ‘zh_CN');
下面來說csv導(dǎo)出步驟:
$result = mysql_query("select * from student order by id asc");
$str = "姓名,性別,年齡\n";
$str = iconv('utf-8','gb2312',$str);
while($row=mysql_fetch_array($result)){
$name = iconv('utf-8','gb2312',$row['name']); //中文轉(zhuǎn)碼
$sex = iconv('utf-8','gb2312',$row['sex']);
$str .= $name.",".$sex.",".$row['age']."\n"; //用引文逗號分開
}
$filename = date('Ymd').'.csv'; //設(shè)置文件名
export_csv($filename,$str); //導(dǎo)出
要將數(shù)據(jù)導(dǎo)出到本地即下載,需要修改header信息,代碼如下:
function export_csv($filename,$data) {
header("Content-type:text/csv");
header("Content-Disposition:attachment;filename=".$filename);
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
echo $data;
}