计划,每天学习两小时,坚持带来大改变。

php中pdf word excel操作类分享

PHP 阿尤 3116浏览 0评论
很早的时候,用php生成execl都是件麻烦的事,我一般都会用csv来替代,现在这类工具就很多了,并且比较成熟了。不光有excel的,word,pdf。
1,php excelreader操作excel的php类,生成,读取excel等。功能很强大。

下载地址:http://sourceforge.net/projects/phpexcelreader/

解压后,里面有很多例子,调用方法简单。

例1

 代码如下 复制代码

<?php
/**
 *
 * @copyright 2007-2012 Xiaoqiang.
 * @author Xiaoqiang.Wu <[email protected]>
 * @version 1.01
 */
 
error_reporting(E_ALL);
 
date_default_timezone_set('Asia/ShangHai');
 
/** PHPExcel_IOFactory */
require_once '../Classes/PHPExcel/IOFactory.php';
 
 
// Check prerequisites
if (!file_exists("31excel5.xls")) {
 exit("not found 31excel5.xls.n");
}
 
$reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
$PHPExcel = $reader->load("31excel5.xls"); // 载入excel文件
$sheet = $PHPExcel->getSheet(0); // 读取第一??工作表
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumm = $sheet->getHighestColumn(); // 取得总列数
$highestColumm= PHPExcel_Cell::columnIndexFromString($colsNum); //字母列转换为数字列 如:AA变为27
 
/** 循环读取每个单元格的数据 */
for ($row = 1; $row <= $highestRow; $row++){//行数是以第1行开始
    for ($column = 0; $column < $highestColumm; $column++) {//列数是以第0列开始
        $columnName = PHPExcel_Cell::stringFromColumnIndex($column);
        echo $columnName.$row.":".$sheet->getCellByColumnAndRow($column, $row)->getValue()."<br />";
    }
}
 
?>

例2

 代码如下 复制代码

<?php
/**
 *
 * @copyright 2007-2012 Xiaoqiang.
 * @author Xiaoqiang.Wu <[email protected]>
 * @version 1.01
 */
 
error_reporting(E_ALL);
 
date_default_timezone_set('Asia/ShangHai');
 
/** PHPExcel_IOFactory */
require_once '../Classes/PHPExcel/IOFactory.php';
 
 
// Check prerequisites
if (!file_exists("31excel5.xls")) {
 exit("not found 31excel5.xls.n");
}
 
$reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
$PHPExcel = $reader->load("31excel5.xls"); // 载入excel文件
$sheet = $PHPExcel->getSheet(0); // 读取第一??工作表
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumm = $sheet->getHighestColumn(); // 取得总列数
 
/** 循环读取每个单元格的数据 */
for ($row = 1; $row <= $highestRow; $row++){//行数是以第1行开始
    for ($column = 'A'; $column <= $highestColumm; $column++) {//列数是以A列开始
        $dataset[] = $sheet->getCell($column.$row)->getValue();
        echo $column.$row.":".$sheet->getCell($column.$row)->getValue()."<br />";
    }
}
 
?>

2,phpdocx操作word的php类

PHPDocx是一个用于生成完全动态的、完全兼容的Word文档的PHP类库。

你可能需要直接从任何数据集合或者表格文件来生成报表。这些报表也许会包括图标、图片、表格、开头、结束等等数据。

PHPDocx能够使用一些预定义的模板文件来生成Word文档,这大大简化了工作量。使用很少的一些代码,你能够将PHPDocx集成到你的WEB站点或网络应用,这样能够为你的用户或雇员提供一个很有价值的服务。

 代码如下 复制代码

Basic example
// Include the PHPWord.php, all other classes were loaded by an autoloader
require_once 'PHPWord.php';

// Create a new PHPWord Object
$PHPWord = new PHPWord();

// Every element you want to append to the word document is placed in a section. So you need a section:
$section = $PHPWord->createSection();

// After creating a section, you can append elements:
$section->addText('Hello world!');

// You can directly style your text by giving the addText function an array:
$section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));

// If you often need the same style again you can create a user defined style to the word document
// and give the addText function the name of the style:
$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle');

// You can also putthe appended element to local object an call functions like this:
$myTextElement = $section->addText('Hello World!');
$myTextElement->setBold();
$myTextElement->setName('Verdana');
$myTextElement->setSize(22);

// At least write the document to webspace:
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('helloWorld.docx');

下载地址:http://www.phpdocx.com/

在线演示地址:http://www.phpdocx.com/demo/sample-word-report

3,tcpdf操作pdf的php类

下载地址:http://sourceforge.net/projects/html2fpdf/?source=recommended

在线演示地址:http://www.tcpdf.org/examples.php

下载后,基本上都是有例子的,下载后的东西比较大,这是因为,里面有很多例子,供例子用的pdf,word文件这类,也有很多字体文件。要用的类文件其实并不大的。记录一下用的时候,就不用到处找了。哈哈。

TCPDF自带的65个examples之后,就能完全掌握它的使用方法了。

大体可以分为如下5个步骤:

1.      require_once导入tcpdf.php文件和config/lang/目录的相应语系

2.      实例化TCPDF

3.      设置PDF文档的格式,包括文档信息、页眉、页尾、字体、外间距、图片边框、分页等

4.      导入PDF文档的内容,可以是单行或多行简单字符串,也可以HTML格式的字符串等

5.      输出PDF文档

转载请注明:阿尤博客 » php中pdf word excel操作类分享

游客
发表我的评论 换个身份
取消评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  • 验证码 (必填)点击刷新验证码