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: