php中pdf word excel操作類分享_PHP教程

來源:互聯網
上載者:User
很早的時候,用php產生execl都是件麻煩的事,我一般都會用csv來替代,現在這類工具就很多了,並且比較成熟了。不光有excel的,word,pdf。

1,php excelreader操作excel的php類,產生,讀取excel等。功能很強大。

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

解壓後,裡面有很多例子,調用方法簡單。

例1

代碼如下 複製代碼

/**
*
* @copyright 2007-2012 Xiaoqiang.
* @author Xiaoqiang.Wu
* @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()."
";
}
}

?>

例2

代碼如下 複製代碼

/**
*
* @copyright 2007-2012 Xiaoqiang.
* @author Xiaoqiang.Wu
* @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()."
";
}
}

?>

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文檔

http://www.bkjia.com/PHPjc/631579.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631579.htmlTechArticle很早的時候,用php產生execl都是件麻煩的事,我一般都會用csv來替代,現在這類工具就很多了,並且比較成熟了。不光有excel的,word,pdf。...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.