使用PHP中的超链接创建Excel文件

时间:2010-03-24 06:50:17

标签: php excel

我有以下方法从PHP开始创建Excel文件,从二维数组开始:

header("Content-Disposition: attachment; filename=ExcelFile.xls");
header("Content-Type: application/vnd.ms-excel"); 



$Data[] = array('field1', 'field2', 'field3');
$Data[] = array('field1', 'field2', 'field3');
$Data[] = array('field1', 'field2', 'field3');

foreach($Data as $key => $val) {
    echo implode("\t", array_values($val)) . "\n"; 
}

是否可以在此文件中包含指向网页的超链接?

1 个答案:

答案 0 :(得分:0)

除了基本的.csv输出方法之外,对于任何有用的单元格操作,您可能需要考虑PHP COM。

这是一个非常粗略的例子,它从this link大量借用,只是为了给你一个它的样子的要点:

//create instance of excel on the server
$excel = new COM("excel.application") or die("Unable to create excel object");

//add a book and a worksheet and activate the worksheet
$workbook = $excel->Workbooks->Add();
$worksheet=$workbook->Worksheets(1);
$worksheet->activate;

//pick an active cell (here it's A1) and add text plus a hyperlink
$cell=$worksheet->Cells(1,1);
$cell->Activate;
$cell->value = 'your hyperlink text';

//i took this syntax from my native MS VBA, so check syntax
$cell->Hyperlinks.Add.ActiveCell, 'http://yourdomain.com'

//save your doc
$worksheet->SaveAs([[your path]]);

//close everything up and free up resources
$worksheet->Close(false);
$excel->Workbooks->Close();
unset($sheet);
$excel->Quit();
$excel = null;

结果可能会有所不同,具体取决于有关此主题的所有错误报告。