尝试使用PHPExcel

时间:2015-07-20 22:02:11

标签: php phpexcel

我在stackoverflow上关注了问题,最后得到了以下代码

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.$name.'.xlsx"');
$objWriter->save('php://output');

问题是输出文件是错误的,它的中文(可能是编码),我想知道是否有编码标题或是否有其他问题。

编辑:

header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Pragma: public');

Excel准备代码:

$iterator = 0;
foreach (get_object_vars($temp[0]) as $key => $value){
    $formattedkey = str_replace('_', ' ', $key);
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($iterator, 1, $formattedkey);
    $iterator++;
    $fieldsnum++;
}

$rowiterator = 2;
foreach ($temp as $asset){
    $coliterator = 0;
    foreach (get_object_vars($asset) as $key => $value){
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($coliterator, $rowiterator, utf8_encode($value));
        $coliterator++;
    }
    $rowiterator++;
}

0 个答案:

没有答案