PHP to work with Excel data table classes

Source: Internet
Author: User
Tags getcolor

First, PHP import Excel

Using Php-excelreader, download address: Http://sourceforge.net/projects/phpexcelreader

Example:

<?php
Require_once ' excel/reader.php ';

Excelfile ($filename, $encoding);
$data = new Spreadsheet_excel_reader ();

Set output Encoding.
$data->setoutputencoding (' UTF8 ');

$data->read (' Wjztest.xls ');


Error_reporting (e_all ^ e_notice);

for ($i = 1; $i <= $data->sheets[0][' numrows '; $i + +) {
for ($j = 1; $j <= $data->sheets[0][' numcols '; $j + +) {
echo "". $data->sheets[0][' cells ' [$i] [$j]. "", ";
}
echo "\ n";
}

?>


* * Two PHP import excel**

Recommended Phpexcel, official website: http://www.codeplex.com/PHPExcel
Import export, you can export office2007 format, compatible with 2003.
Download the package contains documents and examples, you can study.

Copy the example out of paragraph:

Http://www.codeplex.com/PHPExcel
<?php
Ini_set ("Display_errors", 1);//whether to display an error message
Ini_set (' include_path ', Ini_get (' include_path '). ';D: \ \php\\phpexcel 1.6.6\\tests\\classes\\ ');/set this page contains a path

Include "classes/phpexcel.php";
Include "classes/phpexcel/writer/excel5.php";

Create an Excel
$objPHPExcel = new Phpexcel ();
$objWriter = new Phpexcel_writer_excel5 ($objPHPExcel);

$objWriter->save ("Xxx.xls");
?>
?
Set include path for the Phpexcel class library
Set_include_path ('. ') Path_separator. ' D:\workspace\biznaligy_eh\dev_src\includes\PHPExcel '. Path_separator. Get_include_path ());


Require_once ' phpexcel.php ';
Require_once ' phpexcel/writer/excel5.php '; For other low version xls

Require_once ' phpexcel/writer/excel2007.php '; For excel-2007 format

Create a Process object instance
$objExcel = new Phpexcel ();

Create a file format to write to an object instance, uncomment
$objWriter = new Phpexcel_writer_excel5 ($objExcel); For other version formats
Or
$objWriter = new phpexcel_writer_excel2007 ($objExcel); Used in 2007 format
$objWriter->setoffice2003compatibility (TRUE);


Set document basic Properties
$objProps = $objExcel->getproperties ();
$objProps->setcreator ("Zeal Li");
$objProps->setlastmodifiedby ("Zeal Li");
$objProps->settitle ("Office XLS Test Document");
$objProps->setsubject ("Office XLS Test Document, Demo");
$objProps->setdescription ("Test document, generated by Phpexcel.");
$objProps->setkeywords ("Office Excel Phpexcel");
$objProps->setcategory ("Test");
Sets the current sheet index for subsequent content operations.
It is generally only necessary to display calls when multiple sheet are used.
By default, Phpexcel will automatically create the first sheet set sheetindex=0
$objExcel->setactivesheetindex (0);

$objActSheet = $objExcel->getactivesheet ();

Sets the name of the current active sheet
$objActSheet->settitle (' Test sheet ');


Set cell contents

Automatically determine cell content types based on incoming content by phpexcel
$objActSheet->setcellvalue (' A1 ', ' string content '); String contents
$objActSheet->setcellvalue (' A2 ', 26); Numerical
$objActSheet->setcellvalue (' A3 ', true); Boolean value
$objActSheet->setcellvalue (' A4 ', ' =sum (A2:A2) '); Formula

Explicitly specifying a content type
$objActSheet->setcellvalueexplicit (' A5 ', ' 847475847857487584 ', phpexcel_cell_datatype::type_string);

Merging cells
$objActSheet->mergecells (' b1:c22 ');

Detach cells
$objActSheet->unmergecells (' b1:c22 ');

Set cell styles

Set width
$objActSheet->getcolumndimension (' B ')->setautosize (true);
$objActSheet->getcolumndimension (' A ')->setwidth (30);

$objStyleA 5 = $objActSheet->getstyle (' A5 ');

Sets the number format for the contents of a cell.

If you use Phpexcel_writer_excel5 to generate content,
Notice here that the const variable in the Phpexcel_style_numberformat class defines the
In various custom formatting ways, other types can be used normally, but when the Setformatcode
For Format_number, the actual effect was not set to "0". Need
Modify the GETXF ($style) method in the Phpexcel_writer_excel5_format class source code,
In the IF ($this->_biff_version = = 0x0500) {(Near line No. 363), add a
Line code:
if ($ifmt = = ' 0 ') $ifmt = 1;

Set format to Phpexcel_style_numberformat::format_number, avoid certain large numbers
is shown using scientific notation, and the following Setautosize method allows the contents of each line
Are all displayed as the original content.
$objStyleA 5->getnumberformat ()->setformatcode (Phpexcel_style_numberformat::format_number);

Set font
$objFontA 5 = $objStyleA 5->getfont ();
$objFontA 5->setname (' Courier New ');
$objFontA 5->setsize (10);
$objFontA 5->setbold (TRUE);
$objFontA 5->setunderline (Phpexcel_style_font::underline_single);
$objFontA 5->getcolor ()->setargb (' FF999999 ');

To set alignment
$objAlignA 5 = $objStyleA 5->getalignment ();
$objAlignA 5->sethorizontal (phpexcel_style_alignment::horizontal_right);
$objAlignA 5->setvertical (Phpexcel_style_alignment::vertical_center);

Set border
$objBorderA 5 = $objStyleA 5->getborders ();
$objBorderA 5->gettop ()->setborderstyle (Phpexcel_style_border::border_thin);
$objBorderA 5->gettop ()->getcolor ()->setargb (' FFFF0000 '); Color
$objBorderA 5->getbottom ()->setborderstyle (Phpexcel_style_border::border_thin);
$objBorderA 5->getleft ()->setborderstyle (Phpexcel_style_border::border_thin);
$objBorderA 5->getright ()->setborderstyle (Phpexcel_style_border::border_thin);

Set Fill Color
$objFillA 5 = $objStyleA 5->getfill ();
$objFillA 5->setfilltype (phpexcel_style_fill::fill_solid);
$objFillA 5->getstartcolor ()->setargb (' ffeeeeee ');

Copies the style information from the specified cell.
$objActSheet->duplicatestyle ($objStyleA 5, ' b1:c22 ');

Add a picture
$objDrawing = new phpexcel_worksheet_drawing ();
$objDrawing->setname (' zealimg ');
$objDrawing->setdescription (' Image inserted by Zeal ');
$objDrawing->setpath ('./zeali.net.logo.gif ');
$objDrawing->setheight (36);
$objDrawing->setcoordinates (' C23 ');
$objDrawing->setoffsetx (10);
$objDrawing->setrotation (15);
$objDrawing->getshadow ()->setvisible (true);
$objDrawing->getshadow ()->setdirection (36);
$objDrawing->setworksheet ($objActSheet);

Add a new worksheet
$objExcel->createsheet ();
$objExcel->getsheet (1)->settitle (' Test 2 ');

Protect cells
$objExcel->getsheet (1)->getprotection ()->setsheet (true);
$objExcel->getsheet (1)->protectcells (' A1:c22 ', ' phpexcel ');

Output content

$outputFileName = "Output.xls";
to a file
$objWriter->save ($outputFileName);
Or
to the browser
Header ("Content-type:application/force-download");
Header ("Content-type:application/octet-stream");
Header ("Content-type:application/download");
Header (' Content-disposition:inline;filename= '. $outputFileName. ' ");
Header ("Content-transfer-encoding:binary");
Header ("last-modified:"). Gmdate ("D, D M Y h:i:s"). "GMT");
Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0");
Header ("Pragma:no-cache");
$objWriter->save (' php://output ');
?>

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.