thinkphp中使用phpexcel生成excel格式文件
2014-03-13 18:51 来源:www.chinab4c.com 作者:ecshop专家
vendor('PHPExcel.PHPExcel');
$fileName = 'BOM-'.date('Y-m-d',time());
$PHPExcel = new PHPExcel();
$PHPExcel->createSheet();
$subObject = $PHPExcel->getSheet(1);
$subObject->setTitle('data');
//填入表头
$PHPExcel->getActiveSheet()->setCellValue('A1', '型号');
$PHPExcel->getActiveSheet()->setCellValue('B1', '品牌');
$PHPExcel->getActiveSheet()->setCellValue('C1', '数量');
$PHPExcel->getActiveSheet()->setCellValue('D1', '用量');
$PHPExcel->getActiveSheet()->setCellValue('E1', '包装');
$PHPExcel->getActiveSheet()->setCellValue('F1', '价格');
$PHPExcel->getActiveSheet()->setCellValue('G1', '交货时间');
$PHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(20);
//填入列表
$k = 1;
foreach ($b_item as $key => $v) {
$k++;
$PHPExcel->getActiveSheet()->setCellValue('A'.($k), $v['goods_name']);
$PHPExcel->getActiveSheet()->setCellValue('B'.($k), $v['brand_name']);
$PHPExcel->getActiveSheet()->setCellValue('C'.($k), $v['goods_number']);
$PHPExcel->getActiveSheet()->setCellValue('D'.($k), $v['dosage']);
$PHPExcel->getActiveSheet()->setCellValue('E'.($k), $v['package_info']);
$PHPExcel->getActiveSheet()->setCellValue('F'.($k), $v['goods_price']);
$PHPExcel->getActiveSheet()->setCellValue('G'.($k), $v['send_time']);
$PHPExcel->getActiveSheet()->getRowDimension($k)->setRowHeight(20);
}
//设置单元格宽度
/*$PHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(6);
$PHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
$PHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
$PHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
$PHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
$PHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
$PHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(16);
*/
//保存为2003格式
$objWriter = new PHPExcel_Writer_Excel5($PHPExcel);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
//多浏览器下兼容中文标题
$encoded_filename = urlencode($fileName);
$ua = $_SERVER["HTTP_USER_AGENT"];
if (preg_match("/MSIE/", $ua)) {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '.xls"');
} else if (preg_match("/Firefox/", $ua)) {
header('Content-Disposition: attachment; filename*="utf8\'\'' . $fileName . '.xls"');
} else {
header('Content-Disposition: attachment; filename="' . $fileName . '.xls"');
}
header("Content-Transfer-Encoding:binary");
$objWriter->save('php://output');
来源:http://www.chinab4c.com
最近更新
常用插件
- ecshop降价通知登记插件
ecshop降价通知登记插件,主要是为了方便某些客户,对商品价格要求比...
- ecshop通用红包编码
很多时候,为了结合促销,必须扩展一下ecshop的红包功能。ecshop的红包...
- ecshop购物车功能改进[插件
ecshop购物车功能改进[插件套餐]主要是我们最近开发工作和开发项目中。...
- ecshop红包修改成满多少减
我们在长期使用ecshop的时候,我们可以发现。ecshop的红包是一个非常强...
- ecshop分类树中统计商品数
最近忙于开发其他项目,在不少朋友不断要求和催促的情况下,做出了该小...