php获取excel表格内容,利用PHPExcel如何读取表格中内容

利用PHPExcel如何读取表格的内容呢?话不多说,下面的这篇文章将给大家详细的介绍关于PHPExcel读取表格中内容的方法。

先引入类IOFactory.phprequire_once '../PHPExcel/IOFactory.php';

$filePath = "test.xlsx"; // 测试文件

加载测试文件$inputFileType = PHPExcel_IOFactory::identify($filePath) 判断文件类型

$objReader = PHPExcel_IOFactory::createReader($inputFileType); 实例化类型对象

$objPHPExcel = $objReader->load($filePath); 加载文件

下面主要判断Excel2007和Excel5类型,即xlsx/xlsm/xltx/xltm和xls/xlt格式文件try {

$inputFileType = PHPExcel_IOFactory::identify($filePath);

if ($inputFileType !== "Excel5" && $inputFileType !== "Excel2007" ) {

unlink($filePath) && str_alert(-1,"请确保导入的文件格式正确!");

}

$objReader = PHPExcel_IOFactory::createReader($inputFileType);

$objPHPExcel = $objReader->load($filePath);

} catch(Except

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来回答你的问题。 要在 PHP 输出内容表格可以使用 HTML 的表格标签,例如: ```html <table> <thead> <tr> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> </thead> <tbody> <tr> <td>张三</td> <td>25</td> <td>男</td> </tr> <tr> <td>李四</td> <td>30</td> <td>女</td> </tr> </tbody> </table> ``` 要使用 PHPExcel表格导出到 Excel 文件可以使用 PHPExcel 库的相关函数,例如: ```php // 引入 PHPExcel 库 require_once 'PHPExcel/PHPExcel.php'; // 创建一个新的 Excel 文件 $objPHPExcel = new PHPExcel(); // 设置 Excel 文件的属性 $objPHPExcel->getProperties() ->setCreator('Your name') ->setLastModifiedBy('Your name') ->setTitle('Title') ->setSubject('Subject') ->setDescription('Description') ->setKeywords('Keywords') ->setCategory('Category'); // 添加数据到 Excel 文件 $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', '姓名') ->setCellValue('B1', '年龄') ->setCellValue('C1', '性别') ->setCellValue('A2', '张三') ->setCellValue('B2', '25') ->setCellValue('C2', '男') ->setCellValue('A3', '李四') ->setCellValue('B3', '30') ->setCellValue('C3', '女'); // 输出 Excel 文件到浏览器 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'); ``` 要在页面上预览 Excel 文件可以将上面的代码的最后一行: ```php $objWriter->save('php://output'); ``` 修改为: ```php $objWriter->save('filename.xls'); ``` 然后在页面上使用 `<iframe>` 标签将该文件嵌入进来: ```html <iframe src="filename.xls" width="100%" height="500"></iframe> ``` 这样就可以在页面上预览该 Excel 文件了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值