php下載excel表格(php導出excel表格)

本文目錄一覽:

PHP請問我需要在頁面上顯示 EXCEL表格,並且能下載怎麼做呢

$retdata=array(); //內容 //二維數組

$filename= “文件.csv”;

//生成頭文件

$now = gmdate(“D, d M Y H:i:s”);

header(“Expires: Tue, 03 Jul 2001 06:00:00 GMT”);

header(“Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate”);

header(“Last-Modified: {$now} GMT”);

header(“Content-Type: application/force-download”);

header(“Content-Type: application/octet-stream”);

header(“Content-Type: application/download”);

header(“Content-Disposition: attachment;filename={$filename}”);

header(“Content-Transfer-Encoding: binary”);

//生成表格

$title=array(‘”金額”‘,'”會員”‘,'”餘額”‘); //每列的標題

$str=implode(‘,’,$title) . PHP_EOL;

foreach ($retdata as $row) {

// print_r($row);

$str_arr = array();

foreach ($row as $column) {

$str_arr[] = ‘”‘ . str_replace(‘”‘, ‘””‘, $column) . ‘”‘;

}

$str.=implode(‘,’, $str_arr) . PHP_EOL;

}

print(chr(0xEF).chr(0xBB).chr(0xBF));

echo $str;

php導出excel表、壓縮成文件下載到本地如何實現?

你好!你所提出的三個問題.我沒怎麼看懂.如果以第二個為主.我倒可以說說我的看法.

一.如何把數據賦到excel中?

答:sorry.沒怎麼明白.見諒.

二.導出為excel表?

答:有兩種方法實現.1.phpexcel.(稍顯複雜) 2.簡單的(我講這個.簡單^_^)直接上代碼.自己改一下名字 和字段名.就成.

?php

error_reporting(0); //屏蔽警告和NOTICE等所有提示.包括error

Header( “Content-type: application/octet-stream “);

Header( “Accept-Ranges: bytes “);

Header( “Content-type:application/vnd.ms-excel;charset=Big5”); //此處寫編碼,如,UTF-8….

Header( “Content-Disposition:attachment;filename=abnormal_Report.xls “); //自己寫文件名 *.xls

require “conn_mysql.php”; //連接mysql

$sql = “select * from `netart`.`abnormal_records` order by record_abtime desc”;

$result = mysql_query($sql,$conn);

echo “table width=’100%’ border=’1′ “;

echo”tr”;

echo “td style=’color:red’ font size=4 ID /font/td”;

echo “td style=’color:red’ font size=4異常時間 /font/td”;

echo “td style=’color:red’ font size=4異常地點 /font/td”;

echo “td style=’color:red’ font size=4詳細內容 /font/td”;

echo “td style=’color:red’ font size=4提交人 /font/td”;

echo “td style=’color:red’ font size=4提交時間 /font /td”;

echo “/tr”;

while ($rs=mysql_fetch_array($result)){

echo “tr”;

echo “td width=’30’ {$rs[‘record_id’]}/td”; //用width 控制表格的寬度.自己改變.

echo “td width=’150′ {$rs[‘record_abtime’]}/td”;

echo “td width=’80’ {$rs[‘record_abplace’]}/td”;

echo “td width=’700′ {$rs[‘record_content’]} /td”;

echo “td width=’60’ {$rs[‘record_username’]} /td”;

echo “td width=’120′ {$rs[‘record_uptime’]} /td”;

echo “/tr”;

}

echo “/tbale”;

?

//以上代碼.自己去改一下名字.和字段名就可以運行了.

==========================================================================

下面的代碼針對MSSQL:(基本跟 Mysql一樣啦.只是改用了ODBC)

?php

error_reporting(0);

Header( “Content-type: application/octet-stream”);

Header( “Accept-Ranges: bytes “);

Header( “Content-type:application/vnd.ms-excel;charset=Big5”);

Header( “Content-Disposition:attachment;filename=Syslog_view.xls “);

require “conn_mssql.php”;

session_start();

$flag1=@$_SESSION[‘flag_1’];

$flag2=@$_SESSION[‘flag_2’];

$flag3=@$_SESSION[‘flag_3’];

$content=@$_SESSION[‘content’];

$ip=@$_SESSION[‘ip’];

$content_2=@$_SESSION[‘content_2’];

$ip_2=@$_SESSION[‘ip_2’];

$time=@$_SESSION[‘time’];

if($flag1==1)

{ $sql_s=”select SysLog.DateTime,SysLog.IP,SysLog.Message from NetPerfMon.dbo.SysLog where IP=’$ip’ and convert(varchar(10),DateTime,120)=’$time’ order by DateTime desc”;}

if($flag2==2)

{ $sql_s=”select SysLog.DateTime,SysLog.IP,SysLog.Message from NetPerfMon.dbo.SysLog where IP=’$ip’ and convert(varchar(10),DateTime,120)=’$time’ order by DateTime desc”;}

if($flag3==3)

{$sql_s=”select SysLog.DateTime,SysLog.IP,SysLog.Message from NetPerfMon.dbo.SysLog where IP=’$ip’ and Message like ‘%$content%’ and convert(varchar(10),DateTime,120)=’$time’ order by DateTime desc”;}

