1. We use php to generate an excel document to describe its principle:
The document directory components in excel2007 are:
2. We use the ZipArchive() method to generate a simple excel file.
Usage:
3. The code is as follows:
<?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' ?>"); ?>
After executing php, an excel2007 file will be generated. After renaming the file and compressing it, you can see the generated file, but this version of excel The file is incomplete and cannot be used. To use it, you need to use the excel package to complete a large amount of data writing functions. This move only completes the understanding of ecxcel file generation.