用php生成一个excel文件

高洛峰
Lepaskan: 2016-11-18 11:09:50
asal
1271 orang telah melayarinya

1.我们用php来生成一个excel文档来讲述其原理:

excel2007里面的文档目录组成部分为:

wKioL1gtNbGzud-QAAEE-_1MOB8811.jpg-wh_651x-s_468450385.jpg

2.我们使用ZipArchive()方法来生成一个简易的excel文件。

使用方法:

wKioL1gtNbGzud-QAAEE-_1MOB8811.jpg-wh_651x-s_468450385.jpgwKiom1gtudmz3u2sAAEuTTwV-g4258.png-wh_651x-s_2584281609.png

wKiom1gtudmz3u2sAAEuTTwV-g4258.png-wh_651x-s_2584281609.png

3.代码如下:

<?php
header("content-type:text/html;charset=utf-8");
//生成一个2007版本的excel文件
//1.实例化一个压缩文档对象
$ex= new ZipArchive();
//2.打开一个excel文件(2007版本)
$ex->open(&#39;./01.xlsx&#39;,ZIPARCHIVE::CREATE);
//3.创建excel文档的各个组成文件(文件目录、xml文件)
$ex->addFromString(&#39;[Content_Types].xml&#39;,"<?xml version=&#39;1.0&#39; charset=&#39;utf-8&#39; ?>");
$ex->addFromString(&#39;_rels/.rels&#39;,"<?xml version=&#39;1.0&#39; charset=&#39;utf-8&#39; ?>");
$ex->addFromString(&#39;docProps/app.xml&#39;,"<?xml version=&#39;1.0&#39; charset=&#39;utf-8&#39; ?>");
$ex->addFromString(&#39;docProps/core.xml&#39;,"<?xml version=&#39;1.0&#39; charset=&#39;utf-8&#39; ?>");
$ex->addFromString(&#39;docProps/custom.xml&#39;,"<?xml version=&#39;1.0&#39; charset=&#39;utf-8&#39; ?>");
$ex->addFromString(&#39;xl/_rels/workbork.xml.rels&#39;,"<?xml version=&#39;1.0&#39; charset=&#39;utf-8&#39; ?>");
$ex->addFromString(&#39;xl/theme/theme1.xml&#39;,"<?xml version=&#39;1.0&#39; charset=&#39;utf-8&#39; ?>");
$ex->addFromString(&#39;xl/theme/worksheets/sheet1.xml&#39;,"<?xml version=&#39;1.0&#39; charset=&#39;utf-8&#39; ?>");
$ex->addFromString(&#39;xl/theme/worksheets/sheet2.xml&#39;,"<?xml version=&#39;1.0&#39; charset=&#39;utf-8&#39; ?>");
$ex->addFromString(&#39;xl/theme/worksheets/sheet3.xml&#39;,"<?xml version=&#39;1.0&#39; charset=&#39;utf-8&#39; ?>");
$ex->addFromString(&#39;xl/styles.xml&#39;,"<?xml version=&#39;1.0&#39; charset=&#39;utf-8&#39; ?>");
$ex->addFromString(&#39;xl/workbook.xml&#39;,"<?xml version=&#39;1.0&#39; charset=&#39;utf-8&#39; ?>");
?>
Salin selepas log masuk

执行php后会生成一个excel2007文件,把此文件改名压缩后就可以看到生成的文件,但此版本的excel文件并不完整,不能使用,要使用还需要借助excel包来完成大量的数据写入功能。此举只是完成ecxcel文件生成的理解。

Label berkaitan:
php
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan