我正在尝试使用PHPExcel将html表转换为excel。问题是,有时我会收到错误消息(“无法打开文件,导致其格式/扩展名已损坏”,而其他时候却没有,它可以完美运行。 通常,我在尝试创建文件的前3次出现错误,而在第4次运行良好时就出现错误。
有人知道为什么会这样吗?我考虑过要放在桌子上的信息量,但老实说,我不知道。
<?php
session_start();
require_once '../classes/PHPExcel-1.8/Classes/PHPExcel.php';
ini_set('max_execution_time', 300);
$servidorodbc=file('../public/odbc.txt',FILE_IGNORE_NEW_LINES);
$dsn=$servidorodbc['0'];
$user=$servidorodbc['1'];
$pwd=$servidorodbc['2'];
if($pwd="''"){
$pwd="";
}
$ano = addslashes( $_SESSION['anopr']);
$mes = addslashes( $_SESSION['mespr']);
$db = odbc_connect($dsn, $user, $pwd);
$query = odbc_exec($db, "Select
Cliente,Nome_Cli,CCusto,Descricao,Codigo,Nome,Tipo,Provisoes,Pagamentos,Acrescimos,Liquidacoes,Saldo,Ac_Provisoes,Ac_Pagamento,Ac_Acrescimos,Ac_Liquidacoes,Ac_Saldo,Amor_Abertura,Amor_Utilizacao,Amor_Fecho,Amor_Saldo
from GP_Provisoes_Acum where Ano=".$ano." and Mes=".$mes."
union all
Select Cliente=0,Nome_Cli='',CCusto=0,Descricao='SOMATÓRIOS TOTAIS',Codigo=0,Nome='',Tipo='TOTAL',
SUM(Provisoes) as Provisoes, SUM(Pagamentos) as Pagamentos,SUM(Acrescimos) as Acrescimos,
SUM(Liquidacoes) as Liquidacoes,SUM(Saldo) as Saldo, SUM(Ac_Provisoes) as Ac_Provisoes,
SUM(Ac_Pagamento) as Ac_Pagamento, SUM(Ac_Acrescimos) as Ac_Acrescimos, SUM(Ac_Liquidacoes) as Ac_Liquidacoes,
SUM(Ac_Saldo) as Ac_Saldo, Amor_Abertura=0,Amor_Utilizacao=0,Amor_Fecho=0,Amor_Saldo=0
from GP_Provisoes_Acum where Ano=".$ano." and Mes=".$mes
);
$objPHPExcel = new PHPExcel();
$rowCount = 1;
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount,'Cliente');
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount,'Nome do Cliente');
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount,'Centro de Custo');
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount,'Descricao');
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowCount,'Codigo');
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$rowCount,'Nome');
$objPHPExcel->getActiveSheet()->SetCellValue('G'.$rowCount,'Tipo');
$objPHPExcel->getActiveSheet()->SetCellValue('H'.$rowCount,'Provisoes');
$objPHPExcel->getActiveSheet()->SetCellValue('I'.$rowCount,'Pagamentos');
$objPHPExcel->getActiveSheet()->SetCellValue('J'.$rowCount,'Acrescimo');
$objPHPExcel->getActiveSheet()->SetCellValue('K'.$rowCount,'Liquidacoes');
$objPHPExcel->getActiveSheet()->SetCellValue('L'.$rowCount,'Saldo');
$objPHPExcel->getActiveSheet()->SetCellValue('M'.$rowCount,'Ac_Provisoes');
$objPHPExcel->getActiveSheet()->SetCellValue('N'.$rowCount,'Ac_Pagamento');
$objPHPExcel->getActiveSheet()->SetCellValue('O'.$rowCount,'Ac_Acrescimo');
$objPHPExcel->getActiveSheet()->SetCellValue('P'.$rowCount,'Ac_Liquidacoes');
$objPHPExcel->getActiveSheet()->SetCellValue('Q'.$rowCount,'Ac_Saldo');
$objPHPExcel->getActiveSheet()->SetCellValue('R'.$rowCount,'Amor_Abertura');
$objPHPExcel->getActiveSheet()->SetCellValue('S'.$rowCount,'Amor_Utilização');
$objPHPExcel->getActiveSheet()->SetCellValue('T'.$rowCount,'Amor_Fecho');
$objPHPExcel->getActiveSheet()->SetCellValue('U'.$rowCount,'Amor_Saldo');
while ($row = odbc_fetch_array($query)){
$rowCount++;
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount,$row['Cliente']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount,$row['Nome_Cli']);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount,$row['CCusto']);
$objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount,$row['Descricao']);
$objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowCount,$row['Codigo']);
$objPHPExcel->getActiveSheet()->SetCellValue('F'.$rowCount,$row['Nome']);
$objPHPExcel->getActiveSheet()->SetCellValue('G'.$rowCount,$row['Tipo']);
$objPHPExcel->getActiveSheet()->SetCellValue('H'.$rowCount,$row['Provisoes']);
$objPHPExcel->getActiveSheet()->SetCellValue('I'.$rowCount,$row['Pagamentos']);
$objPHPExcel->getActiveSheet()->SetCellValue('J'.$rowCount,$row['Acrescimos']);
$objPHPExcel->getActiveSheet()->SetCellValue('K'.$rowCount,$row['Liquidacoes']);
$objPHPExcel->getActiveSheet()->SetCellValue('L'.$rowCount,$row['Saldo']);
$objPHPExcel->getActiveSheet()->SetCellValue('M'.$rowCount,$row['Ac_Provisoes']);
$objPHPExcel->getActiveSheet()->SetCellValue('N'.$rowCount,$row['Ac_Pagamento']);
$objPHPExcel->getActiveSheet()->SetCellValue('O'.$rowCount,$row['Ac_Acrescimos']);
$objPHPExcel->getActiveSheet()->SetCellValue('P'.$rowCount,$row['Ac_Liquidacoes']);
$objPHPExcel->getActiveSheet()->SetCellValue('Q'.$rowCount,$row['Ac_Saldo']);
$objPHPExcel->getActiveSheet()->SetCellValue('R'.$rowCount,$row['Amor_Abertura']);
$objPHPExcel->getActiveSheet()->SetCellValue('S'.$rowCount,$row['Amor_Utilizacao']);
$objPHPExcel->getActiveSheet()->SetCellValue('T'.$rowCount,$row['Amor_Fecho']);
$objPHPExcel->getActiveSheet()->SetCellValue('U'.$rowCount,$row['Amor_Saldo']);
}
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
');
header('Content-Disposition: attachment;filename="Tabela_de_Previsoes.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
$objWriter->save('php://output');
?>
答案 0 :(得分:0)
如果您的数据库包含数百万条信息,那么它肯定会减慢该过程。
另一个原因可能与您在循环中创建的单元有关。
有关代码的更多信息将更好地概述这种情况。
答案 1 :(得分:0)
在这种情况下,当文件生成过程中出现错误时,excel文件可能会损坏。
尝试使用本机文本阅读器(例如记事本)打开excel文件,然后查看是否存在错误/警告/通知。
确保启用错误报告/显示。