方法1 - 使用HTTP头
至于在MS Word中所述,您需要格式化的HTML / PHP页面使用Excel友好CSS和标头信息添加到您的PHP脚本,实例代码如下:
- <?php
- header("Content-type: application/vnd.ms-excel");
- header("Content-Disposition: attachment;Filename=document_name.xls");
-
- echo "<html>";
- echo "<meta http-equiv="Content-Type" content="text/html;
-
- charset=Windows-1252">";
- echo "<body>";
- echo "<b>testdata1</b> <u>testdata2</u> ";
- echo "</body>";
- echo "</html>";
- ?>
方法2 - 使用COM对象
请注意,在服务器运行下面必须有MS Excel中所述的代码安装,我们使用一个文件保存到临时目录第一,作为MS Word的同样的做法,实例代码如下:
-
- $xl = new COM("excel.application");
-
-
- $xl->Visible = 0;
-
-
- $xlBook = $xl->Workbooks->Add();
-
-
- $xlBook->Worksheets(1)->Name = "Worksheet 1";
- $xlBook->Worksheets(1)->Select;
-
-
- $xl->ActiveSheet->Range("A1:A1")->ColumnWidth = 10.0;
- $xl->ActiveSheet->Range("B1:B1")->ColumnWidth = 13.0;
-
-
- $xl->ActiveSheet->Cells(1,1)->Value = "TEXT";
- $xl->ActiveSheet->Cells(1,1)->Font->Bold = True;
-
-
- $filename = tempnam(sys_get_temp_dir(), "excel");
- $xlBook->SaveAs($filename);
-
-
- unset( $xlBook);
- $xl->ActiveWorkBook->Close();
- $xl->Quit();
- unset( $xl );
-
- header("Content-type: application/vnd.ms-excel");
- header("Content-Disposition: attachment;Filename=document_name.xls");
-
-
- readfile($filename);
- unlink($filename);
|