面对PHP Excel导出/导入问题

时间:2013-12-31 16:19:15

标签: php codeigniter

我正在尝试使用PHP(Codeignitor)将记录从db导出到excel,然后使用PHP(Codignitor)再次导入相同的excel文件

导出代码IS:

header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename=' . date('d-m-Y') . '.xls');

$export .= '<table border="1">';
$export .= '<tr>';
$export .= '<td>Gate Name</td>';
$export .= '<td>ID</td>';
$export .= '<td>Name</td>';
$export .= '<td>Rank</td>';
$export .= '<td>Time</td>';
$export .= '<td>Entry Type</td>';
$export .= '<td>Work Place</td>';
$export .= '<td>Car Type</td>';
$export .= '<td>Car Number</td>';
$export .= '<td>Inspector Name</td>';
$export .= '</tr>';
foreach ($result as $row) {
    $export .= '<tr>';
    $export .= '<td>' . $row->gate_name . '</td>';
    $export .= '<td>' . $row->soldier_id . '</td>';
    $export .= '<td>' . $row->soldier_name . '</td>';
    $export .= '<td>' . $row->soldier_rank . '</td>';
    $export .= '<td>' . $row->time . '</td>';
    $export .= '<td>' . $row->entry_type . '</td>';
    $export .= '<td>' . $row->work_place . '</td>';
    $export .= '<td>' . $row->vehicle_type . '</td>';
    $export .= '<td>' . $row->vehicle_number . '</td>';
    $export .= '<td>' . $row->inspector_name . '</td>';
    $export .= '</tr>';
}
$export .= '</table>';
echo $export;

当我尝试使用This PHP Library - SimpleXLSX安排下载的文件时。

$SimpleXLSX = new SimpleXLSX($filename);
$data = $SimpleXLSX->rows();

$ data为空

如果我自己使用MS Excel创建一个excel文件,

这个库可以工作。 如果我尝试使用

获取文件内容
print_r(file_get_contents($filename));

使用HTML代码

返回内容

<table border="1">
            <tr>
                <td>Gate Name</td>
                <td>ID</td>
                <td>Name</td>
                <td>Rank</td>
                <td>Time</td>
                <td>Entry Type</td>
                <td>Work Place</td>
                <td>Car Type</td>
                <td>Car Number</td>
                <td>Inspector Name</td>
            </tr>
            <tr>
                <td>Gate 2</td>
                <td>1111</td>
                <td>فارس الغامدي</td>
                <td>ملازم اول</td>
                <td>28/02/1435 07:01:00</td>
                <td>دخول</td>
                <td>قطاع رفحاء</td>
                <td>جيب</td>
                <td>Tuy 124</td>
                <td>عبدالله</td>
            </tr>
        </table>

我认为问题在于创建Excel文件时使用header()函数或编码。 请帮我。 谢谢。

1 个答案:

答案 0 :(得分:1)

SimpleXLSX将读取XLSX文件并打印HTML代码,但您无法使用SimpleXLSX将HTML代码转换为XLSX格式。以下是如何在PHPExcel中执行此操作的示例:

<?php


$date = date("m-d-Y-g-i-s");

// Include and instantiate PHPExcel object.
include '../lib/PHPExcel/PHPExcel.php';
include '../lib/PHPExcel/PHPExcel/Writer/Excel2007.php';

$objPHPExcel = new PHPExcel();

// Set metadata.
$objPHPExcel->getProperties()->setTitle("Price List $date");
$objPHPExcel->getProperties()->setSubject("$date Price List");
$objPHPExcel->getProperties()->setDescription("The active price list for $date.");

// Set active sheet index at zero.
$objPHPExcel->setActiveSheetIndex(0);

// Determine limit and offset.
if($_GET['limit'] > 0 && $_GET['offset'] > 0) {
    $limit = $_GET['limit'];
    $offset = $_GET['offset'];
} else {
    $limit = 10000;
    $offset = 0;
}

// Query the database.
$query = mysql_query("SELECT * FROM `items` LIMIT $offset, $limit") or die(mysql_error());
$c = 0; // Initialize index at zero.

// Set column dimensions.
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);

// Loop through data and insert it into their respective columns.
while($row = mysql_fetch_array($query)) {
    $c++;
    $objPHPExcel->getActiveSheet()->getCell("A$c")->setValueExplicit(stripslashes($row[strStock]), PHPExcel_Cell_DataType::TYPE_STRING);
    $objPHPExcel->getActiveSheet()->SetCellValue("B$c", stripslashes(html_entity_decode($row[strDescription])));
    $objPHPExcel->getActiveSheet()->SetCellValue("C$c", stripslashes($row[curPrice]));
}

$objPHPExcel->setActiveSheetIndex(0);
// Send headers to download XLSX file.
header('Content-Type: application/vnd.ms-excel');

if(isset($_GET[part]) && isset($_GET[of])) {
    header('Content-Disposition: attachment;filename="Price List (Part '.$_GET[part].' of '.$_GET[of].') '.$date.'.xlsx"');
}

header('Cache-Control: max-age=0');

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

?>

编辑:对不起,这是PHPExcel的代码,而不是SimpleXLSX。 Here是PHPExcel的文档。