如何格式化excel文件与样式,字体,颜色,表等等与纯PHP? [英] How to format excel file with styles, fonts, colors, tables etc with pure PHP?

查看:105
本文介绍了如何格式化excel文件与样式,字体,颜色,表等等与纯PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种通过Pure PHP编写格式为excel的文件的方法。
警告:请,这个Q不是关于libs,插件等等 - 不要打扰。
我已经google了很多,没有找到答案,但在一些文章StackOverflow,有人提到的HTML / CSS格式的方式
没有工作的HTML格式解决方案
,我试图这样做,但在文件保存的列,其中填充了标签,什么也没有发生。
可能有一些专业人士可以帮助我通过PHP,HTML,CSS或其他方式编写Excel的纯代码。

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 work and 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.

再次举个例子've已经使用的概念,像这样没有帮助:

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"); 

我不仅使用了divs和表格 - 它没有工作。

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

推荐答案

以下示例可能会帮助您:

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>

将其另存为.php文件。

Save this as a .php file on your server.

它的作用是:添加标题以强制浏览器传递一个Excel文件。然后在里面返回一个表。我的Excel完全选择这个。

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.

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

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

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

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,也就是使用< table> 开始,对每行使用< tr> < / tr> ,并将字段放入< td> 标签,然后以结束< / table> 结束,因此基本上:

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:

$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"); 

如果这样做,我建议您将输出更改为< th> 标记,而不是您范例中的div。

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.

这篇关于如何格式化excel文件与样式,字体,颜色,表等等与纯PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