PHPExcel import and export common method summary

Recently the company back office management system requirements, use PHP export excel spreadsheet data, so special compiled a commonly used method for your reference:

PHPExcel expansion pack Address: https://github.com/wanqianworld/phpexcel-1.8

Roughly divided into several parts:

First, the method of deriving and steps

1. introduced PHPExcel file: require_once ( "../Classes/PHPExcel.php" );
 2. Examples of PHPExcel: $ ObjPHPExcel = new new PHPExcel ();
 3. Get Current Work Sheet: $ ObjPHPExcel -> getACtiveSheet ();
 . 4 . provided to the current worksheet name: $ ObjPHPExcel -> setTitle ( 'sheet table name' );
 5. Add data assignment: $ ObjPHPExcel -> setCellValue ( 'cell coordinates', 'value' );
 6. The specified format generating excel file: $ ObjPHPExcel -> PHPExcel_IOFactory :: createWriter ( $ ObjPHPExcel , 'Excal5' );
 7. The save Excel files: $ ObjPHPExcel -> save ( "path and file name to save ' );
 8. header If you want to generate an Excel file output to the browser:
        Excel5 need to add the following code:
          ( "the Content-type: file application / vnd.ms-Excel"); // output Excel03 file 
         header ( " the Content-Disposition: Attachment; filename = Test.xls'); 
         header ( " the Content-Control : max -AGE = 0 " );
         $ ObjPHPExcel -> Save (" PHP: // Output "); 
        Excel7 need to add the following code:
          header (" the Content-type: file application / vnd.openxmlformats-officedocument.spreadsheetml.sheet ") ; // output Excel07 file 
         header ( " Content-Disposition: Attachment; filename = test.xlsx '); // name of the output file browser 
         header ( " Content-Control:max-age = 0"); // disable browser cache
        // Disable the browser cache $ ObjPHPExcel -> the Save ( "PHP: // the Output");

Two, Excel style control

1. merge cells: $ ObjPHPExcel -> getActiveSheet () -> MergeCells ( 'A18: E22' );
 2. Split Cell: $ ObjPHPExcel -> getActiveSheet () -> unmergeCells ( 'A18: E22' );
 . 3 set default cell horizontally centered: $ ObjPHPExcel -> getDefaultStyle () -> getAlignment () -> setHorizontal (PHPExcel_Style_Alignment :: HORIZONTAL_CENTER);
 4. default centrally disposed vertical frame unit: $ ObjPHPExcel -> getDefaultStyle () -> getAlignment () -> setvertical (PHPExcel_Style_Alignment :: VERTICAL_CENTER);
 5. set the default font style: font type: $ ObjPHPExcel -> getDefaultStyle () -> getFont () -> setName ( 'Microsoft elegant black' ); 
        font size: $ ObjPHPExcel -> getDefaultStyle () -> getFont () ->the setSize (14 );
 6. The set font styles specified:$ ObjPHPExcel -> getStyle ( 'A18: E22') -> getFont () -> setName ( 'Microsoft elegant black' ); 
        font size: $ ObjPHPExcel -> getStyle ( 'A18: E22') -> getFont () -> the setSize (14 );
 7. The background color setting: filling mode (completely filled): $ ObjPHPExcel -> the getStyle ( 'B2') -> getFill () -> setFillType (PHPExcel_Style_Fill :: FILL_SOLID); 
        fill color: $ objPHPExcel -> the getStyle ( 'B2') -> getFill () -> getStartColor () -> setARGB ( 'FFFF0000' );
 8. The setting frame:
     $ styleArray = Array (
         'borders' => Array (
             'Outline' =>array(
                'style' => PHPExcel_Style_Border::BORDER_THICK,
                'color' => array ( 'ARGB' => 'FFFF0000'), 
            ) , 
        ) , 
    ); 
    $ objWorksheet -> the getStyle ( 'B2: for G8') -> applyFromArray ( $ styleArray ); 
    call getStyle () method, the parameters It can be passed through an array of ways. 
9. Wrap: 
  Open Wrap: $ objPHPExcel -> getActiveSheet () -> the getStyle ( 'A1') -> getAlignment () -> setWrapText ( to true ); add \ n values need to change the row, and then to wrap : $ objPHPExcel -> getActiveSheet () -> getCell ( 'A1') -> the setValue ( "Hello \ nworld" ); 10. The format cells:
  setting cell format string (ID viable solution for the phone number format scientific notation): $ objPHPExcel -> getActiveSheet () -> getCell ( 'A1') -> setValueExplicit ('25 ',

 

Third, add a picture, mark

1. Add image:
         $ objSheet = $ ObjPHPExcel -> getActiveSheet (); // // Get the current sheet table 
        $ ObjDrawing = new new PHPExcel_WorkSheet_Drawing (); // get a picture of the operation target 
        $ ObjDrawing -> setPath ( "Image Path" ); // load picture paths 
        $ ObjDrawing -> setCoordinates ( 'the F5'); // set the picture into left corner 
        $ ObjDrawing -> setWidth (500); // set the image width (geometric scaling) 
        $ ObjDrawing -> setHeight (100); // set the height of the image (geometric scaling) and set if the width and height, two geometric scaling images will note here 
        $ ObjDrawing -> setOffsetX (20 is); // the offset is set cells not exceed the width of the cell, otherwise invalid 
        $ ObjDrawing-> setOffsetY (20 is); // Set the offset within the cell, the cell can not exceed the height, otherwise invalid 
        $ ObjDrawing -> setWorkSheet ( $ objSheet ); // the picture into the sheet 
2. add rich text blocks ( That text is provided within a cell of a different style)
         $ ObjRichtext = new new PHPExcel_RichText (); // get a block of text operation target 
        $ ObjRichtext -> CreateText ( 'text'); // add normal text 
        $ ObjStyleFont = $ ObjRichtext - > createTextRun ( "need to modify the text format"); // add the need to modify the style of the text 
        $ ObjStyleFont -> getFont () -> the setSize (16) -> setBold ( True ); // set the text style 
        $ ObjRichtext -> CreateText ( 'text');//Add normal text 
        $ objSheet -> getCell ( 'A4') -> the setValue ( $ ObjRichtext ); // text block into the sheet 
3. Annotations :( Note: Cells adding annotations Excel2007 is supported above)
         $ objSheet -> getComment ( "the F4") -> the gettext () -> createTextRun ( 'annotation content' );
 4. hyperlink:
         $ objSheet -> setCellValue ( 'Bl', 'Baidu' );
         $ objSheet -> getCell ( 'B1') -> getHyperlink ( ) -> setUrl ( "https://www.baidu.com");

 

Four, to generate a graphical report (line, pie, cylindrical, region, etc.)
    This is more content, daily use may be less, I do not give you summary, but you can see examples of PHP files in the directory Examples PHPExcel extension package, there are a lot of demo, the comment is also very clear.
 
Five, Excel Import File (fully loaded)
1. The introduction of PHPExcel export file: require_once ( "../Classes/PHPExcel/IOFactory.php" );
 2. the need to load Excel files in Excel: $ ObjPHPExcel -> :: PHPExcel_IOFactory the Load ( $ filename );
 3. obtain excel file in the number of sheet: $ SheetCount = $ ObjPHPExcel -> getSheetCount ();
 4. traversing the sheet, the data sheet of the composition of all the array:
     for ( $ I = 0; $ I < $ SheetCount ; $ I ++ ) {
         dataArray is $ = $ ObjPHPExcel -> getSheet ( $ I ) -> toArray (); 
    }
 . 5.The method of obtaining only the upper Excel all data, such as the need to filter through the data, as follows:
     -> the getValue ();the foreach ( $ ObjPHPExcel -> getWorksheetIterator   AS   $ Sheet ) {
             the foreach ( $ ObjPHPExcel -> getRowIterator   AS   $ Row ) {
                     IF ( $ Row -> getRowIndex () <. 3 ) {
                             Continue ; 
                    } // get the line number, only the third obtaining data after line 
                    the foreach ( $ ObjPHPExcel -> getCellIterator   AS  $ cell ) {
                             $ data = $ cell // obtaining cell data 
                    } 
            } 
    }

 

Six, Excel Import File (partial loading)

1. Obtain excel file types (XLS \ XLSX ...): $ FILE_TYPE = :: PHPExcel_IOFactory the Identify ( $ filename );
 2. acquiring operation target document reading: $ objReader = PHPExcel_IOFactory :: CreateReader ( $ FILE_TYPE );
 . 3. Specifies the name of the loaded sheet: $ sheetName = arrat ( 'Sheet1', 'Sheet2' );
 4. the load data sheet name: $ objReader -> setLoadSheetsOnly ( $ sheetName );

 

It must be noted that PHPExcel take up a lot of memory, without special settings, the cell will read information phpExcel stored in memory, a cell occupied by about 1KB.

To address the issue of memory footprint:

1. You can use :: PHPExcel_Settings setCacheStorageMethod () to set different cache way, has the purpose to reduce memory consumption! Note that: You must add before the new PHPExcel object.
2. The sequence of the cell data stored in the memory      
  PHPExcel_CachedObjectStorageFactory :: cache_in_memory_serialized; 3. the sequence of cells for Gzip then compressed and then stored in the memory
  PHPExcel_CachedObjectStorageFactory :: cache_in_memory_gzip; 4. cached in a temporary disk file , the speed may be slower
  PHPExcel_CachedObjectStorageFactory :: cache_to_discISAM; 5. save in PHP: // the TEMP   PHPExcel_CachedObjectStorageFactory :: cache_to_phpTemp; 6. save in memcache in
  PHPExcel_CachedObjectStorageFactory :: cache_to_memcache.

Here are two examples of small memory footprint to deal with the problem:

EG1:
         $ cacheMethod = PHPExcel_CachedObjectStorageFactory :: cache_to_phpTemp; // selected cache ways 
        $ CacheSettings = Array ( 'memoryCacheSize' => '16MB'); // buffer provided 
        PHPExcel_Settings :: setCacheStorageMethod ( $ cacheMethod , $ CacheSettings ); // set the cache 
        objPHPExcel $ = new new PHPExcel (); 
EG2: 
    $ cacheMethod = PHPExcel_CachedObjectStorageFactory :: cache_to_memcache; // selected cache ways 
    $ CacheSettings = Array (
             'memcacheServer' => 'localhost', 
            'memcachePort' => 11211, 
            'CacheTime' => 600 
    ); // memcache cache settings 
    PHPExcel_Settings :: setCacheStorageMethod ( $ cacheMethod , $ CacheSettings ); // set the cache

 

These can basically meet the daily needs of the development, if you want more complex requirements, we can see examples of PHP files in the directory Examples PHPExcel extension package, there are a lot of demo, the comment is also very clear. Or read the official document PHPExcel.

 

 
 

Guess you like

Origin www.cnblogs.com/yuanwanli/p/12617066.html