Thêm một hàm xuất dữ liệu ra file Excel trong module nukeviet.

Thứ năm - 27/04/2023 21:35 839 0

Xuất dữ liệu ra Excel

Xuất dữ liệu ra Excel
Xuất dữ liệu từ MySQL Database luôn là một nhu cầu rất cần thiết đối với nhiều website, vì vậy tôi xin hướng dẫn các bạn cách để thêm một chức năng xuất dữ liệu ra file Excel trong module Nukeviet. Trong bài viết này chúng ta sẽ thực hiện xuất dữ liệu từ module users ra Excel. Còn đối với các module khác các bạn thực hiện tương tự.

Phần 1.


Phần 2:

Bước 1. Tạo file xuatex.tpl lưu vào thư mục : themes\admin_default\modules\users\

Bạn vào thư mục theme mặc định của admin là themes\admin_default\modules\users\ tạo một file có tên là xuatex.tpl có nội dung sau:
<!-- BEGIN: main -->
<form action="/admin/index.php?{NV_LANG_VARIABLE}={NV_LANG_DATA}&amp;{NV_NAME_VARIABLE}={MODULE_NAME}&amp;{NV_OP_VARIABLE}={OP}" method="post">
    <div class="text-left"><input class="btn btn-primary" name="submit" type="submit" value="{LANG.xuat}" /></div>
</form>
<!-- END: main -->
File này chỉ hiển thị nút bấm vào để xuất ra excel.

 

Bước 2. Tạo file xuatex.php lưu vào thư mục: modules\users\admin\

Tiếp theo các bạn vào thư mục modules\users\admin\ tạo file xuatex.php có nội dung như sau:
<?php

/**
 * @Project NUKEVIET 4.x
 * @Author VINADES.,JSC <contact@vinades.vn>
 * @Copyright (C) 2020 VINADES.,JSC. All rights reserved
 * @License: Not free read more http://nukeviet.vn/vi/store/modules/nvtools/
 * @Createdate Sat, 31 Oct 2020 02:20:33 GMT
 */

if (!defined('NV_IS_FILE_ADMIN')) {
    die('Stop!!!');
}

$page_title = $lang_module['xuatex'];

//------------------------------
// Viết code xử lý chung vào đây
//------------------------------

$xtpl = new XTemplate('xuatex.tpl', NV_ROOTDIR . '/themes/' . $global_config['module_theme'] . '/modules/' . $module_file);
$xtpl->assign('LANG', $lang_module);
$xtpl->assign('NV_LANG_VARIABLE', NV_LANG_VARIABLE);
$xtpl->assign('NV_LANG_DATA', NV_LANG_DATA);
$xtpl->assign('NV_BASE_ADMINURL', NV_BASE_ADMINURL);
$xtpl->assign('NV_NAME_VARIABLE', NV_NAME_VARIABLE);
$xtpl->assign('NV_OP_VARIABLE', NV_OP_VARIABLE);
$xtpl->assign('MODULE_NAME', $module_name);
$xtpl->assign('OP', $op);

//-------------------------------
// Viết code xuất ra site vào đây
//-------------------------------
$xtpl->parse('main');
$contents = $xtpl->text('main');

include NV_ROOTDIR . '/includes/header.php';
echo nv_admin_theme($contents);
include NV_ROOTDIR . '/includes/footer.php';
Nội dung file này chưa xử lý gì, mà đây chỉ là cấu hình cơ bản của một chức năng trong admin.

 

Bước 3. Thêm ngôn ngữ trong admin. 

Việc tiếp theo là thêm ngôn ngữ vào file ngôn ngữ trong thư mục mặc định theme admin: Vào thư mục modules\users\language\ tìm file admin_vi.php mở ra và thêm vào cuối cùng đoạn mã sau:
$lang_module['xuatex'] = 'Xuất dữ liệu ra Excel';
$lang_module['xuat'] = 'Bấm để xuất dữ liệu ra Excel';


 

Bước 4. Thêm chức năng vào menu trong quản lý admin của module. 

Ở bước này các bạn tìm file: admin.menu.php trong thư mục modules/users/admin.menu.php mở và tìm:
$allow_func[] = 'config';
Thêm vào ngay dưới dòng lệnh sau:
$submenu['xuatex'] = $lang_module['xuatex'];
$allow_func[] = 'xuatex';


Đến đây cơ bản đã thêm được một hàm  trong admin. Vào lại phần quản lý module users trong admin để xem kết quả:

Screenshot 2023 04 30 090309

 

Bước 5. Viết các lệnh xử lý để xuất dữ liệu ra Excel.

<?php

if (!defined('NV_IS_FILE_ADMIN')) {
    die('Stop!!!');
}

$page_title = $lang_module['xuatex'];

if (!class_exists('PHPExcel')) {
    if (file_exists(NV_ROOTDIR . '/includes/class/PHPExcel.php')) {
        require_once NV_ROOTDIR . '/includes/class/PHPExcel.php';
    } 
}
if (!class_exists('PHPExcel')) {
    $contents = nv_theme_alert('LỖI RỒI', 'Lỗi thiếu class PHPExcel, Bạn hãy tải PHPExcel vào trong thư mục includes', 'danger');
    include NV_ROOTDIR . '/includes/header.php';
    echo nv_admin_theme($contents);
    include NV_ROOTDIR . '/includes/footer.php';
}
//------------------------------
// Viết code xử lý chung vào đây
//------------------------------
$xtpl = new XTemplate('xuatex.tpl', NV_ROOTDIR . '/themes/' . $global_config['module_theme'] . '/modules/' . $module_file);
$xtpl->assign('LANG', $lang_module);
$xtpl->assign('GLANG', $lang_global);
$xtpl->assign('ACTION_URL', NV_BASE_ADMINURL . 'index.php?' . NV_LANG_VARIABLE . '=' . NV_LANG_DATA . '&' . NV_NAME_VARIABLE . '=' . $module_name . '&' . NV_OP_VARIABLE);
$xtpl->assign('OP', $op);

