phpoffice文档笔记

phpword

html转word

<?php
namespace app\index\controller;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\Html;
class Index extends Controller
{
	public function index(){
		$word = new PhpWord();
		$content = $word->addSection();
		$html = new Html();
		$html::addHtml($content, "<p style='color:#FF4400'>test</p><p><img src='http://t.com/image.png'/></p>", false, false);
		$word->save('test.docx');
	}
}

图片路径要么网络地址,要么图片在服务器的绝对路径,普通A4纸,图片宽度建议750


phpexcel

从数据库导出

<?php
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class Index {
  public function export()
    {
        $model = new Spreadsheet();
        $sheet = $model->getActiveSheet();
        $model->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
        //设置值,单元格,值
        $sheet->setCellValue('A1', 'ID');
       	$sheet->setCellValue('A2', '1');
        //实例化类
        $writer = new Xlsx($model);
        //保存到本地
        $writer->save(test.xlsx');
    }
}
posted @ 2022-06-24 09:56  小枫同学  阅读(660)  评论(0编辑  收藏  举报