zoukankan      html  css  js  c++  java
  • 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;
  • 相关阅读:
    Hibernate save, saveOrUpdate, persist, merge, update 区别
    Eclipse下maven使用嵌入式(Embedded)Neo4j创建Hello World项目
    Neo4j批量插入(Batch Insertion)
    嵌入式(Embedded)Neo4j数据库访问方法
    Neo4j 查询已经创建的索引与约束
    Neo4j 两种索引Legacy Index与Schema Index区别
    spring data jpa hibernate jpa 三者之间的关系
    maven web project打包为war包,目录结构的变化
    创建一个maven web project
    Linux下部署solrCloud
  • 原文地址:https://www.cnblogs.com/yangchong/p/6179131.html
Copyright © 2011-2022 走看看