本文介绍了将 HTML 代码转换为 XLS/XLSX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PHP 生成包含多个工作表、样式、背景颜色等的 XLS 工作表.现在所有数据都在 HTML 表格中.(Sheet1.html, Sheet2.html ...)

I am trying to generate a XLS sheet with multiple sheets, styles, background colors and so on using PHP.Right now all data lie in HTML tables. (Sheet1.html, Sheet2.html ...)

有没有什么库可以把 HTML 转换成一个 xls 文件?

Is there any library to convert HTML into one xls file?

谢谢

推荐答案

这是一个很好的 PHP 类.

Here is a good PHP Class.

http://phpexcel.codeplex.com/

这是关于如何做到这一点的一般想法.我假设您的 HTML 表格位于二维数组中.如果它还不是一个数组,这里有一个很好的链接来执行此操作 从 HTML 表格到 PHP 数组的转换.

This is a general idea on how to do this. I'm assuming your HTML tables are in a 2-dimensional array. If it's not yet an array, here's a good link to do this conversion from HTML table to array in PHP.

$tabledata = array();
$tabledata[0][0] = "value stored in A1";
$tabledata[0][2] = "value stored in B1";
$tabledata[1][0] = "value stored in A2";
$tabledata[1][3] = "value stored in B2";

以第一个索引为行(excel的行号),第二个索引为列(excel的列字母).下面的代码是该站点的第一个示例代码,我修改了数据部分以从二维数组插入数据.

With the first index as the row (excel's row numbers) and the second index as the column (excel's column letters). The code below is the first sample code from the site and I modified the data part to insert data from a 2-dimensional array.

<?php
/** Error reporting */
error_reporting(E_ALL);

/** Include path **/
ini_set('include_path', ini_get('include_path').';../Classes/');

/** PHPExcel */
include 'PHPExcel.php';

/** PHPExcel_Writer_Excel2007 */
include 'PHPExcel/Writer/Excel2007.php';

// Create new PHPExcel object
echo date('H:i:s') . " Create new PHPExcel object\n";
$objPHPExcel = new PHPExcel();

// Set properties
echo date('H:i:s') . " Set properties\n";
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");


// Add some data
echo date('H:i:s') . " Add some data\n";
$objPHPExcel->setActiveSheetIndex(0);
foreach ($html as $row_index => $row_value) {
  foreach ($row_value as $column_index => $cell_value) {
    $objPHPExcel->getActiveSheet()->SetCellValue("'".chr($column_index+65).($row_index+1)."'", $cell_value);
  }
}
// $objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
// $objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');
// $objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');
// $objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');

// Rename sheet
echo date('H:i:s') . " Rename sheet\n";
$objPHPExcel->getActiveSheet()->setTitle('Simple');


// Save Excel 2007 file
echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));

// Echo done
echo date('H:i:s') . " Done writing file.\r\n";

这篇关于将 HTML 代码转换为 XLS/XLSX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 12:22