php 產生excel xls文檔_PHP教程

來源:互聯網
上載者:User
php教程 產生excel xls文檔

方法1 - 使用HTTP頭

至於在MS Word中所述,您需要格式化的HTML / PHP頁面使用Excel友好CSS和標題資訊

添加到您的PHP指令碼。
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment;Filename=document_name.xls");

echo "";
echo "";
echo "";
echo "testdata1 t testdata2 t n ";
echo "";
echo "";
?>

方法2 - 使用COM對象

請注意,在伺服器運行下面必須有MS Excel中所述的代碼安裝。

我們使用一個檔案儲存到臨時目錄第一,作為MS Word的同樣的做法。

//Create new COM object – excel.application
$xl = new COM("excel.application");

//Hide MS Excel application window
$xl->Visible = 0;

//Create new document
$xlBook = $xl->Workbooks->Add();

//Create Sheet 1
$xlBook->Worksheets(1)->Name = "Worksheet 1";
$xlBook->Worksheets(1)->Select;

//Set Width & Height
$xl->ActiveSheet->Range("A1:A1")->ColumnWidth = 10.0;
$xl->ActiveSheet->Range("B1:B1")->ColumnWidth = 13.0;

//Add text
$xl->ActiveSheet->Cells(1,1)->Value = "TEXT";
$xl->ActiveSheet->Cells(1,1)->Font->Bold = True;

//Save document
$filename = tempnam(sys_get_temp_dir(), "excel");
$xlBook->SaveAs($filename);

//Close and quit
unset( $xlBook);
$xl->ActiveWorkBook->Close();
$xl->Quit();
unset( $xl );

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment;Filename=document_name.xls");

// Send file to browser
readfile($filename);
unlink($filename);


http://www.bkjia.com/PHPjc/444977.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444977.htmlTechArticlephp教程 產生excel xls文檔 方法1 - 使用HTTP頭 至於在MS Word中所述,您需要格式化的HTML / PHP頁面使用Excel友好CSS和標題資訊 添加到您的PHP指令碼。...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.