如何使用纯 PHP 格式化带有样式、字体、颜色、表格等的 excel 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6620398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:52:58  来源:igfitidea点击:

How to format excel file with styles, fonts, colors, tables etc with pure PHP?

phphtmlcssexcel

提问by Arthur Kushman

I need a way to write an excel file with format through Pure PHP. WARNING:Please, this Q not about the libs, plugins and so on- do not disturb. I've googled a lot and didn`t find the Answer, but in some article on StackOverflow, some guy mentioned the HTML/CSS format way HTML format solutions that didn't workand I've tried to do this, but in file which was saved columns where filled with tags and nothing happens. May be there are some professionals who can help me with pure code writing an Excel through PHP, HTML, CSS or something.

我需要一种通过纯 PHP 编写具有格式的 excel 文件的方法。警告:请注意,此 Q 与库、插件等无关-请勿打扰。我在谷歌上搜索了很多并没有找到答案,但是在 StackOverflow 上的一些文章中,有人提到了 HTML/CSS 格式方式 HTML 格式解决方案不起作用,我尝试这样做,但是在文件中这是保存的列,其中填充了标签,但没有任何反应。可能有一些专业人士可以帮助我通过 PHP、HTML、CSS 或其他方式编写 Excel 的纯代码。

And again with example, I've already used concept like this wich didn't help:

再举个例子,我已经使用了这样的概念,但没有帮助:

    // headers for excel
$headers .= "<div style='font:bold 14px Times;'>Журнал остатков и инкассаций</div>\n";
$headers .= "Начало периода\t ".date('d.m.Y')."0:00 \n";
$headers .= "Начало периода\t ".date('d.m.Y')."23:59 \n\n";

$headers .= "Терминал\t";
$headers .= "Место расположения\t";
$headers .= "Дата\t";
$headers .= "Время\t";
$headers .= "Сумма инкассации\t";
$headers .= "Банкноты\t";

// excel content with overloaded terminals
if (mssql_num_rows($resCollection)>0) {
  while ($rowCollection = mssql_fetch_object($resCollection)) {

    $data .= $rowCollection->Name."\t".$rowCollection->Address."\t"
            .$rowCollection->TerminalNotes."\t".$rowCollection->TerminalAmount
            ."\t".$rowCollection->DayAmountAll."\t"
            .$rowCollection->LastPaymentTime."\n";

  }
}

$fileName = 'collection'.date('d_m_Y_H_i_s').'.xls';


header("Content-type: application/vnd.ms-excel");// application/excel had also been used
header("Content-Disposition: attachment; filename=$fileName");
echo iconv('utf-8', 'cp1251', "$headers\n$data"); 

I've used not only divs and tables too - it didn't work though.

我不仅使用了 div 和表格 - 但它没有用。

回答by Eljakim

The following example would probably help you:

以下示例可能会对您有所帮助:

  1 <?php
  2 header('Content-type: application/excel');                                  
  3 header('Content-Disposition: attachment; filename="test.xls"');
  4 ?>
  5 
  6 <table>
  7 <tr><th>Column 1</th><th>Column 2</th></tr>
  8 <tr><td style="font-size:200%">Answer 1</td><td style="color:#f00">Answer 2<    /td></tr>
  9 <tr><td colspan="2" style="font-weight:bold">Answer 3 with 2 columns</td></t    r>
 10 </table>

Save this as a .php file on your server.

在您的服务器上将其另存为 .php 文件。

What it does is: add headers to force the browser thing you're delivering an Excel file. And then returning a table inside of it. My Excel picks this up perfectly.

它的作用是:添加标题以强制浏览器提供 Excel 文件。然后返回其中的一张桌子。我的 Excel 完美地解决了这个问题。

Note: the sample HTML comes from the answer that you had already found. I have just added the headers to the beginning.

注意:示例 HTML 来自您已经找到的答案。我刚刚将标题添加到开头。

Note: You just changed your question and added an example. Your example does NOT use html! You are delivering a TAB-delimited file where each line ends with a newline.

注意:您刚刚更改了问题并添加了示例。您的示例不使用 html!您正在提供一个制表符分隔的文件,其中每一行都以换行符结尾。

The proper way to do this is to really generate HTML, ie, use a <table>at the beginning, use <tr>and </tr>for each row, and put the fields in <td>tags, and then finish with the closing </table>, so basically:

正确的方法是真正生成HTML,即<table>在开头使用a ,对每一行使用<tr>and </tr>,并将字段放在<td>标签中,然后以结束结束</table>,所以基本上:

$data='<table>';
// excel content with overloaded terminals
if (mssql_num_rows($resCollection)>0) {
  while ($rowCollection = mssql_fetch_object($resCollection)) {
    $data.='<tr>';
    $data .= '<td>'.$rowCollection->Name."</td><td>".$rowCollection->Address."</td><td>"
            .$rowCollection->TerminalNotes."</td><td>".$rowCollection->TerminalAmount
            ."</td><td>".$rowCollection->DayAmountAll."</td><td>"
            .$rowCollection->LastPaymentTime."</td>";
    $data.='</tr>';

  }
}
$data.='</table>';

Then change your last line to:

然后将最后一行更改为:

echo iconv('utf-8', 'cp1251', "$data"); 

If that works I suggest you change the output to have <th>tags around your table headers, instead of the div that is in your example.

如果可行,我建议您更改输出以<th>在表格标题周围添加标签,而不是示例中的 div。