//-------------------------------
// Viết code xuất ra site vào đây
//-------------------------------
$post = $nv_Request->get_title('submit', 'post', '');
if (!empty($post)){
	$objExcel = new PHPExcel;
	$objExcel->setActiveSheetIndex(0);
	// Đặt tên Sheet là Xuất dữ liệu.
	$sheet = $objExcel->getActiveSheet()->setTitle('Xuất dữ liệu');
	//--Tạo tiêu đề các cột A, B, C, D... bắt đầu từ hàng 4 trong Excel.
	$rowCount = 4;
	$sheet->setCellValue('A'.$rowCount,'STT');
	$sheet->setCellValue('B'.$rowCount,'TÊN ĐĂNG NHẬP');
	$sheet->setCellValue('C'.$rowCount,'EMAIL');
	$sheet->setCellValue('D'.$rowCount,'HỌ VÀ TÊN ĐỆM');
	$sheet->setCellValue('E'.$rowCount,'TÊN');
	$sheet->setCellValue('F'.$rowCount,'NGÀY SINH');
	$sheet->setCellValue('G'.$rowCount,'GIỚI TÍNH');
	//Lấy dữ liệu từ MySQL:
	$sql_query = "SELECT * FROM nv4_users";
	$resultset = $db->query($sql_query);
	//Chèn dữ liệu vào file Excel.
	$stt = 1;
	foreach($resultset as $developer) {
		$rowCount++;
		$sheet->setCellValue('A'.$rowCount,$stt);
		$sheet->setCellValue('B'.$rowCount,$developer['username']);
		$sheet->setCellValue('C'.$rowCount,$developer['email']);
		$sheet->setCellValue('D'.$rowCount,$developer['last_name']);
		$sheet->setCellValue('E'.$rowCount,$developer['first_name']);
		$developer['birthday'] = nv_date('d/m/Y', $developer['birthday']);
		$sheet->setCellValue('F'.$rowCount,$developer['birthday']);
		$sheet->setCellValue('G'.$rowCount,$developer['gender']);
		$stt++;
		}
	//--Set độ rộng cột A là tự động.
		$sheet->getColumnDimension('A')->setAutoSize(true);
		$sheet->getColumnDimension('B')->setAutoSize(true);
		$sheet->getColumnDimension('C')->setAutoSize(true);
		$sheet->getColumnDimension('D')->setAutoSize(true);
		$sheet->getColumnDimension('E')->setAutoSize(true);
		$sheet->getColumnDimension('F')->setAutoSize(true);
		$sheet->getColumnDimension('G')->setAutoSize(true);
	//--Căn giữa hàng tiêu đề A4:G4.
		$sheet->getStyle('A4:G4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	// Set border Từ A4 đến G cuối cùng +1
			$styleArray = array(
				'borders' => array(
					'allborders' => array(
						'style' => PHPExcel_Style_Border::BORDER_THIN,
						'color' => array(
							'argb' => 'FF000000'
						)
					)
				)
			);
			$sheet->getStyle('A4' . ':' . 'G'.($rowCount+1))->applyFromArray($styleArray);
	//Set font size.	
			$sheet->getStyle('A1'.':'.'G'.($rowCount+1))
					->getFont()->setBold(false)
                    ->setName('Times New Roman')
                    ->setSize(14);
	// Tai file vừa tạo về máy.
	$objWriter = new PHPExcel_Writer_Excel2007($objExcel);
	$filename = 'xuatex.xlsx';
	$objWriter->save($filename);
	header('Content-Disposition: attachment; filename="'.$filename.'"');
	header('Content-Type: application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet');
	header('Content-Length: ' . filesize($filename));
	header('Content-Transfer-Encoding: binary');
	header('Cache-Control: must-revalidate');
	header('Pragma: no-cache');
	readfile($filename);
}

$xtpl->parse('main');
$contents = $xtpl->text('main');

include NV_ROOTDIR . '/includes/header.php';
echo nv_admin_theme($contents);
include NV_ROOTDIR . '/includes/footer.php';
  Và đây là thành quả khi xuất ra:
Screenshot 2023 05 02 234619

Nếu thấy hữu ích, xin đừng tiếc cho tôi xin một ĐĂNG KÝ KÊNH và một LIKE. Xin cảm ơn!

Hình ảnh

Tác giả: admin

Tổng số điểm của bài viết là: 20 trong 4 đánh giá

Xếp hạng: 5 - 4 phiếu bầu

  Ý kiến bạn đọc

Top điểm cao
  • 9.6
    Quản Lý KSCL
    KSCL TIẾNG ANH 9
  • 8.8
    Quản Lý KSCL
    KSCL TIẾNG ANH 9
  • 8.8
    Quản Lý KSCL
    KSCL TIẾNG ANH 9
  • 6.8
    Quản Lý KSCL
    KSCL TIẾNG ANH 9
  • 0.4
    Nguyễn Đức Lưu
    KSCL TOÁN 9
Thành viên
Hãy đăng nhập thành viên để trải nghiệm đầy đủ các tiện ích trên site

Đăng nhập thông qua Google
Bạn đã không sử dụng Site, Bấm vào đây để duy trì trạng thái đăng nhập. Thời gian chờ: 60 giây