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.