用phpexcel 导出不能打开问题 20
用phpexcel导出excel出现这错误,在xp中导出是没问题的,在win64中导出就出现这提示了贴上代码publicfunctionexport_excel($col...
用phpexcel导出excel出现这错误
,在xp中导出是没问题的,在win64中导出就出现这提示了贴上代码
public function export_excel($cols, $data, $filename) {
$objPHPExcel = new PHPExcel();
//设置标题
foreach($cols as $k => $v){
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($k.'1', $v['name']);
}
//导出数据
$i = 2;
foreach($data as $row) {
foreach($cols as $k => $v){
//$objPHPExcel->setActiveSheetIndex(0)->setCellValue($k.$i, $row[$v['field']]);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($k.$i, $row[$v['field']]);
}
$i++;
}
//设置列宽
foreach($cols as $k => $v){
if (empty($v['width'])) continue;
$objPHPExcel->getActiveSheet()->getColumnDimension($k)->setWidth($v['width']);
}
//直接下载
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
//操作日志
$this->write_log(ConstantsCMP::LOG_MODULE_EXPORT_EXCEL);
exit;
}
补充:php编码是utf-8无bom编码 展开
,在xp中导出是没问题的,在win64中导出就出现这提示了贴上代码
public function export_excel($cols, $data, $filename) {
$objPHPExcel = new PHPExcel();
//设置标题
foreach($cols as $k => $v){
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($k.'1', $v['name']);
}
//导出数据
$i = 2;
foreach($data as $row) {
foreach($cols as $k => $v){
//$objPHPExcel->setActiveSheetIndex(0)->setCellValue($k.$i, $row[$v['field']]);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($k.$i, $row[$v['field']]);
}
$i++;
}
//设置列宽
foreach($cols as $k => $v){
if (empty($v['width'])) continue;
$objPHPExcel->getActiveSheet()->getColumnDimension($k)->setWidth($v['width']);
}
//直接下载
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
//操作日志
$this->write_log(ConstantsCMP::LOG_MODULE_EXPORT_EXCEL);
exit;
}
补充:php编码是utf-8无bom编码 展开
2个回答
展开全部
自己写吧 别用PHPExcel了
//$fileName文件名
//$headArr标题
//$data数据
public function getExcel($fileName,$headArr,$data){
header("Content-type:application/octet-stream");
header("Accept-Ranges:bytes");
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename={$fileName}.xls");
header("Pragma: no-cache");
header("Expires: 0");
array_unshift($data,$headArr);
foreach($data as $key => $val){
$data[$key]=implode("\t", $data[$key]);
}
echo iconv('utf-8','gbk',implode("\n",$data));
}
这是我自己写的 比较简单
如果需求没要求导出的excel要排版 设置样式之类的话
就凑合着用吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询