Save mysql query for excel file from php webpage

I have a table in mysql database. What I want to do is on the php page, run the select statement and print the result to an excel file.

I saw quite a few tutorials and tried them, however they output the contents of the web page to an excel file. I just need data and mysql table headers.

Is there any way to do this?

+3
source share
3 answers

You can use one of the available PHP libraries for this. One , two .

One thing that is developing pretty fast, but not as neat as using LIBRARY for this:

  • Run MySQL query
  • HTML
  • : application/vnd.ms-excel

HTML Excel. HTTP, .

+3

PHPExcel , , , .

, ( , )

+1

Excel. lib:

  • :: WriteExcel: Excel
  • (C) 2002 Xavier Noguer xnoguer@rezebra.com

:

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$filename" );
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
header("Pragma: public");

$formato1 =& $workbook->add_format();
$formato1->set_border(1);
$formato1->set_bg_color('white');
$formato1->set_fg_color('grey');
$formato1->set_pattern();
$formato1->set_size(10);
$formato1->set_color('white');
$formato1->set_align('center');
$formato1->set_bold();

$formato2 =& $workbook->add_format();
$formato2->set_size(10);
$formato2->set_border(1);

$worksheet1 =& $workbook->add_worksheet('Relatório de Inscrições');
$linha = 1;

//Query dos dados das inscrições recebidas

$sql = "select 
          A.Name, A.Code
        from 
          Customers A
        order by
          A.Name";

$resultado = $conn ->_execute($sql);

$worksheet1->write_string(0, 0, 'Name', $formato1);
$worksheet1->write_string(0, 1, 'Code', $formato1);

for($a = 0; $a < count($resultado); $a++) 
{
  $row = $resultado[$a];

  $worksheet1->write_string($linha, 0, utf8_decode($row->Name),    $formato2);  
  $worksheet1->write_number($linha, 1, $row->Code,                 $formato2);

  $linha++;
}  

$workbook->close();
0
source

Source: https://habr.com/ru/post/1767343/


All Articles