Home  >  Article  >  Backend Development  >  Generate an excel file using php

Generate an excel file using php

高洛峰
高洛峰Original
2016-11-18 11:09:501230browse

1. We use php to generate an excel document to describe its principle:

The document directory components in excel2007 are:

Generate an excel file using php

2. We use the ZipArchive() method to generate a simple excel file.

Usage:

Generate an excel file using phpGenerate an excel file using php

Generate an excel file using php

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(&#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; ?>");
?>

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn