简体   繁体   English

从PHP导出到Excel

[英]Export from PHP to Excel

I need to export data from php to Excel, and be able to format various items in the excel spreadsheet. 我需要将数据从php导出到Excel,并能够格式化excel电子表格中的各种项目。

So far, the best library that I have found is PHPExcel . 到目前为止,我找到的最好的库是PHPExcel However, it seems very heavy, and somewhat slow. 但是,它看起来很重,而且有点慢。 Granted, it is very powerful. 当然,它非常强大。

Is there anything a bit more lightweight and faster, that allows me to export to excel and be able to apply simple formatting (bold, alignment, borders)? 有没有更轻量级和更快的东西,这允许我导出到Excel并能够应用简单的格式(粗体,对齐,边框)?

I just got done with this yesterday. 我昨天刚刚完成了这件事。 Using PHPExcel, I had no problems reading in a "master" document with formatting, writing 20-100 rows of content, and saving off the file (I save it "to screen" for immediate download. While some people on the forums complained about speed and overhead, I'm pushing a lot of data its way and it doesn't have any problem at all doing what it advertises. 使用PHPExcel,我在使用格式化的“主”文档中读取没有任何问题,写入20-100行内容,并保存文件(我将其保存到“屏幕”以便立即下载。虽然论坛上的一些人抱怨速度和开销,我正在推动大量的数据,并没有任何问题做它宣传的东西。

Note that somewhere I read to do styling in series as opposed to in loops when possible. 请注意,我读到的某个地方是为了在可能的情况下在循环中进行样式设置。 For example, style a1:a50 as opposed to style->a1, style->a2 in a loop. 例如,样式a1:a50而不是样式style->a1, style->a2 ,样式style->a1, style->a2在循环中。 Apparently, the two different scenarios have very different memory implications. 显然,这两种不同的场景具有非常不同的内存含义。

The only gotcha I found was a few quirks between outputting and reading Excel 2003 files. 我发现的唯一问题是输出和读取Excel 2003文件之间的一些怪癖。 If you're working entirely in XLSX files, it should function exactly as documented. 如果您完全使用XLSX文件,它应该完全按照记录的方式运行。

Here is simple Excel file generation function, very fast and exactly .xls file. 这是简单的Excel文件生成功能,非常快速且完全.xls文件。

$filename = "sample_php_excel.xls";
$data = array(
array("User Name" => "Abid Ali", "Q1" => "$32055", "Q2" => "$31067", "Q3" => 32045, "Q4" => 39043),
array("User Name" => "Sajid Ali", "Q1" => "$25080", "Q2" => "$20677", "Q3" => 32025, "Q4" => 34010),
array("User Name" => "Wajid Ali", "Q1" => "$93067", "Q2" => "$98075", "Q3" => 95404, "Q4" => 102055),
);
to_xls($data, $filename);

function to_xls($data, $filename){
$fp = fopen($filename, "w+");
$str = pack(str_repeat("s", 6), 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); // s | v
fwrite($fp, $str);
if (is_array($data) && !empty($data)){
    $row = 0;
    foreach (array_values($data) as $_data){
        if (is_array($_data) && !empty($_data)){
            if ($row == 0){
                foreach (array_keys($_data) as $col => $val){
                    _xlsWriteCell($row, $col, $val, $fp);
                }
                $row++;
            }
            foreach (array_values($_data) as $col => $val){
                _xlsWriteCell($row, $col, $val, $fp);
            }
            $row++;
        }
    }
}
$str = pack(str_repeat("s", 2), 0x0A, 0x00);
fwrite($fp, $str);
fclose($fp);
}

function _xlsWriteCell($row, $col, $val, $fp){
if (is_float($val) || is_int($val)){
    $str  = pack(str_repeat("s", 5), 0x203, 14, $row, $col, 0x0);
    $str .= pack("d", $val);
} else {
    $l    = strlen($val);
    $str  = pack(str_repeat("s", 6), 0x204, 8 + $l, $row, $col, 0x0, $l);
    $str .= $val;
}
fwrite($fp, $str);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM