通过 PHP 格式化 Excel 文件中的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/279917/
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-24 22:13:02  来源:igfitidea点击:

Format text in Excel file via PHP

phpexport-to-excel

提问by Pamela

I'm loading data from my database, and exporting to an Excel file via a method I found on this site: http://www.appservnetwork.com/modules.php?name=News&file=article&sid=8

我正在从我的数据库加载数据,并通过我在本网站上找到的方法导出到 Excel 文件:http: //www.appservnetwork.com/modules.php?name=News&file=article& sid=8

It works, but what I want to do now is format the text before it exports - change the font and text size. Does anybody have any ideas on how to do this?

它有效,但我现在想要做的是在导出之前格式化文本 - 更改字体和文本大小。有没有人对如何做到这一点有任何想法?

回答by Bill Karwin

I have written Excel spreadsheets from PHP 5 using the PEAR :: Package :: Spreadsheet_Excel_Writerclasses. You can do a lot with this package.

我已经使用这些PEAR :: Package :: Spreadsheet_Excel_Writer类从 PHP 5 编写了 Excel 电子表格。你可以用这个包做很多事情。

回答by Eric Caron

Depending on the speed in which you wish to deploy your solution, one method is to just use the HTML table tag, store all your data in tables using style markup, and then use PHP header's option to force the browser to save is as a .xls file.

根据您希望部署解决方案的速度,一种方法是仅使用 HTML 表格标记,使用样式标记将所有数据存储在表格中,然后使用 PHP 标头的选项强制浏览器将文件保存为 . .xls 文件。

For proof of concept, copy this code into notepad, save as .xls, and then open with Excel:

为了验证概念,将此代码复制到记事本中,另存为 .xls,然后用 Excel 打开:

<table>
<tr><th>Column 1</th><th>Column 2</th></tr>
<tr><td style="font-size:200%">Answer 1</td><td style="color:#f00">Answer 2</td></tr>
<tr><td colspan="2" style="font-weight:bold">Answer 3 with 2 columns</td></tr>
</table>

Its not the most elegant solution, but it will absolutely suit your needs.

它不是最优雅的解决方案,但绝对适合您的需求。

回答by Peter Howe

PHPExcelprovides you with the facilities to do this and more.

PHPExcel为您提供了执行此操作及更多功能的工具。

Edit: This project is now deprecated.

编辑:该项目现已弃用。

回答by Peter Howe

I've used php_writeexcela few times, and liked it a lot! :)

我用过php_writeexcel几次,非常喜欢它!:)

回答by raj

<?php
    include '../include/config.php';
    $sql                        =   "SELECT SQL_CALC_FOUND_ROWS
                                    ".DB_TBL_TUTORS_RECAPS.".id,
                                    ".DB_TBL_TUTORS_RECAPS.".date_of_tutoring_session,
                                    ".DB_TBL_TUTORS_RECAPS.".created,
                                    ".DB_TBL_TUTORS_RECAPS.".hours_tutored,
                                    ".DB_TBL_TUTORS_RECAPS.".billable_travel,
                                    ".DB_TBL_TUTORS_RECAPS.".billable_additional,
                                    ".DB_TBL_TUTORS_RECAPS.".type_of_tutoring,
                                    ".DB_TBL_TUTORS_RECAPS.".additional_comments,
                                    ".DB_TBL_TUTORS_RECAPS.".total_billable,
                                    ".DB_TBL_TUTORS_RECAPS.".materials_covered_during_session,
                                    ".DB_TBL_TUTORS_RECAPS.".next_scheduled_session_location,
                                    ".DB_TBL_TUTORS_RECAPS.".rate,
                                    ".DB_TBL_TUTORS_RECAPS.".tutor_pay_rate,
                                    ".DB_TBL_APPLICANTS.".first_name as tutor_first_name,
                                    ".DB_TBL_APPLICANTS.".last_name as tutor_last_name,
                                    ".DB_TBL_PIPELINE.".tutor_match_notes,
                                    ".DB_TBL_PIPELINE.".date_of_submission,
                                    ".DB_TBL_PIPELINE.".tutor_name,
                                    ".DB_TBL_PIPELINE.".tutor_id,
                                    ".DB_TBL_CLIENTS.".id as client_id,
                                    ".DB_TBL_CLIENTS.".first_name,
                                    ".DB_TBL_CLIENTS.".last_name,
                                    ".DB_TBL_CLIENTS.".location_name,
                                    ".DB_TBL_CLIENTS.".last_name,
                                    ".DB_TBL_CHILDREN.".id as child_id,
                                    ".DB_TBL_CHILDREN.".last_name as last,
                                    ".DB_TBL_CHILDREN.".first_name as first
                                FROM 
                                    ".DB_TBL_TUTORS_RECAPS."
                                LEFT JOIN ".DB_TBL_PIPELINE." ON ".DB_TBL_PIPELINE.".id= ".DB_TBL_TUTORS_RECAPS.".pipeline_id
                                LEFT JOIN ".DB_TBL_CHILDREN." ON ".DB_TBL_CHILDREN.".id= ".DB_TBL_TUTORS_RECAPS.".child_id  
                                LEFT JOIN ".DB_TBL_CLIENTS." ON ".DB_TBL_CLIENTS.".id= ".DB_TBL_TUTORS_RECAPS.".client_id   
                                LEFT JOIN ".DB_TBL_TUTORS." ON ".DB_TBL_TUTORS_RECAPS.".tutor_id= ".DB_TBL_TUTORS.".id  
                                LEFT JOIN ".DB_TBL_APPLICANTS." ON ".DB_TBL_APPLICANTS.".id= ".DB_TBL_TUTORS.".applicant_id 
                            WHERE 


                            " . DB_TBL_CLIENTS . ".status = 'Existing' AND

                            " . DB_TBL_APPLICANTS . ".status = 'Existing' AND

                            " . DB_TBL_PIPELINE . ".status = 'Existing' AND                             
                            " . DB_TBL_TUTORS_RECAPS . ".is_deleted = '0'                               
                                GROUP BY " . DB_TBL_TUTORS_RECAPS . ".id
                                ORDER BY " . DB_TBL_TUTORS_RECAPS . ".created DESC ";

    $totallogs      =   $db->query($sql);
    $filename       =   "Tutoring_Log.xls";
    $contents       =   "Recap# \t Tutor Name \t Client Name \t Child Name \t Type of tutoring \t Date of Tutoring session \t Hours tutored \t Billable Travel \t Billable Additional \t Total Billable \t Client Rate \t Tutor Pay Rate \t  \n";
    $contents       .=  "  \n";

    while($tutorRecords = $db->fetchNextObject($totallogs)){

        $contents .= "".$tutorRecords->id." \t ".$tutorRecords->tutor_first_name.' '.$tutorRecords->tutor_last_name." \t ".$tutorRecords->first_name.' '.$tutorRecords->last_name." \t ".$tutorRecords->first.' '.$tutorRecords->last." \t ".$globalsConstant['type_of_tutoring'][$tutorRecords->type_of_tutoring]." \t ".date(MDY,$tutorRecords->date_of_tutoring_session)." \t ".str_replace('.',':',$tutorRecords->hours_tutored)." \t ".str_replace('.',':',$tutorRecords->billable_travel)." \t ".str_replace('.',':',$tutorRecords->billable_additional)." \t ".str_replace('.',':',$tutorRecords->total_billable)." \t ".CURRENCY.$tutorRecords->rate." \t  ".CURRENCY.$tutorRecords->tutor_pay_rate."  \t \n";

    }

    header('Content-type: application/ms-excel');
    header('Content-Disposition: attachment; filename='.$filename);
    echo $contents;
      ?>

回答by Cruachan

The TinyButStrong template system (www.tinybutstrong.com) has an optional plugin that allows you to write to excel templates.

TinyButStrong 模板系统 (www.tinybutstrong.com) 有一个可选插件,允许您写入 Excel 模板。

I've used this a great deal. It's particularly neat in that you can set up the page to write to either HTML or Excel depending on a flag - so typically my reports offer 'display on page' and 'write to excel' options.

我已经大量使用了这个。它特别简洁,因为您可以根据标志将页面设置为写入 HTML 或 Excel - 所以通常我的报告提供“在页面上显示”和“写入 excel”选项。