$res=odbc_do($link,$sql_s);

echo “table width=’100%’ border=’1′”;

echo”tr”;

echo “td style=’color:red’ font size=4 DateTime /font/td”;

echo “td style=’color:red’ font size=4 Switch IP /font/td”;

echo “td style=’color:red’ font size=4 Content/font/td”;

echo “/tr”;

while ($rs=odbc_fetch_array($res))

{

echo “tr”;

echo “td width=’130′ {$rs[‘DateTime’]}/td”;

echo “td width=’110′ {$rs[‘IP’]}/td”;

echo “td width=’800′ {$rs[‘Message’]}/td”;

echo “/tr”;

}

echo “/tbale”;

session_stop();

?

三.壓縮成文件下載到本地?

答:此處也沒怎麼明白.因為,你做個按鈕/鏈接至上面的代碼.不就可以保存成excel到本地了..還要做什麼壓縮呢.

綜:回答完畢.希望能幫到你.

php導出excel表後,打包成壓縮包,然後下載到本地如何實現?

用PHPExcel,PHPExcel是相當強大的 MS Office Excel 文檔生成類庫。

你上它的官/網把程序包下/載下來,裏面有 PHPExcel 的程序、還有30個實例程序和三個文檔。

看一下其中的開發文檔你就會用了。

讀取(這段在開發文檔里有的,在13頁):

require_once ‘../Classes/PHPExcel/IOFactory.php’;

$objReader = PHPExcel_IOFactory::createReader(‘Excel2007’);

$objReader-setReadDataOnly(true);

$objPHPExcel = $objReader-load(“test.xlsx”);

$objWorksheet = $objPHPExcel-getActiveSheet();

echo ‘table’ . “\n”;

foreach ($objWorksheet-getRowIterator() as $row) {

echo ‘tr’ . “\n”;

$cellIterator = $row-getCellIterator();

$cellIterator-setIterateOnlyExistingCells(false);

foreach ($cellIterator as $cell) {

echo ‘td’ . $cell-getValue() . ‘/td’ . “\n”;

}

echo ‘/tr’ . “\n”;

}

echo ‘/table’ . “\n”;

?

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/219985.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-09 11:02
下一篇 2024-12-09 11:02

相關推薦

  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • 使用Treeview顯示錶格

    在web開發中,顯示錶格數據是一項很常見的需求。當我們需要在頁面上顯示大量數據時,除了使用傳統的表格樣式外,還可以使用Treeview這種可摺疊的表格樣式,以便更好地展示數據。本文…

    編程 2025-04-29
  • Python根據表格數據生成折線圖

    本文將介紹如何使用Python根據表格數據生成折線圖。折線圖是一種常見的數據可視化圖表形式,可以用來展示數據的趨勢和變化。Python是一種流行的編程語言,其強大的數據分析和可視化…

    編程 2025-04-29
  • Python 圖片轉表格

    本文將詳細介紹如何使用Python將圖片轉為表格。大家平時在處理一些資料的時候難免會遇到圖片轉表格的需求。比如從PDF文檔中提取表格等場景。當然,這個功能也可以通過手動複製、粘貼,…

    編程 2025-04-29
  • PHP怎麼接幣

    想要在自己的網站或應用中接受比特幣等加密貨幣的支付,就需要對該加密貨幣擁有一定的了解,並使用對應的API進行開發。本文將從多個方面詳細闡述如何使用PHP接受加密貨幣的支付。 一、環…

    編程 2025-04-29
  • 為什麼不能用Microsoft Excel進行Python編程?

    Microsoft Excel是一個廣泛使用的數據分析工具,但是它不能直接用於Python編程。這是因為Microsoft Excel並不是一個編程工具,它的主要功能是進行數據處理…

    編程 2025-04-29
  • python爬取網頁並生成表格

    本文將從以下幾個方面詳細介紹如何使用Python爬取網頁數據並生成表格: 一、獲取網頁數據 獲取網頁數據的一般思路是通過HTTP請求獲取網頁內容,最常用的方式是使用Python庫r…

    編程 2025-04-28
  • Python如何做表格為中心

    本文將從多個方面詳細闡述Python如何做表格,包括表格的創建、數據的插入、表格的樣式設置等內容。 一、創建表格 要在Python中創建表格,我們可以使用第三方庫Pandas。具體…

    編程 2025-04-28
  • 使用PHP foreach遍歷有相同屬性的值

    本篇文章將介紹如何使用PHP foreach遍歷具有相同屬性的值,並給出相應的代碼示例。 一、基礎概念 在講解如何使用PHP foreach遍歷有相同屬性的值之前,我們需要先了解幾…

    編程 2025-04-28
  • 基尼係數Excel計算模板

    這篇文章將介紹基尼係數Excel計算模板,為大家詳細闡述如何使用Excel進行基尼係數的計算。 一、模板下載及導入 首先需要下載基尼係數的Excel計算模板,可以在Excel中通過…

    編程 2025-04-28

發表回復

登錄後才能評論