PHP Excel Import and Export Method

Source: Internet
Author: User
Tags echo date php excel

Original Author: podcasts on the iceberg
When I saw this article, I was surprised by the patience of the original author. Although we used some of them at ordinary times, they did not list all of them. When I was writing an excel file, I have used pear libraries and pack headers, and I have also used simple xml replacement by using smarty and so on. I don't need to talk about csv. Haha. (Don't talk about the COM method. This is too readable. I have also written articles such as generating word Using wps)
However, I only used one method when I was reading it. I forgot what it was, and I had to go back to the code. This is because the app is a tailism and cannot be remembered.
Address: http://xinsync.xju.edu.cn/index.php/archives/3858
Original article content:

Recently, due to project requirements, a module needs to be developed to export some data in the system to Excel, and then export the data back to the system after modification. We will take the opportunity to make a summary of this study.
Basically, the exported files are divided into two types:
1: Excel-like format. This is not an Excel file in the traditional sense. It is only because of its strong compatibility and ability to open it correctly. After you modify the file and save it, you are usually prompted to convert it into an Excel file.
Advantage: simple.
Disadvantage: It is difficult to generate a format. If it is used for import, you need to write the corresponding program separately.
2: Excel format, which corresponds to Excel-like files. The files generated by this method are closer to the real Excel format.

If garbled characters occur during Chinese export, you can convert the string to gb2312. For example, convert $ yourStr from UTF-8 to gb2312:
$ YourStr = mb_convert_encoding ("gb2312", "UTF-8", $ yourStr );

The following describes several methods.
1. Export Excel in PHP

1: The first recommended cool PHPExcel, Official Website: http://www.codeplex.com/PHPExcel
Both import and export are supported. The office2007 format can be exported and 2003 compatible.
The downloaded package contains documents and examples, which you can study on your own.
Example:


PHP code
<? Php
/**
* PHPExcel
*
* Copyright (C) 2006-2007 PHPExcel
*
* This library is free software; you can redistribute it and/or
* Modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* Version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* But without any warranty; without even the implied warranty
* MERCHANTABILITY or fitness for a special PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You shoshould have written ed a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @ Category PHPExcel
* @ Package PHPExcel
* @ Copyright Copyright (c) 2006-2007 PHPExcel (http://www.codeplex.com/PHPExcel)
* @ License http://www.gnu.org/licenses/lgpl.txt LGPL
* @ Version 1.5.0, 2007-10-23
*/

/** Error reporting */
Error_reporting (E_ALL );

/** Include path **/
Set_include_path (get_include_path (). PATH_SEPARATOR. '../Classes /');

/** PHPExcel */
Include 'phpexcel. php ';

/** PHPExcel_Writer_Excel2007 */
Include 'phpexcel/Writer/excel2007.php ';

// Create new PHPExcel object
Echo date ('H: I: s'). "Create new PHPExcel object ";
$ ObjPHPExcel = new PHPExcel ();

// Set properties
Echo date ('H: I: s'). "Set properties ";
$ ObjPHPExcel-> getProperties ()-> setCreator ("Maarten Balliauw ");
$ ObjPHPExcel-> getProperties ()-> setLastModifiedBy ("Maarten Balliauw ");
$ ObjPHPExcel-> getProperties ()-> setTitle ("Office 2007 XLSX Test Document ");
$ ObjPHPExcel-> getProperties ()-> setSubject ("Office 2007 XLSX Test Document ");
$ ObjPHPExcel-> getProperties ()-> setDescr encryption ption ("Test document for Office 2007 XLSX, generated using PHP classes .");
$ ObjPHPExcel-> getProperties ()-> setKeywords ("office 2007 openxml php ");
$ ObjPHPExcel-> getProperties ()-> setCategory ("Test result file ");

// Add some data
Echo date ('H: I: s'). "Add some data ";
$ ObjPHPExcel-> setActiveSheetIndex (0 );
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('a1', 'Hello ');
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('b2', 'World! ');
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('c1', 'Hello ');
$ ObjPHPExcel-> getActiveSheet ()-> setCellValue ('d2 ', 'World! ');

// Rename sheet
Echo date ('H: I: s'). "Rename sheet ";
$ ObjPHPExcel-> getActiveSheet ()-> setTitle ('simple ');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$ ObjPHPExcel-> setActiveSheetIndex (0 );

// Save Excel 2007 file
Echo date ('H: I: s'). "Write to Excel2007 format ";
$ ObjWriter = new PHPExcel_Writer_Excel2007 ($ objPHPExcel );
$ ObjWriter-> save (str_replace ('. php', '.xlsx', _ FILE __));

// Echo done
Echo date ('H: I: s'). "Done writing file .";

 

2. Use the Spreadsheet_Excel_Writer class of pear.
: Http://pear.php.net/package/Spreadsheet_Excel_Writer
This class depends on OLE,: http://pear.php.net/package/OLE
Note that the exported Excel file format is relatively old. After modification, the system will prompt whether to convert it to the updated format.
However, you can set the format, which is very powerful.


PHP code
<? Php
Require_once 'spreadsheet/Excel/Writer. php ';

// Creating a workbook
$ Workbook = new Spreadsheet_Excel_Writer ();

// Sending HTTP headers
$ Workbook-> send('test.xls ');

// Creating a worksheet
$ Worksheet = & $ workbook-> addWorksheet ('My first worksheet ');

// The actual data
$ Worksheet-> write (0, 0, 'name ');
$ Worksheet-> write (0, 1, 'age ');
$ Worksheet-> write (1, 0, 'johnsmith ');
$ Worksheet-> write (1, 1, 30 );
$ Worksheet-> write (2, 0, 'johann Schmidt ');
$ Worksheet-> write (2, 1, 31 );
$ Worksheet-> write (3, 0, 'juan Herrera ');
$ Worksheet-> write (3, 1, 32 );

// Let's send the file
$ Workbook-> close ();
?>


3: Use smarty to generate XML or HTML files conforming to Excel specifications
Supported formats, perfect export solution. However, the imported XML file is essentially an XML file. If it is used for import, additional processing is required.
For more information, see rardge's post: http://bbs.chinaunix.net/viewthread.php? Tid = 745757

Note that if the number of rows of the exported table is unknown, you are advised to delete "ss: ExpandedColumnCount =" 5 "ss: ExpandedRowCount =" 21 "" in the template.

4. Use the pack function to print out the broken sentence symbols in the simulated Excel format, which is closer to the standard Excel format. If you use office2003 to modify and save it, no prompt is displayed. This method is recommended.
The disadvantage is that there is no format.


PHP code
<? Php
// Send Header
Header ("Pragma: public ");
Header ("Expires: 0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.