当前位置:网站首页>php导出Excel表格
php导出Excel表格
2022-04-23 03:51:00 【撸BUG】
php保存excel表格
一,首要条件
需要下载phpExcel库 放到自己网页代码目,在调用函数require_once(“phpExcel_path”)
即可对即将导出的excel表格进行格式设置。
二,引入对象
$excel = new PHPExcel();
//创建Excel输入对象
$write = new PHPExcel_Writer_Excel5($excel);
壹,创建表头
定义数组
$letter= array(‘A’,‘B’,‘C’,‘D’…);
或者是数字
定义表头数组:
$tableheader = array(‘中文’,‘中文’,‘中文’,‘中文’);
1,写入表头
for($i = 0;$i < count($tableheader);$i++)
{
$excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
}
贰,记录表数据
定义表主体数组:
$tablebody = array(‘数据’,‘数据’,‘数据’,‘数据’,‘数据’);
$number变量是行序号 ,根据需求更改。
for($i = 0;$i < count($tablebody);$i++)
{
$excel->getActiveSheet()->setCellValue("$letter[$i]$number","$tablebody[$i]");
}
三,保存
记录数据就基本完成
最后就是保存到本地
header("Pragma: public");
header("Expires: 0");
ob_end_clean();
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
//header('Cache-Control: max-age=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=$project_code.$datetmp.xls");
header("Content-Transfer-Encoding:binary");
$write->save('php://output')
四,设置表格格式
基础表格保存就如上所诉,需要保存一定格式的表格需要对单元格进行设置。
壹,创建sheet表
$i变量相当于表示sheet表的id号,
$sheet_name变量是表的名字,也可以不用设置,有默认名。
$excel->createSheet(); //创建sheet
$excel->setactivesheetindex($i); //sheet索引
$excel->getActiveSheet()->setTitle($sheet_name);
贰,单元格设置
1,合并单元格
2,单元格背景色
3,字体大小
4,字体颜色
5,字体背景色
6,字体设置
7,单元格宽高
设置B列宽度为 10
$excel->getActiveSheet()->getColumnDimension()->setAutoSize(true);
$excel->getActiveSheet()->getColumnDimension(\'B\')->setWidth(10);
版权声明
本文为[撸BUG]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_45647036/article/details/124247645
边栏推荐
- Who will answer the question?
- Machine translation baseline
- Design and implementation of redis (5): master-slave replication strategy and optimization
- Vs Studio modifie le langage C scanf et d'autres erreurs
- Notes sur l'apprentissage profond (Ⅱ) - - Principe et mise en oeuvre de la fonction d'activation
- PolarMask is not in the models registry
- 知乎有问题,谁来解答?
- Design and implementation of redis (1): understand data structures and objects
- 創下國產手機在海外市場銷量最高紀錄的小米,重新關注國內市場
- Let matlab2018b support the mex configuration of vs2019
猜你喜欢
Mechanical design knowledge point planning
ROS series (III): introduction to ROS architecture
[latex] formula group
Basic knowledge of convolutional neural network
[AI vision · quick review of robot papers today, issue 30] Thu, 14 APR 2022
Shopping mall for transportation tools based on PHP
Wechat applet canvas draws a simple asymptotic color of the dashboard
The art of concurrent programming (3): an in-depth understanding of the principle of synchronized
[AI vision · quick review of NLP natural language processing papers today, issue 29] Mon, 14 Feb 2022
Retrieval question answering system baseline
随机推荐
[AI vision · quick review of NLP natural language processing papers today, issue 28] wed, 1 Dec 2021
Software testing process
【李宏毅2022 机器学习春】hw6_GAN(不懂..)
Installation and configuration of clion under win10
[mathematical modeling] my mathematical memory
Solve the technical problems in seq2seq + attention machine translation
ROS series (IV): ROS communication mechanism series (2): Service Communication
The latest price trend chart and trading points of London Silver
Installation and configuration of MinGW under win10
Basic knowledge of convolutional neural network
The whole process of connecting the newly created unbutu system virtual machine with xshell and xftp
Idea debug debugging tutorial
Several common methods of multithreading
Second kill all interval related problems
知乎有问题,谁来解答?
Design and implementation of redis (1): understand data structures and objects
Openvino only supports Intel CPUs of generation 6 and above
Operating skills of spot gold_ Wave estimation curve
[latex] formula group
Instructions for fastmock