php excel

<?php
require_once '../framework/library/phpexcel/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");

// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'ID')
->setCellValue('B1', '姓名')
->setCellValue('C1', '学号')
->setCellValue('D1', '成绩');

$link = mysqli_connect('127.0.0.1', 'root', 'xxxxxx', 'YC_test');
mysqli_query($link, "set names 'utf8'");

$sql_sec = "select * from lgd_score where 1";
$res_sec = mysqli_query($link,$sql_sec);

$count=mysqli_num_rows($res_sec);

for($it=0;$it<$count;$it++){
$i=$it+2;
$row = mysqli_fetch_array($res_sec);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A' . $i, $row['id'])
->setCellValue('B' . $i, $row['name'])
->setCellValueExplicit('C' . $i, $row['num'],PHPExcel_Cell_DataType::TYPE_STRING)
->setCellValue('D' . $i, $row['score']);

}
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(22);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(22);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle(date('Y-m-d'));

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="理工大图书馆答题成绩:'.date('Y-m-d').'.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
原文地址:https://www.cnblogs.com/yangchong/p/6179131.html