-1

所以我在这里有这段代码,我从互联网上下来的,它应该将数据从 mySQL 数据库导出到一个 excel 文件。问题是我应该如何触发这个?这没有图形方面,没有触发脚本的按钮,什么都没有。我怎样才能让它工作?

<?PHP
  // Original PHP code by Chirp Internet: www.chirp.com.au
  // Please acknowledge use of this code by including this header.

  include('config.php');
  function cleanData(&$str)
  {
    $str = preg_replace("/\t/", "\\t", $str);
    $str = preg_replace("/\r?\n/", "\\n", $str);
    if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
  }

  // filename for download
  $filename = "website_data_" . date('Ymd') . ".xls";

  header("Content-Disposition: attachment; filename=\"$filename\"");
  header("Content-Type: application/vnd.ms-excel");

  $flag = false;
  $result = pg_query("SELECT * FROM norse5_proov ORDER BY id") or die('Query failed!');
  while(false !== ($row = pg_fetch_assoc($result))) {
    if(!$flag) {
      // display field/column names as first row
      echo implode("\t", array_keys($row)) . "\r\n";
      $flag = true;
    }
    array_walk($row, 'cleanData');
    echo implode("\t", array_values($row)) . "\r\n";
  }
  exit;
?>
4

1 回答 1

1

这是一些适用于商业应用程序的代码;

if($_REQUEST["EXCEL"]){
   header("Content-Type: application/excel");
   header("Content-Disposition: attachment; FileName=assets.xls");
   header("Cache-Control: private");
   header("Content-Length: ".strLen($data));
   echo $data;
   exit;
}

$data 是制表符分隔的文本,但 excel 标题打开 Excel 并导入文本

于 2013-03-11T10:18:57.223 回答