在php中将数组导出到Excel
我在每个子数组的开头有一个数组数组,子数组是列的标题,后跟我要填充列的整数。它看起来像这样:在php中将数组导出到Excel,php,arrays,excel,Php,Arrays,Excel,我在每个子数组的开头有一个数组数组,子数组是列的标题,后跟我要填充列的整数。它看起来像这样: Array ( [0] => Array ( [0] => How was the Food? [1] => 3 [2] => 4 ) [1] => Array ( [0] => How was the first party of the semester? [1] => 2 [2] => 4 [3] => 0 ) ) 有没有办法分解数组并将其导
Array ( [0] => Array ( [0] => How was the Food? [1] => 3 [2] => 4 ) [1] => Array ( [0] => How was the first party of the semester? [1] => 2 [2] => 4 [3] => 0 ) )
有没有办法分解数组并将其导出到Excel?Excel可以直接打开csv文件。。。试一试
$array = Array (
0 => Array (
0 => "How was the Food?",
1 => 3,
2 => 4
),
1 => Array (
0 => "How was the first party of the semester?",
1 => 2,
2 => 4,
3 => 0
)
);
header("Content-Disposition: attachment; filename=\"demo.xls\"");
header("Content-Type: application/vnd.ms-excel;");
header("Pragma: no-cache");
header("Expires: 0");
$out = fopen("php://output", 'w');
foreach ($array as $data)
{
fputcsv($out, $data,"\t");
}
fclose($out);
如果确实要将数组导出到excel,请查看。我编写了这个类来简化phpexcel的导出。它支持xls和xlsx。将数组导出到excel的最佳方法是在此处
$data = array(
'0' => array('Name'=> 'Parvez', 'Status' =>'complete', 'Priority'=>'Low', 'Salary'=>'001'),
'1' => array('Name'=> 'Alam', 'Status' =>'inprogress', 'Priority'=>'Low', 'Salary'=>'111'),
'2' => array('Name'=> 'Sunnay', 'Status' =>'hold', 'Priority'=>'Low', 'Salary'=>'333'),
'3' => array('Name'=> 'Amir', 'Status' =>'pending', 'Priority'=>'Low', 'Salary'=>'444'),
'4' => array('Name'=> 'Amir1', 'Status' =>'pending', 'Priority'=>'Low', 'Salary'=>'777'),
'5' => array('Name'=> 'Amir2', 'Status' =>'pending', 'Priority'=>'Low', 'Salary'=>'777')
);
$filename = time().".xls";
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$filename\"");
ExportFile($data);
function ExportFile($records) {
$heading = false;
if(!empty($records))
foreach($records as $row) {
if(!$heading) {
// display field/column names as a first row
echo implode("\t", array_keys($row)) . "\n";
$heading = true;
}
echo implode("\t", array_values($row)) . "\n";
}
exit;
}
你为什么不想导出到CSV这样的非专有格式?我也可以。Excel将是首选选择您希望从web获得它,还是希望代码在服务器中生成Excel?这很有魅力,非常感谢!!!你刚刚帮我又省了一个不眠之夜如何在excel中加粗一个值?