PhpSpreadsheet导入数据例子

使用预先写好的类文件phpoffice.inc.php来直接引入

<?php
require('phpoffice.inc.php');

function import($file)
{
  date_default_timezone_set("Asia/Shanghai");
  $starttime = microtime(1);
  global $conn, $slng;
  $ndatabase='';
  $nidfield='';
  $nfpre='';
  $phpexcel = new Excel();
  $tfile = $phpexcel->import($file, $start_line = 1, $end_line = null, $start_column = 1, $end_column = null);
    $num = count($tfile);
    $long = ceil($num/50);//单次数据导入长度;
    for($i=0;$i<$long;$i++){
      $tsqlstr = '';
      $sql_sub = '';
      for($j=0;$j<50;$j++){
        $num_sub = $i*50 + $j;
        $ttopic = $tfile[$num_sub][0];
        $tcontent = $tfile[$num_sub][1];
        //拼接mysql语句
        if($j==50 - 1) $sql_plus = ';';
        else $sql_plus = ',';
        // && !mm_search_field($ngenre,$ttopic,'topic') 判断是否标题重复,会耗费大量资源,不建议使用.
        if(!ii_isnull($ttopic)) $sql_sub .= "('" . $ttopic . "','".$tcontent."','". ii_now() . "','" . ii_now() . "','0','0','$slng')".$sql_plus;
      }
      //拼接后的mysql语句进行入库
      $tsqlstr = "insert into $ndatabase (topic,content,time,update,hidden,good,lng) values ".$sql_sub;
      ii_conn_query($tsqlstr, $conn);
    }
    $endtime = microtime(1);
    $protime = number_format((($endtime - $starttime) * 1000), 3, '.', '');
    eco $num.'条数据,耗时'.$protime.'ms';

}

 

整个代码压缩包网盘下载:(访问密码wdja)

http://share.menglei.net/f/16922972-579986429-925042

备注:此压缩包内文件可直接使用,无需使用composer进行导入操作,非常方便。