viest 6 år sedan
förälder
incheckning
179632bf12
7 ändrade filer med 300 tillägg och 33 borttagningar
  1. 23 2
      README.md
  2. 165 30
      README_zh.md
  3. 41 1
      kernel/excel.c
  4. 1 0
      kernel/include.h
  5. 5 0
      kernel/write.c
  6. 36 0
      tests/insert_url_format.phpt
  7. 29 0
      tests/insert_url_no_format.phpt

+ 23 - 2
README.md

@@ -2,6 +2,12 @@
 
 [![Build Status](https://travis-ci.org/viest/php-ext-excel-export.svg?branch=master)](https://travis-ci.org/viest/php-ext-excel-export)
 [![Build status](https://ci.appveyor.com/api/projects/status/w4cfjo9e4gsrs6rn/branch/master?svg=true)](https://ci.appveyor.com/project/viest/php-ext-excel-export/branch/master)
+[![](https://img.shields.io/github/release/viest/php-ext-excel-export.svg)](https://github.com/viest/php-ext-excel-export)
+[![](https://img.shields.io/badge/PHP-%3E%3D%207.0-brightgreen.svg)](https://github.com/viest/php-ext-excel-export)
+[![](https://img.shields.io/github/contributors/viest/php-ext-excel-export.svg)](https://github.com/viest/php-ext-excel-export)
+[![](https://img.shields.io/badge/platform-macos%20%7C%20linux%20%7C%20windows-brightgreen.svg)](https://github.com/viest/php-ext-excel-export)
+[![](https://img.shields.io/badge/license-BSD-green.svg)](https://github.com/viest/php-ext-excel-export)
+[![](https://img.shields.io/github/issues/viest/php-ext-excel-export.svg)](https://github.com/viest/php-ext-excel-export)
 
 #### Why use xlswriter
 
@@ -9,10 +15,25 @@ Please refer to the image below. PHPExcel has been unable to work properly for m
 
 ![php-excel](https://github.com/viest/php-excel-writer/blob/master/resource/performance_comparison.png)
 
-[中文文档](https://github.com/viest/php-ext-excel-export/blob/master/README_zh.md) | [Documents](https://github.com/viest/php-ext-excel-export/wiki)
+xlswriter is a PHP C Extension that can be used to write text, numbers, formulas and hyperlinks to multiple worksheets in an Excel 2007+ XLSX file. It supports features such as:
+
+* 100% compatible Excel XLSX files.
+* Full Excel formatting.
+* Merged cells.
+* Defined names.
+* Autofilters.
+* Charts.
+* Data validation and drop down lists.
+* Worksheet PNG/JPEG images.
+* Memory optimization mode for writing large files.
+* Works on Linux, FreeBSD, OpenBSD, OS X, Windows.
+* Compiles for 32 and 64 bit.
+* FreeBSD License.
+
+[文档](https://github.com/viest/php-ext-excel-export/blob/master/README_zh.md) | [Documents](https://github.com/viest/php-ext-excel-export/wiki)
 
 [![pecl](https://github.com/viest/php-excel-writer/blob/master/resource/pecl.png)](https://pecl.php.net/package/xlswriter)
 
 #### License
 
-BSD license
+FreeBSD license

+ 165 - 30
README_zh.md

@@ -1,28 +1,39 @@
-## Excel-Export
+## xlswriter [ Excel-Export ]
 
 Authon: viest [[email protected]](mailto:[email protected])
 
--------
+## 安装
+
+- [编译安装](#编译安装)
+- [PECL](#PECL)
+    
+## 使用
+    
+1. [创建一个简单的xlsx文件](#创建一个简单的xlsx文件)
+2. [单元格插入文字](#单元格插入文字)
+3. [单元格插入链接](#单元格插入链接)
+4. [单元格插入公式](#单元格插入公式)
+5. [单元格插入本地图片](#单元格插入本地图片)
+6. [数据过滤](#数据过滤)
+7. [合并单元格](#合并单元格)
+8. [设置列单元格样式](#设置列单元格格式)
+9. [设置行单元格样式](#设置行单元格格式)
+10. [固定内存导出](#固定内存导出)
+11. [创建工作表](#创建工作表)
+12. [组合样式](#组合样式)
+13. [样式列表](#样式列表)
+
+## PECL
 
-- **安装**
-    - **[编译安装 Excel-Export](#编译安装)**
-- **使用**
-    - **[创建一个简单的xlsx文件](#创建一个简单的xlsx文件)**
-    - **[1、向单元格插入文字](#向单元格插入文字)**
-    - **[2、单元格插入公式](#单元格插入公式)**
-    - **[3、单元格插入本地图片](#单元格插入本地图片)**
-    - **[4、数据过滤](#数据过滤)**
-    - **[5、合并单元格](#合并单元格)**
-    - **[6、设置列单元格格式](#设置列单元格格式)**
-    - **[7、设置行单元格格式](#设置行单元格格式)**
-    - **[8、固定内存导出](#固定内存导出)**
-    - **[9、向文件追加表格](#追加表格)**
+```bash
+pecl install xlswriter
 
---------
+# 添加 extension = xlswriter.so 到 ini 配置
+```
 
-### 编译安装
+## 编译安装
 
-#### Unix
+### Unix
 
 ##### Ubuntu
 
@@ -31,7 +42,7 @@ Authon: viest [[email protected]](mailto:[email protected])
 
 sudo apt-get install -y zlib1g-dev
 
-# Excel-Export
+# 扩展
 
 git clone https://github.com/viest/php-ext-excel-export.git
 
@@ -53,7 +64,7 @@ make && make install
 
 brew install zlib
 
-# Excel-Export
+# 扩展
 
 git clone https://github.com/viest/php-ext-excel-export.git
 
@@ -122,7 +133,7 @@ $filePath = $excel->fileName('tutorial01.xlsx', 'sheet1')
     ->output();
 ```
 
-### 单元格插入文字
+### 单元格插入文字
 
 #### 语法
 
@@ -162,9 +173,53 @@ for ($index = 0; $index < 10; $index++) {
 $textFile->output();
 ```
 
+### 单元格插入链接
+
+#### 函数原型
+
+```php
+insertUrl(int $row, int $column, string $url[, resource $format])
+```
+
+##### int $row
+
+> 单元格所在行
+
+##### int $column
+
+> 单元格所在列
+
+##### string $url
+
+> 链接地址
+
+##### resource $format
+
+> 链接样式
+
+##### 实例
+
+```php
+$excel = new \Vtiful\Kernel\Excel($config);
+
+$urlFile = $excel->fileName("free.xlsx")
+    ->header(['url']);
+
+$fileHandle = $fileObject->getHandle();
+
+$format    = new \Vtiful\Kernel\Format($fileHandle);
+$urlStyle = $format->bold()
+    ->underline(Format::UNDERLINE_SINGLE)
+    ->toResource();
+
+$urlFile->insertUrl(1, 0, 'https://github.com', $urlStyle);
+
+$textFile->output();
+```
+
 ### 单元格插入公式
 
-#### 语法
+#### 函数原型
 
 ```php
 insertFormula(int $row, int $column, string $formula)
@@ -203,7 +258,7 @@ $freeFile->output();
 
 ### 单元格插入本地图片
 
-#### 语法
+#### 函数原型
 
 ```php
 insertImage(int $row, int $column, string $localImagePath[, double $widthScale, double $heightScale])
@@ -245,7 +300,7 @@ $freeFile->output();
 
 ### 数据过滤
 
-#### 语法
+#### 函数原型
 
 ```php
 autoFilter(string $scope);
@@ -267,7 +322,7 @@ $excel->fileName('test.xlsx')
 
 ### 合并单元格
 
-#### 语法
+#### 函数原型
 
 ```php
 mergeCells(string $scope, string $data);
@@ -291,7 +346,7 @@ $excel->fileName("test.xlsx")
 
 ### 设置列单元格格式
 
-#### 语法
+#### 函数原型
 
 ```php
 setColumn(string $range, double $width [, resource $format]);
@@ -329,7 +384,7 @@ $fileObject->header(['name', 'age'])
 
 ### 设置行单元格格式
 
-#### 语法
+#### 函数原型
 
 ```php
 setRow(string $range, double $height [, resource $format]);
@@ -377,7 +432,7 @@ $fileObject->header(['name', 'age'])
 
 最大内存使用量 = 最大一行的数据占用量
 
-#### 语法
+#### 函数原型
 
 ```php
 constMemory(string $fileName);
@@ -401,9 +456,9 @@ $fileObject->header(['name', 'age'])
     ->output();
 ```
 
-### 追加表格
+### 创建工作表
 
-#### 语法
+#### 函数原型
 
 ```php
 addSheet([string $sheetName]);
@@ -433,3 +488,83 @@ $fileObject->addSheet()
 $filePath = $fileObject->output();
 ```
 
+### 组合样式
+
+将多个样式合并为一个新样式应用在单元格上
+
+```php
+// 将粗体与斜体合并为一个样式
+$format          = new \Vtiful\Kernel\Format($fileHandle);
+$boldItalicStyle = $format->bold()->italic()->toResource();
+```
+
+### 样式列表
+
+##### 粗体
+
+```php
+$format    = new \Vtiful\Kernel\Format($fileHandle);
+$boldStyle = $format->bold()->toResource();
+```
+##### 斜体
+
+```php
+$format      = new \Vtiful\Kernel\Format($fileHandle);
+$italicStyle = $format->italic()->toResource();
+```
+##### 下划线
+
+###### 函数原型
+
+```php
+underline(resource $resourchHandle, Format::const $style): \Vtiful\Kernel\Format
+```
+
+###### 实例
+
+```php
+$format         = new \Vtiful\Kernel\Format($fileHandle);
+$underlineStyle = $format->underline(Format::UNDERLINE_SINGLE)->toResource();
+```
+###### Style 
+
+```php
+Format::UNDERLINE_SINGLE;            // 单下划线
+Format::UNDERLINE_DOUBLE;            // 双下划线
+Format::UNDERLINE_SINGLE_ACCOUNTING; // 会计用单下划线
+Format::UNDERLINE_DOUBLE_ACCOUNTING; // 会计用双下划线
+```
+
+##### 对齐
+
+###### 函数原型
+
+```php
+align(resource $resourchHandle, Format::const ...$style): \Vtiful\Kernel\Format
+```
+
+###### 实例
+
+```php
+$format     = new \Vtiful\Kernel\Format($fileHandle);
+$alignStyle = $format
+    ->align(Format::FORMAT_ALIGN_CENTER, Format::FORMAT_ALIGN_VERTICAL_CENTER)
+    ->toResource();
+```
+###### Style 
+
+```php
+Format::FORMAT_ALIGN_LEFT;                 // 水平左对齐
+Format::FORMAT_ALIGN_CENTER;               // 水平剧中对齐
+Format::FORMAT_ALIGN_RIGHT;                // 水平右对齐
+Format::FORMAT_ALIGN_FILL;                 // 水平填充对齐
+Format::FORMAT_ALIGN_JUSTIFY;              // 水平两端对齐
+Format::FORMAT_ALIGN_CENTER_ACROSS;        // 横向中心对齐
+Format::FORMAT_ALIGN_DISTRIBUTED;          // 分散对齐
+Format::FORMAT_ALIGN_VERTICAL_TOP;         // 顶部垂直对齐
+Format::FORMAT_ALIGN_VERTICAL_BOTTOM;      // 底部垂直对齐
+Format::FORMAT_ALIGN_VERTICAL_CENTER;      // 垂直剧中对齐
+Format::FORMAT_ALIGN_VERTICAL_JUSTIFY;     // 垂直两端对齐
+Format::FORMAT_ALIGN_VERTICAL_DISTRIBUTED; // 垂直分散对齐
+```
+

+ 41 - 1
kernel/excel.c

@@ -83,6 +83,13 @@ ZEND_BEGIN_ARG_INFO_EX(xls_insert_text_arginfo, 0, 0, 4)
                 ZEND_ARG_INFO(0, row)
                 ZEND_ARG_INFO(0, column)
                 ZEND_ARG_INFO(0, data)
+                ZEND_ARG_INFO(0, format_handle)
+ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO_EX(xls_insert_url_arginfo, 0, 0, 4)
+                ZEND_ARG_INFO(0, row)
+                ZEND_ARG_INFO(0, column)
+                ZEND_ARG_INFO(0, url)
                 ZEND_ARG_INFO(0, format)
 ZEND_END_ARG_INFO()
 
@@ -368,7 +375,7 @@ PHP_METHOD(vtiful_xls, insertText)
  */
 PHP_METHOD(vtiful_xls, insertChart)
 {
-    zval *chart_resource;
+    zval      *chart_resource;
     zend_long row, column;
 
     ZEND_PARSE_PARAMETERS_START(3, 3)
@@ -385,6 +392,38 @@ PHP_METHOD(vtiful_xls, insertChart)
 }
 /* }}} */
 
+/** {{{ \Vtiful\Kernel\xls::insertUrl(int $row, int $column, string $url)
+ */
+PHP_METHOD(vtiful_xls, insertUrl)
+{
+    zend_long row, column;
+    zend_string *url = NULL;
+    zval *format_handle = NULL;
+
+    int argc = ZEND_NUM_ARGS();
+
+    ZEND_PARSE_PARAMETERS_START(3, 4)
+            Z_PARAM_LONG(row)
+            Z_PARAM_LONG(column)
+            Z_PARAM_STR(url)
+            Z_PARAM_OPTIONAL
+            Z_PARAM_RESOURCE(format_handle)
+    ZEND_PARSE_PARAMETERS_END();
+
+    ZVAL_COPY(return_value, getThis());
+
+    xls_object *obj = Z_XLS_P(getThis());
+
+    if (argc == 4) {
+        url_writer(row, column, &obj->ptr, url, zval_get_format(format_handle));
+    }
+
+    if (argc == 3) {
+        url_writer(row, column, &obj->ptr, url, NULL);
+    }
+}
+/* }}} */
+
 /** {{{ \Vtiful\Kernel\xls::insertImage(int $row, int $column, string $imagePath)
  */
 PHP_METHOD(vtiful_xls, insertImage)
@@ -544,6 +583,7 @@ zend_function_entry xls_methods[] = {
         PHP_ME(vtiful_xls, autoFilter,    xls_auto_filter_arginfo,    ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_xls, insertText,    xls_insert_text_arginfo,    ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_xls, insertChart,   xls_insert_chart_arginfo,   ZEND_ACC_PUBLIC)
+        PHP_ME(vtiful_xls, insertUrl,     xls_insert_url_arginfo,     ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_xls, insertImage,   xls_insert_image_arginfo,   ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_xls, insertFormula, xls_insert_formula_arginfo, ZEND_ACC_PUBLIC)
         PHP_ME(vtiful_xls, mergeCells,    xls_merge_cells_arginfo,    ZEND_ACC_PUBLIC)

+ 1 - 0
kernel/include.h

@@ -115,6 +115,7 @@ STATIC void _populate_range_dimensions(lxw_workbook *self, lxw_series_range *ran
 
 void type_writer(zval *value, zend_long row, zend_long columns, xls_resource_t *res, zend_string *format);
 void chart_writer(zend_long row, zend_long columns, xls_resource_chart_t *chart_resource, xls_resource_t *res);
+void url_writer(zend_long row, zend_long columns, xls_resource_t *res, zend_string *url, lxw_format *format);
 void image_writer(zval *value, zend_long row, zend_long columns, double width, double height, xls_resource_t *res);
 void formula_writer(zval *value, zend_long row, zend_long columns, xls_resource_t *res);
 void auto_filter(zend_string *range, xls_resource_t *res);

+ 5 - 0
kernel/write.c

@@ -44,6 +44,11 @@ void type_writer(zval *value, zend_long row, zend_long columns, xls_resource_t *
     }
 }
 
+void url_writer(zend_long row, zend_long columns, xls_resource_t *res, zend_string *url, lxw_format *format)
+{
+    worksheet_write_url(res->worksheet, row, columns, ZSTR_VAL(url), format);
+}
+
 /*
  * Write the image to the file
  */

+ 36 - 0
tests/insert_url_format.phpt

@@ -0,0 +1,36 @@
+--TEST--
+Check for vtiful presence
+--SKIPIF--
+<?php if (!extension_loaded("xlswriter")) print "skip"; ?>
+--FILE--
+<?php
+$config = [
+    'path' => './tests'
+];
+
+$fileObject = new \Vtiful\Kernel\Excel($config);
+$fileObject = $fileObject->fileName('tutorial.xlsx');
+$fileHandle = $fileObject->getHandle();
+
+$format     = new \Vtiful\Kernel\Format($fileHandle);
+$alignStyle = $format->align(
+    \Vtiful\Kernel\Format::FORMAT_ALIGN_CENTER,
+    \Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_CENTER
+)->toResource();
+
+$filePath = $fileObject->header(['name', 'age'])
+    ->data([
+        ['viest', 21],
+        ['wjx',   21]
+    ])
+    ->insertUrl(3, 0, 'https://github.com', $alignStyle)
+    ->output();
+
+var_dump($filePath);
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . '/tutorial.xlsx');
+?>
+--EXPECT--
+string(21) "./tests/tutorial.xlsx"

+ 29 - 0
tests/insert_url_no_format.phpt

@@ -0,0 +1,29 @@
+--TEST--
+Check for vtiful presence
+--SKIPIF--
+<?php if (!extension_loaded("xlswriter")) print "skip"; ?>
+--FILE--
+<?php
+$config = [
+    'path' => './tests'
+];
+
+$fileObject = new \Vtiful\Kernel\Excel($config);
+$fileObject = $fileObject->fileName('tutorial.xlsx');
+
+$filePath = $fileObject->header(['name', 'age'])
+    ->data([
+        ['viest', 21],
+        ['wjx',   21]
+    ])
+    ->insertUrl(3, 0, 'https://github.com')
+    ->output();
+
+var_dump($filePath);
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . '/tutorial.xlsx');
+?>
+--EXPECT--
+string(21) "./tests/tutorial.xlsx"