鍍金池/ 問答/PHP/ phpExcel打印數(shù)據(jù)問題

phpExcel打印數(shù)據(jù)問題

1.上傳一個.xlsx文件,服務(wù)器端解析文件,用phpExcel讀取excel中內(nèi)容,打印其中內(nèi)容,只顯示了一行數(shù)據(jù)中的一個,其他都沒有顯示
2.html代碼:
<form name="form1" id="form1">

     <p><input type="file" id="excelImport" value=""  name="excelImport"></p>
      <p><input type="button" name="b1" value="submit" onclick="fsubmit()" /></p>

</form>

js代碼:
function fsubmit(){
    var form=document.getElementById("form1");
    var fd =new FormData(form);
    console.log(fd);
    $.ajax({
         url: "/module/library/control/batch_insert?_token={{csrf_token()}}",
         type: "POST",
         data: fd,
         processData: false,  // 告訴jQuery不要去處理發(fā)送的數(shù)據(jù)
         contentType: false,   // 告訴jQuery不要去設(shè)置Content-Type請求頭
         success: function(response,status,xhr){
            console.log(response);
         }
    });
    return false;
}
php代碼:
public function batch_insert()
{
    $file = $_FILES['excelImport']['tmp_name'];
    $file = iconv('UTF-8', 'GBK',$file);
    var_dump($file);
    \Excel::load($file, function($reader) {
        $data = $reader->toArray();
        var_dump($data);
    });
}

3.
clipboard.png

clipboard.png
4.只顯示了人員照片1,其他的姓名啥的都沒顯示,不知道為啥。

回答
編輯回答
歆久

感覺題主最后貼的代碼也實(shí)在太麻煩,不妨試一下我使用的PHPExcel獲取excel文件內(nèi)容的代碼,成數(shù)組返回:

$tmp_file    = $_FILES['excel_file']['name'];
$fileTmpAddr = $_FILES['excel_file']['tmp_name'];
$extend      = strrchr ($tmp_file,'.');  // 文件后綴名
$targetPath  = "./uploads/excel/";
//上傳后的文件路徑

/*判別是不是.xls或者xlsx文件,判別是不是excel文件*/
if ($extend != ".xls" and $extend != '.xlsx'){
    throw new Exception("不是Excel文件,請重新上傳!", -1);
}else{
    if (!file_exists($targetPath)) {
        @mkdir($targetPath);
        chmod($targetPath, 0777);
    }
    $fileDesAddr = $targetPath.date( "YmdHis" ) . $extend;
    $flag = move_uploaded_file( $fileTmpAddr, $fileDesAddr );

    if( $flag ){
        //獲取導(dǎo)入EXcel的數(shù)據(jù)
        $objPHPExcel = IOFactory::load($fileDesAddr);
        $daoru_data  = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
        
        unlink($fileDesAddr);
        return $daoru_data;
    }
}
2018年9月2日 08:25
編輯回答
凹凸曼

換了一種方法做的,沒有用laravel封裝好的代碼寫,貼上自己的php代碼:
$excel = new PHPExcel();
$file = $_FILES'excelImport';
$filename =time().substr($_FILES['excelImport'['name'],strrpos($_FILES'excelImport','.'));
if(move_uploaded_file($_FILES['excelImport']['tmp_name'], $filename)) {
$objReader =PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel= $objReader->load($filename);
$objWorksheet= $objPHPExcel->getActiveSheet();
$highestRow =$objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex =PHPExcel_Cell::columnIndexFromString($highestColumn);
$excelData =array();
for($row = 1;$row <= $highestRow; $row++) {

  for ($col= 0; $col < $highestColumnIndex; $col++) {  
      $excelData[$row][]=(string)$objWorksheet->getCellByColumnAndRow($col,$row)->getValue();  
   }  

}
var_dump($excelData);
這樣就能打印出自己想要的數(shù)據(jù)了!

2017年5月14日 15:20
編輯回答
青黛色
你需要在你封裝好的phpexcel函數(shù)里面,把你要顯示的變量傳進(jìn)去啊.
2018年4月19日 18:29