Phpexcel processing Import export picture, link

Source: Internet
Author: User

Phpexcel is a very powerful PHP tabular processing module that provides a set of PHP programming languages that allow you to write and read different spreadsheet file formats, such as Excel (paper). Xls,excel (Officeopenxml). xlsx, CSV, free/OpenOffice Calc. ODS, Gnumeric, PDF, HTML 、... This project is built on Microsoft's OPENXML standard

: http://phpexcel.codeplex.com/after decompression in the examples directory has a lot of examples, documentation directory is related to the document (if you know English in the API directory there is a finer document page)


In most cases we only use the character import and export function of this plugin. Many times we want to import export links, pictures and so on.

Phpexcel picture and text is separate, that is, in the operation of the time picture and text are added separately to the table, the same resolution should be divided into two times the picture and text to read and merge, the picture is also coordinates, the coordinates of the picture is the upper left corner of the subject.


Import parsing:

Phpexcel Import parsing is very powerful, the supported table files can be viewed in the phpexcel/reader/directory, due to the number of supported table files, so it is convenient to import:

<?php$excel=phpexcel_iofactory::load ("file.xlsx");//import file directory, the system will automatically find the corresponding parsing class $sheet= $excel->getsheet (0); /Select a few tables, such as the following image, the default is three tables $data= $sheet->toarray ();//convert the table data to an array, note: Here the conversion is an array of row numbers for the outer subscript, the column number will be converted to a number in the array subscript, The coordinates corresponding to the value will only take the string left here, the picture or link will not appear here. /* Take picture */$imgData =array (); $imageFilePath = '/images/'. Date (' y/m/d '). ' /';//Picture Save directory foreach ($sheet->getdrawingcollection ()  as  $img) {    list  ($ startcolumn,  $startRow)  = phpexcel_cell::coordinatefromstring ($img->getcoordinates ());// Get column and line number      $imageFileName = $img->getcoordinates (). Mt_rand (100,999);    After parsing the  /* table, the picture is saved as a resource in the object, and the image resource can be obtained directly from the Getimageresource function and then written to the local file */    switch  ($ Img->getmimetype ()) {//Process picture format         case  ' image/jpg ':         case  ' Image/jpeg ':              $imageFileName. = '. jpg '; &nbsP;           imagejpeg ($img->getimageresource (), $ Imagefilepath. $imageFileName);            break;         case  ' Image/gif ':              $imageFileName. = '. gif ';             imagegif ($img->getimageresource (), $imageFilePath. $imageFileName);             break;         case  ' image/png ':             $imageFileName. = '. png ';             imagepng ($img Getimageresource (), $imageFilePath. $imageFileName);             break;    }      $imgData [$startRow] [$startColumn]= $imageFileName;//Append to the array}?>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/57/34/wKioL1SU8grwBayBAAA5o7vzA_g402.jpg "title=" Qq20141220114732.jpg "alt=" Wkiol1su8grwbaybaaa5o7vza_g402.jpg "/>

The above is the basic import parsing function, actually in the picture processing is much simpler, because there is not too many attached attributes, that in the text export is not so convenient, mainly the table in the creation of a lot of formatting, such as the date, in the table written date system will default automatically modify the current grid format as the date, When parsing a table, it is important to note that this data is not text, and if parsing by text is parsed into 5-bit numbers instead of dates, it is necessary to set the parsing format to restore the desired data.


Export Build:

Phpexcel Export generation and import parsing is reversible, it can be said that the import has what export

<?php$excel=new phpexcel ();//Create Phpexcel object/* Write document Ownership properties */$excel->getproperties ()      ->setcreator ("Maarten balliauw")//Set Creator     ->setlastmodifiedby ("Maarten  balliauw ")//Set Modify person     ->settitle (" office 2007 xlsx test  Document ")//Set title     ->setsubject (" Office 2007 xlsx test document ")/ /Set Topic     ->setdescription ("Test document for office 2007 xlsx,  generated using php classes. ") Set Description     ->setkeywords ("office 2007 openxml php")//settings keyword      ->setcategory ("Test result file");//set type          /* Set default font and size */$excel->getdefaultstyle ()     ->getfont ()//Get Font object      ->setname (' Arial ')//Set Font     ->setsize (10);//Set Font size &NBSp;       /* set the current sheet*/$excel->setactivesheetindex (0);/* Set sheet name*/$ Excel->getactivesheet ()->settitle (' Table 1 ');/* Write regular text */$excel->getactivesheet ()     - >setcellvalue (' A1 ',  ' String ')     ->setcellvalue (' B1 ',  ' simple ')      ->setcellvalue (' C1 ',  ' phpexcel ');         /* Write custom style text */$objRichText  = new phpexcel_richtext (); $objRichText->createtext (' Hello   '); $ objpayable =  $objRichText->createtextrun (' Are you   good  ? '); $objPayable->getfont ()     ->setbold (true); $objPayable->getfont ()      ->setitalic (true); $objPayable->getfont ()     ->setcolor ( new  phpexcel_style_color ( PHPExcel_Style_Color::COLOR_DARKGREEN )  ); $objRichText CreateText (',  unless specified otherwise on the&nbsP;invoice. '); $excel->getactivesheet ()     ->setcellvalue (' A13 ',  ' rich text ')      ->setcellvalue (' C13 ',  $objRichText);         /* Write a linked text */$excel->getcell (' A2 ')     ->setvalue (' PHP in the Web ')     - >gethyperlink ()     ->seturl (' http://php2012web.blog.51cto.com/');/* Write a picture */// Add a picture using another table picture object/*     $img =new phpexcel_worksheet_memorydrawing ();      $img->setimageresource ($images->getimageresource ());     $img->setmimetype ($ Images->getmimetype ());     $img->setname ($images->getname ());      $img->setdescription ($images->getdescription ()) *///use local picture $img=new phpexcel_worksheet_drawing (); $img->setpath ('/img/text.jpg ');//write Picture path $img->setheight (100);//write Image Height $img->setwidth (100);//write picture width $ Img->setoffsetX (1);//write the x-coordinate value of the picture in the specified lattice $img->setoffsety (1);//write the y-coordinate value of the picture in the specified lattice $img->setrotation (1);//Set the rotation angle $img->getshadow ()->setvisible (TRUE);//$img->getshadow ()->setdirection ()//$img->setcoordinates (' B2 ');// Set the table position of the picture $img->setworksheet ($excel->getactivesheet ());//write the picture into the current table/* Generate file */$objWriter  =  Phpexcel_iofactory::createwriter ($excel,  ' Excel2007 ');//Create Write File Generator $objwriter->save (' excel.xlsx ');//Generate file? >


This article is from the "gangbusters" blog, make sure to keep this source http://php2012web.blog.51cto.com/5585213/1620057

Phpexcel processing Import export picture, link

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.