Create xlsx File with html table

Asked

Viewed 155 times

0

Good, I am trying to pass the values of an HTML table that receives data from a SYBASE database through odbc.

When creating an 'xlsx' file formatting excel prevents the created page from opening and displays the following error " excel cannot open the following file because the format or file extension is not valid".

The code used in this situation is as follows: :

header("Content-Type:   application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8");
header("Content-Disposition: attachment; filename=abc.xlsx");  //File name extension was wrong
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);

Otherwise, I tried to save the file as a . xls and it gives me the same error, although, it allows me to open the file.

Code used in this situation :

header("Content-Type: application/vnd.ms-excel; charset=utf-8"); header("Content-Disposition: attachment; filename=abc.xls"); //File name extension was wrong header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false);

The problem is that I really need the created file to be an . xlsx file and not an . xls.

I’ve tried using Phpexcel to do it, and the truth is I can create a table. xlxs but the complexity of the data I receive makes it a little complex that I am creating a cycle and having to fill the cell as I have to in Phpexcel. Another problem I find here is that the first line of the result coming from the database is ignored.

The code you were using with Phpexcel is as follows:

$ano = addslashes( $_SESSION['anop']);
$codigo = addslashes( $_SESSION['codigo']);

$db = odbc_connect($dsn, $user, $pwd);

$query = odbc_exec($db, 'Select * from GP_Vw_Valores_Pagos where Ano='.$ano.' and Codigo='.$codigo.' order by CD');

 $row = odbc_fetch_array($query);


            $objPHPExcel = new PHPExcel();

        $rowCount = 1; 
        $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount,'Cd');
        $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount,'Descricao');
        $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount,'Tipo');
        $objPHPExcel->getActiveSheet()->SetCellValue('D'.$rowCount,'Janeiro');
        $objPHPExcel->getActiveSheet()->SetCellValue('E'.$rowCount,'Fevereiro');
        $objPHPExcel->getActiveSheet()->SetCellValue('G'.$rowCount,'Março');
        $objPHPExcel->getActiveSheet()->SetCellValue('H'.$rowCount,'Abril');
        $objPHPExcel->getActiveSheet()->SetCellValue('I'.$rowCount,'Maio');
        $objPHPExcel->getActiveSheet()->SetCellValue('J'.$rowCount,'Junho');
        $objPHPExcel->getActiveSheet()->SetCellValue('L'.$rowCount,'Julho');
        $objPHPExcel->getActiveSheet()->SetCellValue('M'.$rowCount,'Agosto');
        $objPHPExcel->getActiveSheet()->SetCellValue('N'.$rowCount,'Setembro');
        $objPHPExcel->getActiveSheet()->SetCellValue('O'.$rowCount,'Outubro');
        $objPHPExcel->getActiveSheet()->SetCellValue('P'.$rowCount,'Novembro');
        $objPHPExcel->getActiveSheet()->SetCellValue('Q'.$rowCount,'Dezembro');


        while ($row = odbc_fetch_array($query)){ 
            $rowCount++;

            $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['CD']);
               $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['Descricao']);


        } ;

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
    ');
    header('Content-Disposition: attachment;filename="01simple.xlsx"');
    header('Cache-Control: max-age=0'); 

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save('php://output');

Can anyone detect an error or have any solution better than these?

1 answer

0

I enlarged the memory with the following code:

ini_set('memory_limit', '3000M');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.