<?php

namespace Common\Cls;

/**
 * 生成excel文件操作
 *
 * @author wesley wu
 * @date 2013.12.9
 */
class Excel {

	private $limit = 10000;

	public function download($data, $fileName) {
		header("Content-Type: application/vnd.ms-excel; charset=gbk");
		header("Content-Disposition: inline; filename=\"" .  \iconv('utf8', 'gbk', $fileName) . ".xls\"");
		echo "<?xml version=\"1.0\" encoding=\"gbk\"?>\n
            <Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"
            xmlns:x=\"urn:schemas-microsoft-com:office:excel\"
            xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"
            xmlns:html=\"http://www.w3.org/TR/REC-html40\">";
		echo "\n<Worksheet ss:Name=\"" . \iconv('utf8', 'gbk', $fileName) . "\">\n<Table>\n";
		$guard = 0;
		foreach ($data as $v) {
			$guard++;
			if ($guard == $this->limit) {
				ob_flush();
				flush();
				$guard = 0;
			}
			echo $this->_addRow($v);
		}
		echo "</Table>\n</Worksheet>\n</Workbook>";
	}

	private function _addRow($row) {
		$cells = "";
		foreach ($row as $k => $v) {
			$cells .= "<Cell><Data ss:Type=\"String\">" . \iconv('utf8', 'gbk', $v) . "</Data></Cell>\n";
		}
		return "<Row>\n" . $cells . "</Row>\n";
	}

	function buildData($header = [], $data = []) {
		$arr = [];
		foreach ($data as $item) {
			$arr[] = array_values($item);
		}
		if ($header) array_unshift($arr, $header);
		return $arr;
	}
}
form.on('submit(export)', function (e) {
	e.field.export = 'on'
	query = parseParams(e.field)
	console.log(query)
	location.href = '__ACTION__/?' + query
	return false
})

parseParams = function (data) {
	try {
		var tempArr = []
		for (var i in data) {
			var key = encodeURIComponent(i)
			var value = encodeURIComponent(data[i])
			tempArr.push(key + '=' + value)
		}
		var urlParamsStr = tempArr.join('&')
		return urlParamsStr
	} catch (err) {
		return ''
	}
}
if (\I('export') == 'on') {
	$header = ['日期', '项目', '工程机械', '开机时间', '关机时间', '台时', '驾驶员', '审核状态', '项目经理', '审核时间'];
	$data = \M('job')
		->field("job_startdate ,job_prj_id_ ,job_car_id_ ,job_startdatetime ,job_enddatetime ,job_hours ,job_drv_id_ ,job_status_ ,job_mgr_id_ ,job_statusdatetime")
		->where($map)
		->order("job_startdate desc,job_id desc")
		->select();
	$excel = new Excel;
	$data = $excel->buildData($header, $data);
	$excel->download($data, '施工日志' . \date('YmdHis'));
	die;
}
版权声明:本文为小馬過河﹎原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/sunr/p/16670083.html