php 原生生成xls和csv

2021-07-25 10:59:27 字數 1774 閱讀 9339

參考文獻:

1、生成xls

核心**

//開始輸出

$excel = new excel();//引入類檔案,例項化類

header('content-type: text/html; charset=utf-8');

header("pragma: public");

header("expires: 0");

header("cache-control: must-revalidate, post-check=0, pre-check=0");

header("content-disposition: attachment;filename=".$filename.".xls");

header("content-transfer-encoding: binary ");

$excel->xlsbof();

//輸出標題         

for ($i=0;$i<$filedsnum;$i++)

for ($sh=0;$sh<$rownum;$sh++)

}$excel->xlseof();   

exit;

對應的類檔案excel.php

<?php

//excel匯出類

class excel

//excel檔案結束   

function xlseof() 

//中文資料轉換

function format( $str )

*/$str = iconv( "utf-8", "gb2312", $str );

return $str;

}//儲存單元格是數字

function xlswritenumber($row, $col, $value) 

//存單元是字元

function xlswritelabel($row, $col, $value ) 

//寫入內容

// $label_true 強制用字元儲存

function write_excel_line($hang,$lie,$val,$label_true=false)

else if(is_numeric($val) && !$label_true)

else}}

?>

2、生成csv檔案

核心**

header('content-type: text/html; charset=gbk');     

header("content-disposition: attachment;filename=".$filename.".csv");

header("content-type: text/x-csv");

header("pragma: no-cache");

header("expires: 0");

//輸出標題     

$header_csv="";   

for ($i=0;$i<$filedsnum;$i++)

$header_csv=substr($header_csv,0,strlen($header_csv)-1)."\n";       

echo $header_csv;

for ($sh=0;$sh<$rownum;$sh++)

$detail_csv=$detail_csv."\"".$rec[$fileds[$i]]."\"\n"; 

echo  $detail_csv;     

$detail_csv=""; }

exit;

php原生 生成隨機字串

生成隨機字串 param integer length 生成的長度 param integer type 生成的型別 return type str description php 隨機碼型別 0,數字 大寫字母 1,數字 2,小寫字母 3,大寫字母 4,特殊字元 1,數字 大小寫字母 特殊字元 f...

Android原生生成JSON與解析JSON

json資料是一種輕量級的資料交換格式,在 android 中通常應用於客戶端與伺服器互動之間的資料傳輸。像現在在網上有很多解析 json 資料的jar 包,但是歸根到底用的都是 android 原生解析 json 資料的方式,所以掌握 android 原生解析 json 資料的方法相當重要。下面分...

php 生成csv檔案

廢話不多說,上乾貨 方法一 匯出excel csv data 匯出資料 headlist 第一行,列名 filename 輸出excel檔名 functioncsv export data array headlist array filename 將資料通過fputcsv寫到檔案控制代碼 fput...