php輸出excel php生成excel
來(lái)源:易賢網(wǎng) 閱讀:1338 次 日期:2014-09-10 11:53:00
溫馨提示:易賢網(wǎng)小編為您整理了“php輸出excel php生成excel”,方便廣大網(wǎng)友查閱!

php輸出excel,在本站之前采用table的形式,以header代碼打開網(wǎng)頁(yè)為excel進(jìn)行保存,但是在header方法中生成的excel不是標(biāo)準(zhǔn)的,有的時(shí)候不能被微軟的excel識(shí)別,傳送也不好傳送。

現(xiàn)在采用<cell>的形式進(jìn)行輸出。類來(lái)源于網(wǎng)絡(luò),做了部分解釋

class Excel_XML

{

private $header = "<?xml version=\"1.0\" encoding=\"%s\"?\>\n<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">";

private $footer = "</Workbook>";

private $lines = array();

private $sEncoding;

private $bConvertTypes;

private $sWorksheetTitle;

public function __construct($sEncoding = 'UTF-8', $bConvertTypes = false, $sWorksheetTitle = 'Table1')

{

$this->bConvertTypes = $bConvertTypes;

$this->setEncoding($sEncoding);

$this->setWorksheetTitle($sWorksheetTitle);

}

public function setEncoding($sEncoding)

{

$this->sEncoding = $sEncoding;

}

public function setWorksheetTitle ($title)

{

// $title = preg_replace ("/[\\\|:|\/|\?|\*|\[|\]]/", "", $title);//如果有特殊字符則表示替換

$title = substr ($title, 0, 31);

$this->sWorksheetTitle = $title;

}

private function addRow ($array)

{

$cells = "";

foreach ($array as $k => $v):

$type = 'String';

if ($this->bConvertTypes === true && is_numeric($v)):

$type = ‘Number’;

endif;

$v = htmlentities($v, ENT_COMPAT, $this->sEncoding);

$cells .= "<Cell><Data ss:Type=\"$type\">" . $v . "</Data></Cell>\n";

endforeach;

$this->lines[] = "<Row>\n" . $cells . "</Row>\n";

}

public function addArray ($array)

{

foreach ($array as $k => $v)

$this->addRow ($v);

}

public function generateXML ($filename = 'excel-export')

{

// correct/validate filename

// $filename = preg_replace('/[^aA-zZ0-9\_\-]/', '', $filename);//這里用來(lái)替換了非字母數(shù)字為空

// deliver header (as recommended in php manual)

header("Content-Type: application/vnd.ms-excel; charset=" . $this->sEncoding);

header("Content-Disposition: inline; filename=\"" . $filename . ".xls\"");

// print out document to the browser

// need to use stripslashes for the damn ">"

echo stripslashes (sprintf($this->header, $this->sEncoding));

echo "\n<Worksheet ss:Name=\"" . $this->sWorksheetTitle . "\">\n<Table>\n";

foreach ($this->lines as $line)

echo $line;

echo "</Table>\n</Worksheet>\n";

echo $this->footer;

}

}

生成代碼:

include("excelclass.php");

$data = array(

array ('空格','','空格測(cè)試'),//第一行數(shù)據(jù)

array('Schwarz', '中文測(cè)試'),//第二行數(shù)據(jù)

array('√', '特殊字符測(cè)試')

);

$xls = new Excel_XML('UTF-8', false, 'php輸出Excel第一個(gè)sheet的名稱');

$xls->addArray($data);

$xls->generateXML('php生成Excel的名稱');

運(yùn)行試試,應(yīng)該沒有錯(cuò),我已經(jīng)做過(guò)測(cè)試

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:php輸出excel php生成excel
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)