用php生成一個excel文件(原理)


1.我們用php來生成一個excel文檔來講述其原理:

excel2007里面的文檔目錄組成部分為:

2.我們使用ZipArchive()方法來生成一個簡易的excel文件。

使用方法:

3.代碼如下:

<?php
header("content-type:text/html;charset=utf-8");
//生成一個2007版本的excel文件
//1.實例化一個壓縮文檔對象
$ex= new ZipArchive();
//2.打開一個excel文件(2007版本)
$ex->open('./01.xlsx',ZIPARCHIVE::CREATE);
//3.創建excel文檔的各個組成文件(文件目錄、xml文件)
$ex->addFromString('[Content_Types].xml',"<?xml version='1.0' charset='utf-8' ?>");
$ex->addFromString('_rels/.rels',"<?xml version='1.0' charset='utf-8' ?>");
$ex->addFromString('docProps/app.xml',"<?xml version='1.0' charset='utf-8' ?>");
$ex->addFromString('docProps/core.xml',"<?xml version='1.0' charset='utf-8' ?>");
$ex->addFromString('docProps/custom.xml',"<?xml version='1.0' charset='utf-8' ?>");
$ex->addFromString('xl/_rels/workbork.xml.rels',"<?xml version='1.0' charset='utf-8' ?>");
$ex->addFromString('xl/theme/theme1.xml',"<?xml version='1.0' charset='utf-8' ?>");
$ex->addFromString('xl/theme/worksheets/sheet1.xml',"<?xml version='1.0' charset='utf-8' ?>");
$ex->addFromString('xl/theme/worksheets/sheet2.xml',"<?xml version='1.0' charset='utf-8' ?>");
$ex->addFromString('xl/theme/worksheets/sheet3.xml',"<?xml version='1.0' charset='utf-8' ?>");
$ex->addFromString('xl/styles.xml',"<?xml version='1.0' charset='utf-8' ?>");
$ex->addFromString('xl/workbook.xml',"<?xml version='1.0' charset='utf-8' ?>");
?>

執行php后會生成一個excel2007文件,把此文件改名壓縮后就可以看到生成的文件,但此版本的excel文件並不完整,不能使用,要使用還需要借助excel包來完成大量的數據寫入功能。此舉只是完成ecxcel文件生成的理解。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM