1.我們用php來產生一個excel文檔來講述其原理:
excel2007裡面的文檔目錄組成部分為:
2.我們使用ZipArchive()方法來產生一個簡易的excel檔案。
使用方法:
3.代碼如下:
<?phpheader("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檔案產生的理解。