简体   繁体   English

PHP Excel,但没有Spreadsheet_Excel_Writer

[英]PHP Excel, but no Spreadsheet_Excel_Writer

I need to save some data in .xls file. 我需要在.xls文件中保存一些数据。 I am trying to use PEAR library Spreadsheet_Excel_Writer. 我正在尝试使用PEAR库Spreadsheet_Excel_Writer。 It is work. 这是工作。 But now i am working with hosting without PEAR. 但现在我正在与没有PEAR的托管合作。 Of cause, it is very terrible. 原因,这是非常可怕的。

Spreadsheet_Excel_Writer has a lot of dependencies. Spreadsheet_Excel_Writer有很多依赖项。 I need a module or code, which i can include in my scripts. 我需要一个模块或代码,我可以在脚本中包含它。

Thx. 谢谢。

It's a little-known format that never really caught on, but MS Office has support for so-called Excel 2003 XML files - which are indeed XML and, at a bare minimum, look like this: 这是一个鲜为人知的格式,从未真正流行,但MS Office支持所谓的Excel 2003 XML文件 - 这些文件确实是XML,并且至少看起来像这样:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
    xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
  <Worksheet>
    <Row>
      <Cell>
        <Data ss:Type="String">First</Data>
      </Cell>
    </Row>
   </Worksheet>
 </Workbook>

You could generate this quite simply without any PHP extensions, and it will open correctly in any version of MS Excel since 2003. OpenOffice 2 and 3 will open these correctly if you rename the file extension to .xls.xml 你可以很简单地生成这个,没有任何PHP扩展,并且它将在2003年以来的任何版本的MS Excel中正确打开。如果你将文件扩展名重命名为.xls.xml OpenOffice 2和3将正确打开它们。

When a user opens this file and saves it from Excel, it will be, by default, converted to the classic XLS file. 当用户打开此文件并将其从Excel保存时,默认情况下,它将转换为经典XLS文件。

As @Alexander.Plutov notes in a comment, there is a library for writing these files . 正如@ Alexander.Plutov在评论中指出的那样,有一个用于编写这些文件的库

There's a useful (and more comprehensive) page on IBM's site about this format and PHP: http://www.ibm.com/developerworks/opensource/library/os-phpexcel/#N101F7 IBM网站上有一个关于这种格式和PHP的有用(且更全面)的页面: http//www.ibm.com/developerworks/opensource/library/os-phpexcel/#N101F7

I use Spreadsheet Writer a lot and it's nice to have, but if you don't need multiple sheets or the ability to do some formatting, etc, you an always just output HTML and send a 我经常使用Spreadsheet Writer,它很高兴,但如果你不需要多张工作表或能够做一些格式化等,你总是只输出HTML并发送一个

header("Content-type: application/vnd.ms-excel")

and an extension of ".xls" and then Excel or OpenOffice's spreadsheet program (and I assume, I guess, Mac's spreadsheet program) will open it and format the HTML as a spreadsheet. 和“.xls”的扩展,然后Excel或OpenOffice的电子表格程序(我猜,我猜,Mac的电子表格程序)将打开它并将HTML格式化为电子表格。

It's not a native solution but in the end, it looks pretty good to the end-user. 它不是本机解决方案,但最终对最终用户来说看起来不错。 You have to use tables in your html but you can add colors/borders, padding, font changes, rowspan, colspan, etc, for a bit of control. 你必须在你的html中使用表格,但你可以添加颜色/边框,填充,字体更改,rowspan,colspan等,以进行一些控制。 No functions or formulas or multiple sheets, though. 但是,没有功能或公式或多张表。

从字面上看,PHPExcel: http ://phpexcel.codeplex.com/

Im using http://www.bettina-attack.de/jonny/view.php/projects/php_writeexcel/ 我正在使用http://www.bettina-attack.de/jonny/view.php/projects/php_writeexcel/

I got it simply included in many projects and it works like a charm. 我把它简单地包含在许多项目中,它就像一个魅力。

Beware, the source code provided by Piskvor: 请注意,Piskvor提供的源代码:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
    xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
  <Worksheet>
    <Row>
      <Cell>
        <Data ss:Type="String">First</Data>
      </Cell>
    </Row>
   </Worksheet>
 </Workbook>

is not a valid Excel file (at least in my version – Excel 2010) and will fail opening producing an error message, as it is missing two required values corrected in the following example (the missing part is that within a WorkSheet you need a Table and a WorkSheet must have a name specified as a string in ss:Name ): 不是一个有效的Excel文件(至少在我的版本中 - Excel 2010)并且将无法打开生成错误消息,因为它缺少在以下示例中更正的两个必需值(缺少的部分是在WorkSheet需要一个Table并且工作WorkSheet必须在ss:Name指定名称作为字符串:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
    xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
   <Worksheet ss:Name="test">
     <Table>
       <Row>
         <Cell>
           <Data ss:Type="String">First</Data>
         </Cell>
       </Row>
     </Table>
   </Worksheet>
 </Workbook>

This then becomes a valid excel spreadsheet indeed and thanks to Piskvor for reminding us of this wonderful open and machine- and human-readable format. 这确实成为一个有效的excel电子表格,感谢Piskvor提醒我们这种精彩的开放式,机器和人类可读的格式。

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

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