2011-09-28 181 views
0

我正试图将数组数组导出到excel。我将它设置为一个头变量和一个数据变量,该变量基本上构建了要在导出中执行的巨大字符串。但是,只有标题变量正在经历。让我告诉一些代码:将数组导出到Excel

这是设置参数:

str_replace(" ", "_", $getData['name']); 
    $filename = $getData['name']."_summary.xls"; 
    header("Content-Type: application/x-msdownload"); 
    header("Content-Disposition: attachment; filename=\"$filename\""); 
    header("Pragma: no-cache"); 
    header("Expires: 0"); 

都到一个函数来获取信息:

foreach($tempRS as $key=>$value) 
{ 
    foreach($value as $iKey=>$iValue) 
    { 
     if($count == 6) 
     { 
      $iValue = str_replace('"', '""', $iValue); 
      $iValue = '"'.$iValue.'"'."\n"; 
      $data .= trim($iValue); 
      $count = 0; 
     } 
     else 
     { 
      $iValue = str_replace('"', '""', $iValue); 
      $iValue = '"'.$iValue.'"'."\t"; 
      $data .= trim($iValue); 
      $count++; 
     } 
    } 

} 
$header = "ROW HEADER 1\tROW HEADER 2\tROW HEADER 3\tROW HEADER 4\tROW HEADER 5\tROW HEADER 6\n"; 
print "$header\n$data"; 

我似乎无法找出原因我在输出中丢失$ data变量。

+0

这里被定义$的数据? $数据是否有可能超出范围? – superultranova

+0

我在foreach循环之后回显了数据,以确保它包含我希望它包含的数据。否则,我会在进入循环之前清除$ data,之后不做任何处理。 – IceBlueFire

+0

那么你可以回显数据?这不能解决你的问题吗?或者只有在设置标题时才会出现问题? – superultranova

回答

2

为什么不只是fputcsv()为您生成CSV数据?或者更好的做法是,您可以使用PHPExcel输出原生.xls/.xlsx文件,并在生成的电子表格中实际使用格式和公式,而不是将.csv伪装成Excel文件。

1

首先,使用echo而不是print。打印导致开销的负载,因为它既返回并回显数据。

其次,不要把变量引号内,使用

echo $header ."\n".$data; 

为了得到你的问题,并在foreach循环实际上循环?如果包含任何数据,您是否检查过$数据?

一个更好的解决方案可能是这样的:

$header = ''; 
echo $header; 

foreach() loop here { 
    //echo the data here instead of putting it in a variable 
} 

或者,也许更好,使用http://nl.php.net/fputcsv

+0

是的,我追溯到确保$ data变量实际上持有我想要的。它只是不被放入Excel中,这是我遇到的问题。 – IceBlueFire

+1

*“打印导致开销的负载”* - LOL。 – hakre

+0

此外,改变回显和分离出你的回声,仍然不起作用。 – IceBlueFire

0

我测试你的代码,看来你trim()方法修剪您\n\t

如果你删除它,你的代码应该没问题。

这里是我的代码,导出excel中的产品数组。

此代码只有一个小问题:Excel不想打开它,因为格式问题,但如果在提示错误消息时单击“是”,那么您会没事的...

用开放式办公室虽然

上的修复工作没问题...

$filename = "products_exports " . date("Y-m-d H_i_s") . ".xls"; 

/** 
* Tell the browser to download an excel file. 
*/ 

header("Content-Type: application/x-msdownload; charset=utf-8"); 
header("Content-Disposition: attachment; filename=\"$filename\""); 
header("Pragma: no-cache"); 
header("Expires: 0"); 


/** 
* The header of the table 
* @var string 
*/ 
$header = ""; 
/** 
* All the data of the table in a formatted string 
* @var string 
*/ 
$data = ""; 

//We fill in the header 
foreach ($productArray as $i => $product) { 
    $count = 0; 
    foreach ($product as $key => $value) { 

     if($count == count((array)new Product("")) - 1){ 
      $header.= $key; 
     } 
     else{ 
      $header.= $key . "\t"; 
      $count++; 
     } 
    } 

    break; /*One loop is enough to get the header !*/ 
} 


//And then the data by the product and all the categories 

/*Where $productArray = This can be your custom array of object. eg. 
    array[0] = new YouCustomObject(your params...); 
*/ 
foreach ($productArray as $i => $product) { 
    $count = 0; 
    foreach ($product as $key => $value) { 
     if($count == count((array)new Product("")) - 1){ 
      $value = str_replace('"', '""', $value); 
      $value = '"' . $value . '"' . "\n"; 
      $data .= $value; 
      $count = 0; 
     } 
     else{ 
      $value = str_replace('"', '""', $value); 
      $value = '"' . $value . '"' . "\t"; 
      $data .= $value; 
      $count++; 
     } 
    } 
} 

echo $header ."\n". $data;