PHP uses PHPExcel to delete the specified column in an Excel cell. phpexcelexcel

Source: Internet
Author: User

PHP uses PHPExcel to delete the specified column in an Excel cell. phpexcelexcel

This example describes how to delete a column in an Excel cell using PHPExcel. We will share this with you for your reference. The details are as follows:

The requirement is as follows:

One system is only used by internal and external dealers. In one export function, the columns exported by internal employees of the Company are different from those exported by external dealers (some data cannot be provided to dealers)
Because the exported data is the same (some external data does not exist), the exported data is not processed separately. Instead, the exported data is generated in a unified manner and then the columns without permissions are deleted based on different accounts.

/*** @ Author: HTL * @ Description: Remove a cell column * @ objPHPExcel: phpexecel object * @ remove_columns: column to be removed */function _ remove_column ($ objPHPExcel, $ remove_columns) {if (! $ ObjPHPExcel |! Is_object ($ objPHPExcel) |! $ Remove_columns |! Is_array ($ remove_columns) | count ($ remove_columns) <= 0) return; // cell template value, used to match the column to be deleted (in the first column of the excel template) $ cell_val = ''; // total number of cell columns $ 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 a column that has no permission to export. // after removal, column cannot be added with 1, because the current column has been removed from the plus 1 column, it will lead to the deletion of the wrong column // This problem waste tens of minutes if (strlen ($ cell_val)> 0 & in_array ($ cell_val, $ remove_columns) {$ objPHPExcel-> getActiveSheet ()-> removeColumn ($ column) ;}else {$ column ++ ;}}} // example // import the 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 "));

Shows the running effect:

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.