源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

基于php导出到Excel或CSV的详解(附utf8、gbk 编码转换)

  • 时间:2020-01-07 14:41 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:基于php导出到Excel或CSV的详解(附utf8、gbk 编码转换)
php导入到excel乱码是因为utf8编码在xp系统不支持所有utf8编码转码一下就完美解决了 [b]utf-8编码案例 [/b]Php代码
[url=?action=make]?>
也可以做一个封闭函数: [b]封闭函数一: [/b] [b]封闭函数二: [/b]
[u]复制代码[/u] 代码如下:
<? /**  * Simple class to properly output CSV data to clients. PHP 5 has a built  * in method to do the same for writing to files (fputcsv()), but many times  * going right to the client is beneficial.  *  * @author Jon Gales  */ class CSV_Writer {     public $data = array();     public $deliminator;     /**      * Loads data and optionally a deliminator. Data is assumed to be an array      * of associative arrays.      *      * @param array $data      * @param string $deliminator      */     function __construct($data, $deliminator = ",")     {         if (!is_array($data))         {             throw new Exception('CSV_Writer only accepts data as arrays');         }         $this->data = $data;         $this->deliminator = $deliminator;     }     private function wrap_with_quotes($data)     {         $data = preg_replace('/"(.+)"/', '""$1""', $data);         return sprintf('"%s"', $data);     }     /**      * Echos the escaped CSV file with chosen delimeter      *      * @return void      */     public function output()     {         foreach ($this->data as $row)         {             $quoted_data = array_map(array('CSV_Writer', 'wrap_with_quotes'), $row);             echo sprintf("%s/n", implode($this->deliminator, $quoted_data));         }     }     /**      * Sets proper Content-Type header and attachment for the CSV outpu      *      * @param string $name      * @return void      */     public function headers($name)     {         header('Content-Type: application/csv');         header("Content-disposition: attachment; filename={$name}.csv");     } } /* //$data = array(array("one","two","three"), array(4,5,6)); $data[] = array("one","two","three"); $data[] = array(4,5,6); $csv = new CSV_Writer($data); $csv->headers('test'); $csv->output(); */
[b]3. 使用excel类 [/b]
[u]复制代码[/u] 代码如下:
<?php require_once 'Spreadsheet/Writer.php'; $workbook = new Spreadsheet_Excel_Writer(); /* 生成 CSV $filename = date('YmdHis').'.csv'; $workbook->send($filename); // 发送 Excel 文件名供下载 */ // 生成 Excel $filename = date('YmdHis').'.xls'; $workbook->send($filename); // 发送 Excel 文件名供下载 $workbook->setVersion(8); $workbook->setBIFF8InputEncoding('UTF-8'); $worksheet =& $workbook->addWorksheet("Sheet-1"); $data[]= array('id','username','company','email','mob','daytime','intent'); $data[] = array(1,'老梁','**工作室','jb51.net','1363137966*',time(),'y'); $total_row = count($data); $total_col = count($data[0]); for ($row = 0; $row < $total_row; $row ++) {    for ($col = 0; $col < $total_col; $col ++) {   $worksheet->writeString($row, $col, $data[$row][$col]); // 在 sheet-1 中写入数据    } } /* $worksheet =& $workbook->addWorksheet("Sheet-2"); $data[]= array('id','username','company','email','mob','daytime','intent'); $data[] = array(1,'老梁','**工作室','jb51.net','1363137966*',time(),'y'); $total_row = count($data); $total_col = count($data[0]); for ($row = 0; $row < $total_row; $row ++) {    for ($col = 0; $col < $total_col; $col ++) {   $worksheet->writeString($row, $col, $data[$row][$col]); // 在 sheet-2 中写入数据    } } */ $workbook->close(); // 完成下载 ?>
[b]类二 -----函数说明 [/b]读取Excel文件 function Read_Excel_File($ExcelFile,$Result) $ExcelFile    Excel文件名 $Result        返回的结果 函数返回值    正常返回0,否则返回错误信息 返回的值数组 $result[sheet名][行][列] 的值为相应Excel Cell的值 [b]建立Excel文件[/b]    function Create_Excel_File($ExcelFile,$Data) $ExcelFile    Excel文件名 $Data        Excel表格数据 请把函数写在PHP脚本的开头  例1:
[u]复制代码[/u] 代码如下:
<? require "excel_class.php"; Read_Excel_File("Book1.xls",$return); for ($i=0;$i<count($return[Sheet1]);$i++) {     for ($j=0;$j<count($return[Sheet1][$i]);$j++)     {         echo $return[Sheet1][$i][$j]."|";     }     echo "<br>"; } ?>
例2:
[u]复制代码[/u] 代码如下:
<? require "excel_class.php"; Read_Excel_File("Book1.xls",$return); Create_Excel_File("ddd.xls",$return[Sheet1]); ?>
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部