PHP uses Phpexcel to remove Excel cell-specific columns by using the method _php tips

Source: Internet
Author: User
Tags php database php programming php regular expression


This example describes how PHP uses Phpexcel to remove the columns specified by Excel cells. Share to everyone for your reference, specific as follows:



The requirement is this:



There is a system that is used only by the company's internal and external distributors, and in an export function the company's internal employees are not the same as those exported by an external distributor (some data is not available to the distributor)
Because the exported data is the same (some of the data outside of the column is not), it is not handled separately, but it is consolidated and then deleted without permission according to the different accounts


/ **
* @Author: HTL
* @Description: move out of cell column
* @objPHPExcel: phpexecel object
* @remove_columns: the column to be moved out
* /
function _remove_column ($ objPHPExcel, $ remove_columns) {
    if (! $ objPHPExcel
      ||! is_object ($ objPHPExcel)
      ||! $ remove_columns
      ||! is_array ($ remove_columns)
      || count ($ remove_columns) <= 0) return;
    // The cell template value, used to match the column to be deleted (in the first column of the excel template)
    $ cell_val = '';
    // Total number of cells
    $ highestColumm = $ objPHPExcel-> getActiveSheet ()-> getHighestColumn ();
    for ($ column = 'A'; $ column <= $ highestColumm;) {
      // The number of columns starts with column A
      $ cell_val = $ objPHPExcel-> getActiveSheet ()-> getCell ($ column. "1");
      $ cell_val = preg_replace ("/ [\ s {}] / i", "", $ cell_val);
      // Remove out the column without permission to export
      // The column cannot be increased by 1 after moving out, because the current column has been moved out and adding 1 will cause the wrong column to be deleted
      // This question wasted dozens of minutes
      if (strlen ($ cell_val)> 0 && in_array ($ cell_val, $ remove_columns))
      {
        $ objPHPExcel-> getActiveSheet ()-> removeColumn ($ column);
      }
      else
      {
        $ column ++;
      }
    }
}
// example
// Import PHPExcel class
vendor ("PHPExcel.PHPExcel");
$ filepath = './data/upload/temp.xlsx';
$ objReader = \ PHPExcel_IOFactory :: createReader ('Excel2007');
$ objPHPExcel = $ objReader-> load ($ filepath);
$ this-> _ remove_column ($ objPHPExcel, array ("age", "address"));







More interested in PHP related content readers can view the site topics: "PHP operation Office Document Skills Summary (including word,excel,access,ppt)", "PHP Array" operation Skills Encyclopedia, "PHP Sorting algorithm Summary", " PHP commonly used traversal algorithm and skills summary, "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", "PHP Mathematical Operation Skills Summary", "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage Summary" A summary of common PHP database operation techniques



I hope this article will help you with the PHP program design.


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.