溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何在PHP5.6中讀寫excel表格文件

發(fā)布時(shí)間:2021-05-07 16:55:04 來源:億速云 閱讀:171 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)如何在PHP5.6中讀寫excel表格文件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

讀取excel文件:

第一步、下載開源的PHPExcel的類庫文件

第二步、讀取的基本代碼示例:

<?php
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/IOFactory.php';
require_once 'Classes/PHPExcel/Reader/Excel5.php';
$file_url = './excel/phpLv.xls';
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load($file_url);
//設(shè)置當(dāng)前活動(dòng)的工作表
$objPHPExcel->setActiveSheetIndex(1);
//拿到當(dāng)前活動(dòng)的表。以后操作就用這個(gè)了。勞資才不喜歡鏈?zhǔn)讲僮?,太難看了好不啦
$activeSheet = $objPHPExcel->getActiveSheet();
//當(dāng)前表最大行數(shù)
$highestRow = $activeSheet->getHighestRow();
//當(dāng)前表最大列數(shù)
$highestColumn = $activeSheet->getHighestColumn();
echo "最大列:$highestColumn";
echo "最大行:$highestRow";
echo '<hr/>';
$cell = function ($cell) use ($activeSheet) {
  return $activeSheet->getCell("$cell")->getValue();
};
$str1 = $cell('A13');
echo $str1;
exit;

導(dǎo)出excel表格文件:

第一步、同上也是先下載PHPExcel類庫文件。

第二步、導(dǎo)出excel文件的示例代碼:

//--------------------------------導(dǎo)出excel文件--------------------------------
require_once './Classes/PHPExcel.php';
$objPHPExcel=new PHPExcel();
//一些關(guān)于excel文件的描述。在Classes/PHPExcel/DocumentProperties.php中有更多選項(xiàng)
$prop = $objPHPExcel->getProperties();
$prop->setCreator('sweat_xiaoMa');
$prop->setLastModifiedBy('xiaoma');
$prop->setTitle('Office 2007 XLSX Document');
$prop->setSubject('Office 2007 XLSX Document');
$prop->setDescription('Document for Office 2007 XLSX, generated using PHP classes.');
$prop->setKeywords('office 2007 openxml php');
$prop->setCategory('Result file');
//設(shè)置使用的當(dāng)前的工作表的索引
$objPHPExcel->setActiveSheetIndex(0);
//然后就可以設(shè)置單元格上的內(nèi)容了。
$activeSheet = $objPHPExcel->getActiveSheet();
$activeSheet->setCellValue('A1','學(xué)號(hào)');
$activeSheet->setCellValue('B1','年級');
$activeSheet->setCellValue('C1','班級');
$activeSheet->setCellValue('D1','姓名');
$activeSheet->setCellValue('E1','性別');
//給當(dāng)前使用的工作表設(shè)置標(biāo)題。
$activeSheet->setTitle('工作表1啦啦啦');
//文件名字。下面的header中用到。
$filename = '學(xué)生信息統(tǒng)計(jì)表_'.date('Y-m-dHis');
/*
*生成xlsx文件
*/
// header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
// header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
// header('Cache-Control: max-age=0');
// $objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
/*
*生成xls文件
*/
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

看完上述內(nèi)容,你們對如何在PHP5.6中讀寫excel表格文件有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI