Spreadsheet_Excel_Writer

  1. <?php
  2. //调用Writer.php
  3. require_once ‘Spreadsheet/Excel/Writer.php’;
  4. //创建Workbook
  5. $workbook = new Spreadsheet_Excel_Writer();
  6. //定义导出Excel文件名
  7. $workbook->send(‘test.xls’);
  8. //创建Worksheet
  9. $worksheet =& $workbook->addWorksheet(‘My first worksheet’);
  10. //设置字体大小
  11. $format_column = & $workbook->addformat(array(‘Size’=>9,’Bold’=>1));
  12. //数据写入
  13. //标题行(第一行)
  14. $worksheet->write(0, 0, ‘Name’, $format_column);
  15. $worksheet->write(0, 1, ‘Age’, $format_column);
  16. //第一个人(第二行)
  17. $worksheet->write(1, 0, ‘John Smith’);
  18. $worksheet->write(1, 1, 30);
  19. //第二个人(第三行)
  20. $worksheet->write(2, 0, ‘Johann Schmidt’);
  21. $worksheet->write(2, 1, 31);
  22. //第三个人(第四行)
  23. $worksheet->write(3, 0, ‘Juan Herrera’);
  24. $worksheet->write(3, 1, 32);
  25. //关闭Workbook
  26. $workbook->close();
  27. ?>

################################

Excel_Reader

  • <?php
  • //调用Reader
  • require_once ‘Spreadsheet/Excel/Reader/reader.php’;
  • //创建 Reader
  • $data = new Spreadsheet_Excel_Reader();
  • //设置文本输出编码
  • $data->setOutputEncoding(‘GB2312’);
  • //读取Excel文件
  • $data->read("c:\test.xls");
  • //$data->sheets[0][‘numRows’]为Excel行数
  • for ($i = 1; $i <= $data->sheets[0][‘numRows’]; $i++) {
  •   //$data->sheets[0][‘numCols’]为Excel列数
  •   for ($j = 1; $j <= $data->sheets[0][‘numCols’]; $j++) {
  •   //显示每个单元格内容
  •   echo $data->sheets[0][‘cells’][$i][$j];
  •   }
  • }
  • ?>
  • Advertisements

    发表评论

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / 更改 )

    Twitter picture

    You are commenting using your Twitter account. Log Out / 更改 )

    Facebook photo

    You are commenting using your Facebook account. Log Out / 更改 )

    Google+ photo

    You are commenting using your Google+ account. Log Out / 更改 )

    Connecting to %s