午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

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

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

    現(xiàn)在采用<cell>的形式進(jìn)行輸出。類來源于網(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);//這里用來替換了非字母數(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 ('空格','','空格測試'),//第一行數(shù)據(jù)

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

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

    );

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

    $xls->addArray($data);

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

    運行試試,應(yīng)該沒有錯,我已經(jīng)做過測試

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

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

    2025國考·省考課程試聽報名

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