php導入到excel亂碼是因為utf
utf
Php代碼
<?php
header("Content
header("Pragma: public");
header("Expires:
header("Cache
header("Content
header("Content
header("Content
header("Content
header("Content
?>
Php代碼
<?
$filename="php導入到excel
$filename=iconv("utf
echo $filename;
?>
gbk編碼案例
Php代碼
<?php
header("Content
header("Pragma: public");
header("Expires:
header("Cache
header("Content
header("Content
header("Content
header("Content
header("Content
?>
Php代碼
訪問網站的時候就下載到excel裡面
要弄單元格區別的話
用table表格做網頁的就可以了
====================== 其他方法 =============================
<?php
$action =$_GET[
if ($action==
$fp = fopen("demo_csv
$title = array("First_Name"
$data_
$data_
$title = implode("
$data_
$data_
$data_str =$title
fwrite($fp
fclose($fp); //關閉文件句柄
echo "生成成功";
}
echo "<br>";
echo "<a >生成csv文件</a>";
?>
也可以做一個封閉函數
封閉函數一
function exportToCsv($csv_data
$csv_terminated = "/n";
$csv_separator = "
$csv_enclosed =
$csv_escaped = "//";
// Gets the data from the database
$schema_insert =
$out =
// Format the data
foreach ($csv_data as $row)
{
$schema_insert =
$fields_cnt = count($row);
//printr($row);
$tmp_str =
foreach($row as $v)
{
$tmp_str
} // end for
$tmp_str = substr($tmp_str
$schema_insert
$out
$out
} // end while
header("Cache
header("Content
header("Content
header("Content
echo $out;
}
/*
$csv_data = array(array(
array_push($csv_data
exportToCsv($csv_data
*/
封閉函數二
<?
/**
* Simple class to properly output CSV data to clients
* in method to do the same for writing to files (fputcsv())
* going right to the client is beneficial
*
* @author Jon Gales
*/
class CSV_Writer {
public $data = array();
public $deliminator;
/**
* Loads data and optionally a deliminator
* of associative arrays
*
* @param array $data
* @param string $deliminator
*/
function __construct($data
{
if (!is_array($data))
{
throw new Exception(
}
$this
$this
}
private function wrap_with_quotes($data)
{
$data = preg_replace(
return sprintf(
}
/**
* Echos the escaped CSV file with chosen delimeter
*
* @return void
*/
public function output()
{
foreach ($this
{
$quoted_data = array_map(array(
echo sprintf("%s/n"
}
}
/**
* Sets proper Content
*
* @param string $name
* @return void
*/
public function headers($name)
{
header(
header("Content
}
}
/*
//$data = array(array("one"
$data[] = array("one"
$data[] = array(
$csv = new CSV_Writer($data);
$csv
$csv
*/
<?php
require_once
$workbook = new Spreadsheet_Excel_Writer();
/* 生成 CSV
$filename = date(
$workbook
*/
// 生成 Excel
$filename = date(
$workbook
$workbook
$workbook
$worksheet =& $workbook
$data[]= array(
$data[] = array(
$total_row = count($data);
$total_col = count($data[
for ($row =
for ($col =
$worksheet
}
}
/*
$worksheet =& $workbook
$data[]= array(
$data[] = array(
$total_row = count($data);
$total_col = count($data[
for ($row =
for ($col =
$worksheet
}
}
*/
$workbook
?>
類二
讀取Excel文件
function Read_Excel_File($ExcelFile
$ExcelFile Excel文件名
$Result 返回的結果
函數返回值 正常返回
返回的值數組
$result[sheet名][行][列] 的值為相應Excel Cell的值
建立Excel文件
function Create_Excel_File($ExcelFile
$ExcelFile Excel文件名
$Data Excel表格數據
請把函數寫在PHP腳本的開頭
例
<?
require "excel_class
Read_Excel_File("Book
for ($i=
{
for ($j=
{
echo $return[Sheet
}
echo "<br>";
}
?>
例
<?
require "excel_class
Read_Excel_File("Book
Create_Excel_File("ddd
?>
From:http://tw.wingwit.com/Article/program/PHP/201311/21008.html