bztang-admin/app/backend/modules/order/services/models/ExcelModel.php

63 lines
2.9 KiB
PHP

<?php
/**
* Created by PhpStorm.
* Author:
* Date: 2017/3/13
* Time: 下午3:04
*/
namespace app\backend\modules\order\services\models;
class ExcelModel
{
protected function column_str($key)
{
$array = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ', 'BA', 'BB', 'BC', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BK', 'BL', 'BM', 'BN', 'BO', 'BP', 'BQ', 'BR', 'BS', 'BT', 'BU', 'BV', 'BW', 'BX', 'BY', 'BZ', 'CA', 'CB', 'CC', 'CD', 'CE', 'CF', 'CG', 'CH', 'CI', 'CJ', 'CK', 'CL', 'CM', 'CN', 'CO', 'CP', 'CQ', 'CR', 'CS', 'CT', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ'
);
return $array[$key];
}
protected function column($key, $columnnum = 1)
{
return $this->column_str($key) . $columnnum;
}
public function export($list, $params = array())
{
if (PHP_SAPI == 'cli') {
die('This example should only be run from a Web Browser');
}
//ob_end_clean();
require_once base_path() . '/vendor/phpoffice/phpexcel/Classes/PHPExcel.php';
$excel = new \PHPExcel();
$excel->getProperties()->setCreator("芸众商城")->setLastModifiedBy("芸众商城")->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("report file");
$sheet = $excel->setActiveSheetIndex(0);
$rownum = 1;
foreach ($params['columns'] as $key => $column) {
$sheet->setCellValue($this->column($key, $rownum), $column['title']);
if (!empty($column['width'])) {
$sheet->getColumnDimension($this->column_str($key))->setWidth($column['width']);
}
}
$rownum++;
foreach ($list as $row) {
$len = count($params['columns']);
for ($i = 0; $i < $len; $i++) {
$value = $row[$params['columns'][$i]['field']];
$value = @iconv("utf-8", "gbk", $value);
$value = @iconv("gbk", "utf-8", $value);
$sheet->setCellValueExplicit($this->column($i, $rownum), $value, \PHPExcel_Cell_DataType::TYPE_STRING);
}
$rownum++;
}
$excel->getActiveSheet()->setTitle($params['title']);
$filename = $params['title'] . '-' . date('Y-m-d H:i', time());
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $filename . '.xls"');
header('Cache-Control: max-age=0');
$writer = \PHPExcel_IOFactory::createWriter($excel, 'Excel5');
$writer->save('php://output');
exit;
}
}