https://pecl.php.net/package/xlswriter
https://xlswriter-docs.viest.me/zh-cn/kuai-su-shang-shou/chuang-jian-wen-jian

wget https://pecl.php.net/get/xlswriter-1.3.2.tgz
tar xf xlswriter-1.3.2.tgz
cd xlswriter-1.3.2
/www/server/php/72/bin/phpize
./configure --with-php-config=/www/server/php/72/bin/php-config --enable-reader 
 make && make install

pecl install xlswriter
[xlswriter]
extension=/www/server/php/72/lib/php/extensions/no-debug-non-zts-20170718/xlswriter.so

读取

$config   = ['path' => './'];
$excel    = new \Vtiful\Kernel\Excel($config);
$data = $excel->openFile('1.xlsx')
    ->openSheet()
    ->getSheetData();

多表
$sheetList = $excel->openFile('tutorial.xlsx')
    ->sheetList();
​
foreach ($sheetList as $sheetName) {
    echo 'Sheet Name:' . $sheetName . PHP_EOL;
​
    // 通过工作表名称获取工作表数据
    $sheetData = $excel
        ->openSheet($sheetName)
        ->getSheetData();
​
    var_dump($sheetData);
}
读取表头
$data = $excel->openFile('1.xlsx')
    ->openSheet();
$excel->nextRow();
跳过第一行
$data = $excel->openFile('tutorial.xlsx')
    ->openSheet('Sheet1')
    ->setSkipRows(1)//2表示跳过2行
    ->getSheetData();

->openSheet('Sheet1', \Vtiful\Kernel\Excel::SKIP_EMPTY_CELLS); //忽略空白格
->openSheet('Sheet1', \Vtiful\Kernel\Excel::SKIP_EMPTY_ROW);//忽略空白行

const SKIP_NONE = 0x00;         // 不忽略任何单元格、行
const SKIP_EMPTY_ROW = 0x01;    // 忽略空行
const SKIP_EMPTY_CELLS = 0x02;  // 忽略空单元格(肉眼观察单元格内无数据,并不代表单元格未定义、未使用)
const SKIP_EMPTY_VALUE = 0X100; // 忽略单元格空数据

->setType([
        \Vtiful\Kernel\Excel::TYPE_STRING,
        \Vtiful\Kernel\Excel::TYPE_INT,
        \Vtiful\Kernel\Excel::TYPE_TIMESTAMP,//时间类型
    ])
[
    2 => \Vtiful\Kernel\Excel::TYPE_TIMESTAMP,//从0开始
]
类型
const TYPE_STRING = 0x01;    // 字符串
const TYPE_INT = 0x02;       // 整型
const TYPE_DOUBLE = 0x04;    // 浮点型
const TYPE_TIMESTAMP = 0x08; // 时间戳,可以将 xlsx 文件中的格式化时间字符转为时间戳

$f = $excel->constMemory('1.xlsx',"导出表")
    ->header(['姓名', '年龄','时间',"公式","图片"]);
for ($index = 0; $index < 10; $index++) {
    $f->insertText($index+1, 0, '李白');
    $f->insertUrl($index+1, 1, 'https://www.baidu.com');
    $f->insertDate($index+1, 2, time(),"yyyy-mm-dd hh:mm:ss");
    $f->insertFormula($index+1, 3, "=SUM(1+1)");
    $f->insertImage($index+1,4, "1.png",0.1,0.1);//最后参数缩放默认1
}
$path = $f->output();
/*header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Disposition: attachment;filename="下载.xlsx"');
header('Content-Length: ' . filesize($path));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Cache-Control: max-age=0');
header('Pragma: public');
ob_clean();
flush();
if (copy($path, 'php://output') === false) {
}
@unlink($path);*/
/*$fp = fopen('1.csv', 'a');
$csvResult = $excel->openFile('1.xlsx')
    ->openSheet()->setType([
        2 => \Vtiful\Kernel\Excel::TYPE_TIMESTAMP,//从0开始
    ])
    ->putCSV($fp);*/

导出

$filePath = $excel->fileName('2.xlsx',"导出表")//固定内存换constMemory
    ->header(['Item', 'Cost'])
    ->data($data)
    ->output();
作者:Yoby  创建时间:2020-07-23 20:03
 更新时间:2024-12-05 13:26
上一篇:
下一篇: