Board logo

Title: 用 PHPExcel-1.8 ,导出公司考勤EXCEL文件。 [Print this page]

Author: sky999    Time: 2023-7-16 15:46     Title: 用 PHPExcel-1.8 ,导出公司考勤EXCEL文件。



CODE:
[Copy to clipboard]
// Include the PHPExcel classes
require 'Classes/PHPExcel.php';
require 'Classes/PHPExcel/IOFactory.php';

// Create a new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set properties of the Excel file
$objPHPExcel->getProperties()->setTitle("Attendance Report");

// Create a worksheet
$objPHPExcel->setActiveSheetIndex(0);
$sheet = $objPHPExcel->getActiveSheet();

// Set column headers
$sheet->setCellValue('A1', 'id');
$sheet->setCellValue('B1', '序号');
$sheet->setCellValue('C1', 'date&tid');
$sheet->setCellValue('D1', 'AM1');
$sheet->setCellValue('E1', 'AM2');
$sheet->setCellValue('F1', 'PM1');
$sheet->setCellValue('G1', 'PM2');
$sheet->setCellValue('H1', 'url');
$sheet->setCellValue('I1', '备注remark');
$sheet->setCellValue('J1', 'uid');

// Fetch data from the database
$selectQuery = "SELECT * FROM attendance2 WHERE uid ='{$uid}' AND write_s = 1";
$result = $conn->query($selectQuery);

$rowIndex = 2; // Start from row 2 to skip the header row

while ($row = $result->fetch_assoc()) {
    $sheet->setCellValue('A' . $rowIndex, $row['id']);
    $sheet->setCellValue('B' . $rowIndex, $i);
    $sheet->setCellValue('C' . $rowIndex, $row['date'] . ', 星期' . $row['week']);
    $sheet->setCellValue('D' . $rowIndex, $row['AM1'] . ', ' . $row['AM1pid']);
    $sheet->setCellValue('E' . $rowIndex, $row['AM2'] . ', ' . $row['AM2pid']);
    $sheet->setCellValue('F' . $rowIndex, $row['PM1'] . ', ' . $row['PM1pid']);
    $sheet->setCellValue('G' . $rowIndex, $row['PM2'] . ', ' . $row['PM2pid']);
    $sheet->setCellValue('H' . $rowIndex, '打开主题, 编号' . $row['tid']);
    $sheet->setCellValue('I' . $rowIndex, $row['remark']);
    $sheet->setCellValue('J' . $rowIndex, $row['uid'] . ', ' . $row['username']);

    $rowIndex++;
}

// Auto-size columns for better readability
$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);
$sheet->getColumnDimension('H')->setAutoSize(true);
$sheet->getColumnDimension('I')->setAutoSize(true);
$sheet->getColumnDimension('J')->setAutoSize(true);

// Save the Excel file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$filename = 'attendance_report.xlsx';
$objWriter->save($filename);

// Download the file
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Cache-Control: max-age=0');
ob_clean();
flush();
readfile($filename);
unlink($filename);
exit;

$result->close();
$conn->close();





Welcome AbyssalSwamp (http://service.caffz.com/mud/AbyssalSwamp/index/) caffz.com