PHP Import Export Excel Method _php Tutorial

Source: Internet
Author: User
Tags echo date

Original Author: A podcast on the iceberg
See this article, is very surprised the original author's patience, although we have some in peacetime, but not the author listed the whole, when writing Excel, I used Pear Library, also used the pack pressure pack head, also those who use smarty and other simple substitution of XML also used, The CSV doesn't have to be talked about. Oh. (COM does not speak, this can be read too much, I also wrote the use of WPS, such as word generation and other articles)
But in the reading, only used one, specifically what forget, to go back to the code. Because of the use of take doctrine, can't remember.
Original address: http://xinsync.xju.edu.cn/index.php/archives/3858
Original content:

Recently, due to project needs, a module needs to be developed to export some of the data in the system to excel, and then back to the system after modification. Take the opportunity to study this, some of the following summary.
Basically the exported files are divided into two types:
1: Excel format, this is not in the traditional sense of Excel file, just because Excel compatibility ability, can be opened correctly. After you modify this file and then save it, you will usually be prompted whether you want to convert to an Excel file.
Pros: Simple.
Disadvantage: It is difficult to generate a format, if you need to write the appropriate program for the import.
The 2:excel format, which corresponds to class Excel, produces files that are closer to the real Excel format.

If garbled in the export of Chinese, you can try to convert the string to gb2312, for example, the following $yourstr from Utf-8 to gb2312:
$YOURSTR = mb_convert_encoding ("Gb2312″," Utf-8″, $YOURSTR);

Several methods are listed below.
First, PHP export Excel

1: First recommendation Matchless Phpexcel, official website: http://www.codeplex.com/PHPExcel
Import and export, you can export office2007 format, and compatible with 2003.
The downloaded package has documentation and examples that you can study on your own.
To copy the paragraph example out:


PHP code
/**
* Phpexcel
*
* Copyright (C) 2006-2007 Phpexcel
*
* This library was 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 was distributed in the hope that it'll be useful,
* but without any WARRANTY; Without even the implied warranty of
* merchantability or FITNESS for A particular PURPOSE. See the GNU
* Lesser general public License for more details.
*
* You should has received a copy of the GNU Lesser general public
* License along with this library; If not, write to the free software
* Foundation, Inc., Wuyi 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" XLSX Test Document ");
$objPHPExcel->getproperties ()->setsubject ("Office" XLSX Test Document ");
$objPHPExcel->getproperties ()->setdescrīption ("Test document for Office-XLSX, generated using PHP classes.");
$objPHPExcel->getproperties ()->setkeywords ("Office 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 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, the use of Pear Spreadsheet_excel_writer class
Download Address: Http://pear.php.net/package/Spreadsheet_Excel_Writer
This class relies on OLE, download address: Http://pear.php.net/package/OLE
It is important to note that the exported Excel file format is older, and the modified save will prompt you to convert to an updated format.
But you can set the format, very powerful.


PHP code
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, ' John Smith ');
$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: Generate an XML or HTML file that conforms to the Excel specification using Smarty
Support format, very perfect export scheme. However, the nature of the derivative is the XML file, if used to import it needs to be processed separately.
For more information, please see Rardge's post: http://bbs.chinaunix.net/viewthread.php?tid=745757

It is important to note that if the number of table rows exported is uncertain, it is best to delete "ss:expandedcolumncount=" 5″ss:expandedrowcount= "21″" in the template.

4, using the Pack function to print the analog Excel format of the segmentation symbol, which is closer to the standard format of Excel, with office2003 modified to save, will not pop up hints, recommend this method.
The disadvantage is no format.


PHP code
Send Header
Header ("Pragma:public");
Header ("expires:0

http://www.bkjia.com/PHPjc/486584.html www.bkjia.com true http://www.bkjia.com/PHPjc/486584.html techarticle Original Author: The podcast on the iceberg saw this article, it is very surprised the original author's patience, although we usually use some, but not the author listed the full, when writing Excel ...

  • 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.