【问题标题】:PHPExcel: Download the Excel file on the client sidePHPExcel:在客户端下载Excel文件
【发布时间】:2013-11-02 19:35:59
【问题描述】:

问题已解决:对于可能有此问题的其他用户 - 请注意 PHP 文件的编码。如果您使用 PHPExcel,它必须是 ANSII 编码而不是 UTF8,否则 EXCEL 将被损坏。添加的标头(答案 1)在我更改文件本身的编码后解决了问题。

我正在使用 PHPExcel 从 MYSQL DB 中的表创建 EXCEL,以便用户可以将其下载到他的计算机上。

下面的代码创建了一个正确的 Excel 文件,但问题是它已下载到我的服务器。我在 PHPExcel 手册中读到我需要添加标题:

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$name.'.xls"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');

但如果我这样做,下载的文件是: 1.里面有一些jibrish 2.说Excel文件不好,需要修复。 问题是这个文件保存为 UTF8,如果我将它编码为 ANSI,那么它可以正常工作,但当然这是手动更改,我需要一个正常工作的 excel 才能接触到用户。

什么是错误?

我的代码有效(但将文件下载到服务器):

<?php
include 'connection.php';
include 'checkUser.php';

//Getting all the needed information from the DB
$task_id=$_GET['id'];
$query2 = "SELECT * FROM projects WHERE ProjectID=$task_id";
$data2 = mysqli_query($con, $query2);
$row = mysqli_fetch_array($data2);
$project_type = $row['ProjectType'];
$project_name = $row['ProjectName'];

switch ($project_type){
                    case 2: $NumberOfRows=22;  $project = "slivedetails"; break;
                    case 3: $NumberOfRows=30;  $project = "plivedetails"; break;
                    default: $NumberOfRows=0; $project = "none";
                    }
//column names
if ($project="slivedetails"){
    $ColumnNames = mysqli_query($con,"SHOW COLUMNS FROM slivedetails") or die("mysql error"); 
    }
else if ($project="plivedetails"){
    $ColumnNames = mysqli_query($con, "SHOW COLUMNS FROM plivedetails") or die("mysql error"); 
    }

$query = "SELECT * FROM $project WHERE TaskID=$task_id";
$data = mysqli_query($con, $query);
$num_rows = mysqli_num_rows($data); 


/** Include PHPExcel */
require_once 'PHPExcel_1.7.9_doc/Classes/PHPExcel.php';
require_once 'PHPExcel_1.7.9_doc/Classes/PHPExcel/IOFactory.php';


// create new PHPExcel object
$objPHPExcel = new PHPExcel();
    $objPHPExcel = new PHPExcel();

// writer already created the first sheet for us, let's get it
$objSheet = $objPHPExcel->getActiveSheet();
// rename the sheet
$objSheet->setTitle('Task Results');

// let's bold and size the header font and write the header
// as you can see, we can specify a range of cells, like here: cells from A1 to A4
$objSheet->getStyle('A1:AD1')->getFont()->setBold(true)->setSize(12);

$char = 65;
// write header]
for ($i=1;$i<=$NumberOfRows;$i++){
    $col_name = mysqli_fetch_array($ColumnNames);
    $objSheet->getCell(chr($char).'1')->setValue($col_name['Field']);
    $char++;
}

// Now we need to get the data from the DB. While we have a row in the result:
$rowIterator=2; //our row number. We begin from 2 because the first one is the title.

while ($RowInfo = mysqli_fetch_array($data)){
//We will fill the information based on the amount of columns:
$char = 65; //we set the first char as column A
for ($i=0;$i<$NumberOfRows;$i++){
    $objSheet->getCell(chr($char).$rowIterator)->setValue($RowInfo[$i]);
    $char++;
}
$rowIterator++;
}

// autosize the columns
$char = 65;
for ($i=1;$i<=$NumberOfRows;$i++){
    $objSheet->getColumnDimension(chr($char))->setAutoSize(true);
    $char++;
}

// create the writer
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
$objWriter->save('results.xlsx');


?>

【问题讨论】:

  • 天哪,你救了我的命。由于无限的谷歌搜索,我已经头疼了,但多亏了你,我才没有发疯。

标签: php phpexcel


【解决方案1】:

这应该可以,尝试像这样修改您的代码:

header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=my_excel_filename.xls");
header("Pragma: no-cache");
header("Expires: 0");

flush();

require_once 'PHPExcel.php';

$objPHPExcel = new PHPExcel();

// here fill data to your Excel sheet

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

$objWriter->save('php://output');

【讨论】:

  • 我现在试试。什么是闪存?
  • 不,不起作用。 excel文件中仍然有很多奇怪的符号+我的excel(2010)警告文件格式奇怪。下载到我的comp(结果)的文件工作正常:(
  • 问题是这个文件保存为 UTF8,如果我将它编码为 ANSI,那么它可以正常工作,但当然这是手动更改,我需要一个正常工作的 excel 才能接触到用户。
【解决方案2】:

删除以下包含:

require_once 'PHPExcel_1.7.9_doc/Classes/PHPExcel/IOFactory.php';

您两次声明了该对象。删除其中一个:

// create new PHPExcel object
$objPHPExcel = new PHPExcel();

在创建 Writer 之前插入以下标头:

header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=\"results.xlsx\"");
header("Cache-Control: max-age=0");

而不是以下(实际上将文件保存在服务器中):

$objWriter->save('results.xlsx');

插入以下内容(这将创建可下载的文件):

$objWriter->save("php://output");

这应该可以解决乱码文本。如果您仍然收到此类文本,请在最后一行之前插入以下代码 ($objWriter-&gt;save("php://output");):

ob_clean();

这对我有用。希望对您有所帮助。

【讨论】:

    【解决方案3】:

    亚历克斯,我遇到了同样的问题。我做了所有这些,但结果还是一样。我刚变 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

    并将所有.xlsx 更改为.xls

    希望它会有所帮助。

    【讨论】:

      【解决方案4】:
      1. 从 github 下载 PHPExcel-1.8 并将其替换为旧的 PHPExcel。
      2. 将 PHPExcel-1.8 重命名为 PHPExcel。在我的代码中,我包含了require '../library/excel/PHPExcel.php';
      3. 我删除了这个文件(PHPExcel.php)的所有内容,只添加了一行代码 require 'PHPExcel/Classes/PHPExcel.php';

      (注:我上传的PHPExcel是在文件夹library/excel中) 完美运行

      【讨论】:

        猜你喜欢
        • 2023-04-01
        • 1970-01-01
        • 2018-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多