导出Excel电子表格
为了能够将您的数据导出到excel电子表格,需要PHPExcel外部库来将您的php转为excel。 您可以在此处获取PHPExcel库的源代码。 您需要下载代码并解压缩zip文件,然后将Classes内的(PHPExcel.php文件和PHPExcel文件夹)放到主题文件根目录中。
添加以下代码到主题函数
add_action('admin_init', 'export_xlsx'); //you can use admin_init as well
function export_xlsx() {
if (!empty($_POST['mytheme_export_xlsx'])) {
if (current_user_can('manage_options')) {
/** PHPExcel */
include 'PHPExcel.php';
/** PHPExcel_Writer_Excel2007 */
include 'PHPExcel/Writer/Excel2007.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set properties
$objPHPExcel->getProperties()->setTitle( esc_html__('Test xlsx document', 'mytheme') );
$objPHPExcel->getProperties()->setSubject( esc_html__('Test xlsx document', 'mytheme') );
$objPHPExcel->getProperties()->setDescription( esc_html__('Test export users document for XLSX, generated using PHP classes.', 'mytheme') );
// WP_User_Query arguments
$args = array (
'order' => 'ASC',
'orderby' => 'display_name',
'fields' => 'all',
);
// The User Query
$blogusers = get_users( $args );
$cell_counter = 1;
//Set up the labels of the columns
$objPHPExcel->getActiveSheet()->SetCellValue('A1', esc_html__('First Name', 'mytheme'));
$objPHPExcel->getActiveSheet()->SetCellValue('B1', esc_html__('Last Name', 'mytheme'));
$objPHPExcel->getActiveSheet()->SetCellValue('C1', esc_html__('Email', 'mytheme'));
$objPHPExcel->getActiveSheet()->SetCellValue('D1', esc_html__('User Role', 'mytheme'));
foreach ( $blogusers as $user ) {
$cell_counter++;
$meta = get_user_meta($user->ID);
$role = $user->roles;
$email = $user->user_email;
$first_name = ( isset($meta['first_name'][0]) && $meta['first_name'][0] != '' ) ? $meta['first_name'][0] : '' ;
$last_name = ( isset($meta['last_name'][0]) && $meta['last_name'][0] != '' ) ? $meta['last_name'][0] : '' ;
// Add data
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$cell_counter.'', $first_name);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$cell_counter.'', $last_name);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$cell_counter.'', $email);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$cell_counter.'', ucfirst($role[0]));
}
// Set column data auto width
for($col = 'A'; $col !== 'E'; $col++) {
$objPHPExcel->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);
}
// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle(esc_html__('Users', 'mytheme'));
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="users.xlsx"');
header('Cache-Control: max-age=0');
// Save Excel file
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit();
}
}
}
现在,当您单击“Export All as XLSX”按钮时,您将下载一个users.xlsx文件,其中包含各自单元格中的所有用户,这些文件都很简洁,易于编辑。 比逗号分隔值好多了。
如果您有CRM系统,这将特别有用(您也可以使用WordPress实现),当你需要处理大量信息时。 在excel中操作它要容易得多。
关注微信公众号themebest
- 第一时间获取主题更新动态,优惠信息
- WordPress动态、教程分享